2017-07-03 8 views
2

私はthis repoでオープンソースプロジェクトを操作しています。ファイルbank.sqlは、mysqlのデータベースのスキーマです。Springセキュリティ:MySQL JDBC認証に失敗しました

<form name="loginForm" class="form-login" 
     action="<c:url value="/j_spring_security_check" />" method="POST"> 
     <h2>Please sign in</h2> 

     <c:if test="${not empty error}"> 
      <div class="alert alert-danger">${error}</div> 
     </c:if> 
     <c:if test="${not empty msg}"> 
      <div class="alert alert-info">${msg}</div> 
     </c:if> 

     <input type="text" class="form-control" placeholder="Username" name="username"> 
     <input type="password" class="form-control" placeholder="Password" name="password" /> 
     <button type="submit" class="btn btn-lg btn-primary btn-block" name="submit">Login</button> 
     <input type="hidden" name="${_csrf.parameterName}" 
      value="${_csrf.token}" /> 

    </form> 

ファイルSpring-Security.xmlは以下の通りです::

<dependencies> 

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/juli --> 
    <dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>juli</artifactId> 
     <version>6.0.26</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>jsp-api</artifactId> 
     <version>2.0</version> 
     <scope>provided</scope> 
    </dependency> 


    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>2.5</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.11</version> 
    <scope>test</scope> 
    </dependency> 

    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-core</artifactId> 
     <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>3.2.3.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.6</version> 
    </dependency> 

    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 

    <dependency> 
     <groupId>opensymphony</groupId> 
     <artifactId>sitemesh</artifactId> 
     <version>2.4.2</version> 
    </dependency> 
</dependencies> 

私は以下のようにログインフォームを持っている:

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

    <http auto-config="true" use-expressions="true"> 
     <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" /> 
     <intercept-url pattern="/user**" access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')" /> 
     <intercept-url pattern="/change**" access="hasRole('ROLE_NEWUSER')" /> 

     <access-denied-handler error-page="/403" /> 

     <form-login 
      login-page="/login" 
      authentication-success-handler-ref="bankCustomAuthenticationSuccessHandler" 
      authentication-failure-url="/login?error" 
      username-parameter="username" 
      password-parameter="password" /> 
     <logout logout-success-url="/login?logout" /> 
     <!-- enable csrf protection --> 
     <csrf/> 
    </http> 

    <beans:bean id="bankCustomAuthenticationSuccessHandler" 
     class="ee.mikkelsaar.bank.security.MyUrlAuthenticationSuccessHandler" /> 

    <authentication-manager> 
     <authentication-provider> 
      <password-encoder hash="sha" /> 
      <jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password, enabled from users where username=?" authorities-by-username-query="select u.username, a.authority from users u, authorities a where u.username = a.username and u.username =?" /> 
     </authentication-provider> 
    </authentication-manager> 

    <beans:import resource="spring-datasource.xml" /> 

    <beans:bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder"> 
     <beans:constructor-arg value="sha" /> 
    </beans:bean> 


</beans:beans> 

とデータソースを取得するために豆がある。ここpom.xmlがあります以下のようにAuthentication-managerに提供してください。

MySQLサーバーがポート3306で正常に動作していることを確信しています。

正しい資格情報はusername:Tompassword:Tomですが、ログインするたびに失敗します。私は疑問に思っています、私の認証プロセスに何が間違っていますか?

どうすれば修正できますか?

おそらく、データソースBeanが正しく作成されていないかもしれませんが、確認する方法がわかりません。

アップデート:私は私のSpring-Security.xml<http security="none" pattern="/login"/>を追加すると

それは

HTTP Status 405 - Request method 'POST' not supported for (username, password) `(Tom, tom)`, which is not a valid credential. But for a valid credential like `(Tom,Tom)` is still navigates to the login page again. 

と文句を言うが、それは春のセキュリティのすべてのの

+0

まず、「失敗する」とはどういう意味ですか?何かエラーが出ますか?はいの場合、スタックトレースを投稿してください。または、同じログインページに再度リダイレクトされるだけですか?2番目:自分のログインページのセキュリティが無効になっているとは思われません。あなたの既存の ''要素のすぐ上の 'Spring-Security.xml'に次の' 'を追加してみてください。 –

+0

また、 'org.springframework.security'パッケージ –

+0

@RomanPuchkovskiyのDEBUGロギングを有効にしてみてください。失敗すると、ログインページに再び移動します。あなたの答えで私の質問を更新しました –

答えて

7

まず、以前のバージョン4に、デフォルトのパラメータ名を起こりますj_usernamej_password(あなたが言及した投稿のように)であり、username/passwordではありません。春のセキュリティ4では

は、デフォルトの名前は usernamepasswordですが、 UsernamePasswordAuthenticationFilterが結合するデフォルトのURLは /loginない /j_spring_security_checkです。

したがって、すべてのSpring Securityバージョンでは、URLとパラメータ名の組み合わせがデフォルトと一致しません。

データベースに対するユーザ名 - パスワード認証を設定する方法の例を次に示します。http://www.mkyong.com/spring-security/spring-security-form-login-using-database/(Spring Security 3の場合)。x)は

別の例を(はるか)短くてシンプルな、春のセキュリティ4:https://spring.io/guides/gs/securing-web/パラメータは、フォームベースの認証を持っている場合、それはこのように動作し、基本的に

を渡され、どのように

  1. ユーザーは、認証が必要なURLにアクセスしようとします。ユーザーはその認証が不足しています
  2. スプリングセキュリティはユーザーをログインページにリダイレクトします
  3. ユーザーはログインとパスワードを入力して送信します。 4前に春のセキュリティのバージョンではデフォルトの設定では、ユーザ名は/j_spring_security_check URLにj_password
  4. /j_spring_security_checkに春のセキュリティが提供UsernamePasswordAuthenticationFilterプロセスの提出などj_usernameとパスワードとして提出されます。要求が(ログインフォームから)取得されると、パラメータ(ユーザ名/パスワード)が抽出され、UsernamePasswordAuthenticationTokenにパックされ、認証のためにAuthenticationManagerに送られます。
  5. AuthenticationManagerチェックアクセス(JDBCは、例えば、データベースに対してチェックするために使用することができる)
  6. 認証(ユーザが指定された名前で存在し、パスワードが一致した)成功した​​場合、結果Authenticationが構成されている(含まれていますロールに関する情報)が保存され、AuthenticationSuccessHandlerが呼び出されます。 Authentication結果
  7. 認証が成功すると、ユーザーは手順1でアクセスしようとしたURLにリダイレクトされ、ここでビジネスロジックコントローラが実行されます

1

あなたは、1つの変更を行う ためと認証マネージャ 認証マネージャ

<security:authentication-manager alias="authManager"> 
     <security:authentication-provider 
      ref="daoAuthProvider"> 
     </security:authentication-provider> 
    </security:authentication-manager> 



<beans:bean id="daoAuthProvider" 
     class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
     <beans:property name="userDetailsService"> 
      <beans:ref bean="userDetailsService" /> 
     </beans:property> 
     <beans:property name="passwordEncoder" ref="encoder"/> 
    </beans:bean> 

<beans:bean id="userDetailsService" 
     class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> 
     <beans:property name="dataSource"> 
      <beans:ref bean="coreDataSource" /> 
     </beans:property> 
     <beans:property name="usersByUsernameQuery"> 
      <beans:value> 
       SELECT username,password as password FROM userdetails WHERE 
       password != '' and username= ? 
      </beans:value> 
     </beans:property> 
     <beans:property name="authoritiesByUsernameQuery"> 
      <beans:value> 
       SELECT username,authority FROM authorities JOIN userdetails ON authorities.user_id = userdetails.user_id .. 
       WHERE userdetails.username= ? and 
      </beans:value> 
     </beans:property> 


<security:http pattern="/admin/admin.jsp" security="none" /> 
<security:http pattern="/**/*.js" security="none" /> .. 


     <security:custom-filter ref="formAuthFilter" 
      after="FORM_LOGIN_FILTER" /> 
    <beans:bean id="formAuthFilter" 
     class="com.sca.security.SCAAuthenticationProcessingFilter"> 

     <beans:property name="authenticationManager" ref="authManager" /> 
     <beans:property name="allowSessionCreation" value="true" /> 
     <beans:property name="authenticationFailureHandler" 
      ref="authFailureHandler" /> <!-- define authFailureHandler --> 
     <beans:property name="authenticationSuccessHandler" 
      ref="authSuccessHandler" /><!-- define authSuccessHandler --> 
     <beans:property name="filterProcessesUrl" value="/j_spring_security_check" /> 
<!-- define userDAO, globalFilter--> 

     </beans:property> 


    </beans:bean> 

これはまさにあなたが求めているものであるために試すことができます。あなたが満足している場合、または必要に応じてさらに尋ねる場合は、回答を受け入れることを忘れないでください。

1

あなたの春のセキュリティの設定が正しいです。 この問題を解決する鍵は、スプリングセキュリティのcsrf保護戦略を理解することです。詳しくは、[org.springframework.security.web.csrf.CsrfFilter.java] [1]ソースコードを参照してください。

サーバー側CSRFトークンリポジトリはセッションベースです。最初にリクエストを受け取ったときに生成されます。クライアント側のトークンが間違っている場合、または空のサーバがあなたの変更要求(.EG POST、PUT、DELETE)を返さない場合は、応答403の状態コードが返されます。

エラーは、ページに非表示の入力に古いcsrfトークンが保持されているため、すべてのログイン要求がエラーページに転送されているためにクライアント側のcsrfトークンをリフレッシュできないことが原因です。

単純に、あなたはログインページをリフレッシュして再度ログインを試みることができます。

関連する問題