2011-11-17 39 views
11

cấu hình web.xml của tôi làKhông AuthenticationProvider tìm thấy cho 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> 

ở đây là cấu hình an ninh của tôi

<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> 

Dưới đây là lớp customAuthProvider tôi

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()); 
     } 




} 

Trang hiển thị biểu mẫu đăng nhập và khi tôi nhập bob và bob làm thông tin đăng nhập, trang này sẽ phát ra lỗi sau.

Your login attempt was not successful, try again. 

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

Tôi đã kiểm tra nhật ký ở cấp gỡ lỗi TẤT CẢ và đây là những gì tôi nhận được.

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 

Bất kỳ trợ giúp nào về điều này .. tôi đang làm gì sai ở đây?

+0

Ngay sau khi đăng this.I đọc từ doc mùa xuân rằng phương pháp này sẽ trả về true, cho biết rằng các nhà cung cấp hỗ trợ xác thực. Và tôi đã trở về sai! , Tôi chỉ thay đổi nó thành sự thật và đăng nhập ứng dụng bảo mật mùa xuân đầu tiên của tôi đang hoạt động thành công !! Hy vọng thông tin này sẽ hữu ích cho bất kỳ ai bị mắc kẹt giống như tôi. @Override hỗ trợ boolean công khai (Lớp arg0) { // TODO Phương thức tạo tự động được tạo trả về true; } – cherit

Trả lời

29

Như bạn đã viết trong nhận xét của mình, vấn đề là bạn luôn trả lại false theo phương thức supports() của nhà cung cấp xác thực của bạn. Nhưng thay vì luôn trở true bạn nên kiểm tra authentication bạn nhận được như thế này:

public class MyAuthenticationProvider implements AuthenticationProvider, Serializable { 

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

    // ... 
} 
Các vấn đề liên quan