【问题标题】:No AuthenticationProvider found for UsernamePasswordAuthenticationToken未找到 UsernamePasswordAuthenticationToken 的 AuthenticationProvider
【发布时间】:2011-12-31 01:16:08
【问题描述】:

我的 web.xml 配置是

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

这是我的 customAuthProvider 类

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

我检查了调试级别 ALL 的日志,这就是我得到的。

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.springframework.security.authentication.UsernamePasswordAuthenticationToken@ffff8dfd: Principal: sd; Credentials: [PROTECTED]; Authenticated: false; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@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

对此有任何帮助..我在这里做错了什么?

【问题讨论】:

  • 刚刚发布了这个。我从spring doc中读到该方法应该返回true以表明提供者支持身份验证。而我返回的是假的! ,我刚刚将其更改为 true,并且我的第一个 Spring Security 应用程序登录成功!希望这些信息对像我一样被卡住的人有用。 @Override public boolean supports(Class extends Object> arg0) { // TODO 自动生成的方法存根 return true; }

标签: java spring spring-mvc spring-security spring-3


【解决方案1】:

正如您在评论中已经写的那样,问题是您总是在您的身份验证提供程序的supports() 方法中返回false。但不要总是返回true,您应该检查authentication,您会得到这样的结果:

public class MyAuthenticationProvider implements AuthenticationProvider, Serializable {

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

    // ...
}

【讨论】:

  • 在我的集成测试中,我收到此错误消息。但是我的supports() method 返回true。(确实如此 - 我打印了)。还有什么可能导致此错误?
  • 当我不小心配置了两个 UserDetailsService bean 时出现此错误。
【解决方案2】:

我遇到了同样的问题。在我的情况下,解决方案是在身份验证通过后将 AbstractAuthenticationToken.setAuthenticated 设置为 true。

【讨论】:

    猜你喜欢
    • 2016-04-07
    • 2016-01-25
    • 2014-11-20
    • 2016-08-03
    • 2013-04-25
    • 2013-01-27
    • 2015-04-25
    • 2017-05-10
    • 2015-06-08
    相关资源
    最近更新 更多