如何在spring security中绕过登录表单页面?

时间:2015-09-20 19:53:54

标签: spring spring-security

我正在使用自定义UsernamePasswordAuthenticationFilter(在登录页面之前调用)。我正在考虑此过滤器中已登录用户的会话ID。如果对应的会话ID存在auth_token,我想绕过登录页面。

我该怎么做?。

1 个答案:

答案 0 :(得分:0)

一旦检查了auth_token,就必须使用经过身份验证的Authencation填充安全上下文。类似的东西(在您的自定义过滤器中):

... // first check the existence of the auth_token and extract some information from it like user name and roles
String login = ...
String role = ...
PreAuthenticatedAuthenticationToken preAuthenticatedAuthenticationToken
                    = new PreAuthenticatedAuthenticationToken(login, auth_token, Collections.singleton(new SimpleGrantedAuthority(role)));
SecurityContextHolder.getContext().setAuthentication(preAuthenticatedAuthenticationToken);
//At this point : the security context contains an authenticated Authentication and other security filters won't have any impact anymore

我不是说这是满足您需求的最佳方法,但它可以使用或多或少的标准弹簧安全配置。