繁体   English   中英

将Spring Security 3.0升级到Spring Security 3.1时,Spring Security无法正常工作

[英]Spring security not working while upgrading spring security 3.0 to spring security 3.1

我已经成功地将Spring Security 3.0集成到Web应用程序中,并且运行良好,现在我要将Spring Security 3.0升级到3.1,并且在我的CustomAuthenticationManager中遇到问题每当我尝试登录时,CustomAuthenticationManager都会被调用两次。 因此,第一次用户成功进行身份验证并返回usernamePasswordAuthenticationToken,但是再次调用该类,并且此主体返回适当的值,但是凭据返回null,因此用户获得身份验证失败并再次重定向到登录页面,这就是为什么我无法登录。

CustomAuthenticationManger:

public class CustomAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private ILoginService loginService;
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        UsernamePasswordAuthenticationToken usernamePassswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
                authentication.getPrincipal(), authentication.getCredentials());
        if (loginService.authenticateUser((String) authentication.getPrincipal())) {
            if (loginService.validateUserIdAndPass((String) authentication.getPrincipal(), (String) authentication.getCredentials())) {
                usernamePassswordAuthenticationToken.setAuthenticated(false);
            } else
                throw new BadCredentialsException(
                        "Username/Password does not match");
        } else
            throw new BadCredentialsException(
                    "Username/Password does not match");
        return usernamePassswordAuthenticationToken;
    }
    public boolean supports(Class<? extends Object> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}

我的ApplicationContextSecurity.xml:

<global-method-security pre-post-annotations="enabled">     
    </global-method-security>  
    <beans:bean id="myAccessDecisionManager"
        class="com.app.common.security.repository.MyAccessDecisionManager"> 
    </beans:bean> 

    <http auto-config="true" once-per-request="true"
        access-decision-manager-ref="myAccessDecisionManager" access-denied-page="/jsp/errorPage.jsp">

        <intercept-url pattern="/*.app"  access="ROLE_ANONYMOUS"/>  

         <form-login login-page="/login.app" login-processing-url="/j_spring_security_check"
            default-target-url="/login/validate.app"
            authentication-failure-url="/login.app?login_error=1" />
        <logout logout-url="/j_spring_security_logout"
            logout-success-url="/login.app" invalidate-session="true" /> 
        <session-management invalid-session-url="/login.app"
            session-fixation-protection="newSession">
            <concurrency-control max-sessions="100"
                error-if-maximum-exceeded="false" />
        </session-management>
    </http>

    <authentication-manager>
        <authentication-provider ref="customAuthenticationProvider"></authentication-provider>
    </authentication-manager>

    <beans:bean id="customAuthenticationProvider"
        class="com.app.common.security.repository.CustomAuthenticationProvider">        
    </beans:bean>

请告诉我我错了。

从Spring 3.0.3(及更高版本)开始,使用默认的AuthenticationManagerProviderManager在进行AuthenticationManager尝试后将清除凭据( SEC-1493 )。 您必须是较旧的版本(Spring Security的3.0.3之前的版本)。

您的CustomAuthenticationProvider也存在缺陷,因为您应在成功进行身份验证后将身份验证设置为true (扩展AbstractSecurityInterceptor其他拦截器将重新尝试身份验证。

链接

  1. SEC-1493
  2. ProviderManager
  3. AbstractSecurityInterceptor javadoc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM