2011-11-17 25 views
11
見つかり

私のweb.xmlの設定は、私のcustomAuthProviderクラス私のセキュリティの設定ここではませんAuthenticationProviderがUsernamePasswordAuthenticationToken

<filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

ここ

<intercept-url pattern="/*" access="ROLE_USER" /> 
    <intercept-url pattern="/*.ico" filters="none" /> 


</http> 

<beans:bean id="customAuthenticationProvider" class="net.spring3.provider.MyAuthProvider" /> 

    <authentication-manager> 

     <authentication-provider ref="customAuthenticationProvider" /> 

    </authentication-manager> 

がされている

public class MyAuthProvider implements AuthenticationProvider { 


    @Override 
    public boolean supports(Class<? extends Object> arg0) { 
     // TODO Auto-generated method stub 
     return false; 
    } 


    @SuppressWarnings("serial") 
     private static Map<String, String> SIMPLE_USERS = new HashMap<String, String>(2) {{ 
      put("joe", "joe"); 
      put("bob", "bob"); 
     }}; 

     @SuppressWarnings("serial") 
     private static List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>(1) {{ 
      add(new GrantedAuthorityImpl("ROLE_USER")); 
     }}; 

     @Override 
     public Authentication authenticate(Authentication auth) throws AuthenticationException 
     { 
      // All your user authentication needs 
      System.out.println("==Authenticate Me=="); 
      if (SIMPLE_USERS.containsKey(auth.getPrincipal()) 
       && SIMPLE_USERS.get(auth.getPrincipal()).equals(auth.getCredentials())) 
      { 
       return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES); 
      } 
      throw new BadCredentialsException("Username/Password does not match for " + auth.getPrincipal()); 
     } 




} 

ページにログインフォームが表示されます。ログインとしてbobとbobと入力すると、次のエラーが表示されます。

Your login attempt was not successful, try again. 

Reason: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken 

私はすべてのデバッグレベルのログをチェックし、ここで私が得るものです。

FINE: Request is to process authentication 
Nov 17, 2011 5:37:36 AM org.springframework.context.support.AbstractApplicationContext publishEvent 
FINEST: Publishing event in Root WebApplicationContext: org.springframework.security.authentication.event.AuthenticationFailureProviderNotFoundEvent[source=org.springframew[email protected]ffff8dfd: Principal: sd; Credentials: [PROTECTED]; Authenticated: false; Details: org.sprin[email protected]fffe3f86: RemoteIpAddress: 127.0.0.1; SessionId: x4lg4vtktpw9; Not granted any authorities] 
Nov 17, 2011 5:37:36 AM org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter unsuccessfulAuthentication 
FINE: Authentication request failed: org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.UsernamePasswordAuthenticationToken 

私はここで間違っていますか?

+0

の方法は、プロバイダが認証をサポートしていることを示すためにtrueを返すことを春のドキュメントから読み取ります。そして、私は虚偽に戻っていた! 、私はちょうどそれをtrueに変更し、私の最初の春のセキュリティアプリのログインが正常に動作しています!この情報が、私のような邪魔をした人にとって有益であることを願っています。 @Override public boolean supports(Class <?extends Object> arg0){ // TODO自動生成メソッドスタブ return true; } – cherit

答えて

29

あなたのコメントに書いてあるとおり、問題はあなたのオーセンティックプロバイダのsupports()メソッドで常にfalseを返すことです。しかし、その代わりに、常にあなたはauthenticationあなたは、このように取得確認する必要がありますtrue返す:ちょうどthis.Iを掲示した後

public class MyAuthenticationProvider implements AuthenticationProvider, Serializable { 

    @Override 
    public boolean supports(Class<? extends Object> authentication) { 
     return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); 
    } 

    // ... 
} 
関連する問題