我们有一个tomcat应用程序,可以在IE7 / 8和Firefox中正常运行。我们遇到问题的唯一浏览器(我们现在关心的是)谷歌浏览器(47以上版本)。
用户可以正常导航到应用程序并登录并在Firefox和IE中执行他们需要做的任何事情。但是,在尝试使用Chrome登录时,会话显然在登录后立即丢失,并且当经过身份验证的用户尝试导航到另一个页面时,他们会回到登录页面。这种情况一直发生。
我查看了在cookie中设置的JSESSIONID,它在每次请求时都会发送回mozila和IE,而不适用于Chrome(47以上版本)。
我们在登录后设置的清除Spring SecurityContextHolder 。并在每个请求中获取新的SecurityContextHolder。 在这里,我正在考虑我的代码,我是Spring安全配置。
欢迎任何想法!
我们正在使用tomcat 8.0.33和spring boot 4.2.4.RELEASE
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/**","/restServiceFromKwh360/**","/cloudSSO/ssoLogin","/cloudSSO/ssoLogout","/cloudSSO/ssoCallback",
"/cloudSSO/ssoLoginError","/cloudSSO/ssoReturnFromKwh360Services").permitAll()
.antMatchers("/energyAudit/**").access("hasRole('ROLE_CUSTOMER_ADMIN')")
.and().formLogin().loginPage("/cloudSSO/ssoLogin").permitAll()
.and().exceptionHandling().accessDeniedPage("/cloudSSO/ssoLoginError")
.and()
.csrf().disable();
}
}
在这里我手动设置身份验证对象
@Override
public void AutoLoginUser(String username, HttpServletRequest request) {
try{
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(authentication.getPrincipal() == "anonymousUser"){
User user;
try {
user = userDao.getUserByEmail(username);
} catch (KWH360DAOException e) {
e.printStackTrace();
logger.error("Request user is not registred with the system >>" + username);
throw new UsernameNotFoundException("You are not registered");
}
if (user == null){
logger.error("Request user is not registred with the system >>" + username);
throw new UsernameNotFoundException("You are not registered");
}
List<GrantedAuthority> authorities = buildUserAuthority(user.getRole().getName());
authentication = new UsernamePasswordAuthenticationToken(user,user.getPassword().toString(), authorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
SecurityContextHolder.getContext());
}
}catch(Exception e){
e.printStackTrace();
}
}