【问题标题】:Spring Security 2 Custom Authentication Provider not saving security contextSpring Security 2 自定义身份验证提供程序不保存安全上下文
【发布时间】:2013-05-03 01:35:03
【问题描述】:

我已将默认身份验证提供程序更改为自定义。

这是我的 AuthenticationProvider

public class CustomAuthenticationProvider implements AuthenticationProvider {

@Autowired
private ParamsProperties paramsProperties;  

@SuppressWarnings("unchecked")
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    //Check username and passwd
    String user = (String) authentication.getPrincipal();
    String pass = (String) authentication.getCredentials();
    if(StringUtils.isBlank(user) || StringUtils.isBlank(pass) ){
        throw new BadCredentialsException("Incorrect username/password");
    }

    //Create SSO
    SingleSignOnService service = new SingleSignOnService(paramsProperties.getServicesServer());
    try {
        //Check logged
        service.setUsername(authentication.getName());
        service.setPassword(authentication.getCredentials().toString());
        ClientResponse response = service.call();
        String result = response.getEntity(String.class);

        ObjectMapper mapper = new ObjectMapper();
        Map<String,Object> map = mapper.readValue(result, new TypeReference<Map<String,Object>>() {} );
        //Read code
        String code = (String)map.get("code");
        log.debug(" ** [Authenticate] Result: " + code );
        for (String s : (List<String>)map.get( "messages" ) ) {
            log.debug(" [Authenticate] Message: " + s );
        }

        if ( code.equals( "SESSION_CREATED" ) || code.equals( "SESSION_UPDATED" ) || code.equals( "SESSION_VERIFIED" ) ) {              
            UsernamePasswordAuthenticationToken tokenSSO = LoginHelper.getuserSringTokenFromAuthService(map);            
            return tokenSSO;                
        } else {
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new AuthenticationServiceException( e.getMessage() );
    }
}


public boolean supports(Class authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}

这是我的 security.xml

<http>
  <form-login default-target-url ="/Login.html" always-use-default-target="true" login-page="/Login.html" login-processing-url="/j_spring_security_check"
        authentication-failure-url="/Login.html" />  
  <http-basic />
  <logout logout-success-url="/Login.html" />
</http>

<beans:bean id="localeFilter" class="com.mycomp.comunes.server.spring.controller.login.MyLocaleFilter" lazy-init="true">
    <custom-filter position="LAST"/>
</beans:bean>  

<beans:bean id="authenticationProvider" class="com.indra.rfef.comunes.server.spring.manager.autenticacion.CustomAuthenticationProvider">
  <custom-authentication-provider />
</beans:bean>

它会通过我的 CustomAuthenticationProvider,并正确验证用户。但是,当返回 UsernamePasswordAuthenticationToken 类型的 tokenSSO 时,它似乎没有将用户保存在安全上下文中,并且当我将用户(在 authenticate 的回调中)重定向到 index.html 时,我被重定向了返回 Login.html。

为什么会发生这种情况?我是不是忘记了什么?

【问题讨论】:

  • 你看到下面的答案了吗?有帮助吗?

标签: java spring tomcat spring-security


【解决方案1】:

请修正您的配置:

<http>
    <intercept-url pattern="/Login*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>        
    <intercept-url pattern="/**" access="ROLE_USER"/>
    <form-login login-page="/Login.html" login-processing-url="/j_spring_security_check" authentication-failure-url="/Login.html" />  
    <http-basic />
    <logout logout-success-url="/Login.html" />
</http>
  1. 删除default-target-url ="/Login.html"。它在登录到同一登录页面后进行重定向。默认为/
  2. 为所有 URL 添加安全性&lt;intercept-url pattern="/**" access="ROLE_USER"/&gt;
  3. 不要从登录页面删除匿名访问
  4. 为什么需要 BasicAuthentication?如果不需要,请将其删除:&lt;http-basic /&gt;

【讨论】:

  • 谢谢,但顺便说一句,这并不是真正的 CustomAuthenticationProvider 问题。我正在处理我的 SpringGwtCoreServlet 中的一个问题,因为用户不在 SpringSecurityContext 中。我不得不添加这个并且工作! SecurityContextHolder.getContext().setAuthentication(tokenSSO); SecurityContext auth = new SecurityContextImpl(); auth.setAuthentication(tokenSSO); SecurityContextHolder.setContext(auth);
猜你喜欢
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2013-05-02
  • 2016-07-19
  • 2016-09-24
  • 2011-01-03
  • 2016-01-06
  • 2011-02-25
相关资源
最近更新 更多