我在 Spring 4 上有一个应用程序,具有用于身份验证的 Spring Security,以及用于在集群环境中共享会话的 Spring session。
我从 Spring Session 实现 sessionRepository 以将会话存储在数据库中,因此当我进入站点 spring 会话时,创建一个名为“SESSION”的 cookie 并将其存储在数据库中。
这个 session-DB 实现的想法在这里:
如何在 Spring 4 中进行基于关系数据库的 HTTP 会话持久性?
此时我有一个cookie“SESSION”。当我登录网站时,spring security 会创建另一个 cookie “JSESSION”,但这并未存储在数据库中,并且该 cookie 具有“身份验证信息”。
我的问题是:这个实现对于集群环境是否正确?还是我需要再做一次修改?
提前致谢。
编辑2:
我最近测试了我的应用程序,我对我的解释犯了一个错误,当我进入该站点时,即使我登录“SESSION”cookie 仍然有一个 cookie“SESSION”,但是如果我清理了另一个 cookie,则没有另一个 cookie session 表并刷新用户注销的站点。这是正确的行为吗?
编辑:
这是我来自 SecurityConfig 的“配置”(从 WebSecurityConfigurerAdapter 扩展)。
@Override
protected void configure(final HttpSecurity http) throws Exception {
// @formatter:off
http
//.csrf().disable()
.authorizeRequests()
.antMatchers(
"/login*",
"/logout*",
"/forgotPassword*",
"/user/initResetPassword*",
"/user/resetPassword*",
"/admin/saveConfiguration",
"/resources/**"
).permitAll()
.antMatchers("/invalidSession*").anonymous()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login.html")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/homepage.html")
.failureUrl("/login.html?error=true")
.successHandler(myAuthenticationSuccessHandler)
.usernameParameter("username")
.passwordParameter("password")
.permitAll()
.and()
.addFilterBefore(this.sessionSessionRepositoryFilter, ChannelProcessingFilter.class)
.sessionManagement()
.invalidSessionUrl("/login.html")
.sessionFixation()
.migrateSession()
.and()
.logout()
.invalidateHttpSession(false)
.logoutUrl("/vu_logout")
.logoutSuccessUrl("/logout.html?ls=true")
.deleteCookies("JSESSION")
.logoutSuccessHandler(mySimpleUrlLogoutSuccessHandler)
.permitAll();
// @formatter:on
}
这是我的登录成功处理程序:
@Component("myAuthenticationSuccessHandler")
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
handle(request, response, authentication);
HttpSession session = request.getSession(false);
if (session != null) {
session.setMaxInactiveInterval(60 * 10);
}
clearAuthenticationAttributes(request);
}
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
String targetUrl = determineTargetUrl(authentication);
if (response.isCommitted()) {
return;
}
redirectStrategy.sendRedirect(request, response, targetUrl);
}
protected String determineTargetUrl(Authentication authentication) {
boolean isUser = false;
boolean isAdmin = false;
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority grantedAuthority : authorities) {
if (grantedAuthority.getAuthority().equals("OPER") || grantedAuthority.getAuthority().equals("AUDITOR")) {
isUser = true;
} else if (grantedAuthority.getAuthority().equals("ADMIN")) {
isAdmin = true;
isUser = false;
break;
}
}
if(isUser || isAdmin)
{
return "/home.html";
}
else
{
throw new IllegalStateException();
}
}
protected void clearAuthenticationAttributes(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session == null) {
return;
}
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
this.redirectStrategy = redirectStrategy;
}
protected RedirectStrategy getRedirectStrategy() {
return redirectStrategy;
}
}