【发布时间】:2020-10-26 16:22:00
【问题描述】:
我们有一个登录页面,并在 SecurityConfig 中配置了 CustomAuthenticationProvider,在此类中将凭据存储在 UsernamePasswordAuthenticationToken 对象中。我们在 Rest Controller 中捕获用户凭据。我们已经在集群环境中部署了应用程序。
如果我们使用凭据登录,请求将发送到一个服务器,并且用户详细信息将进入 Rest Controller,如果我们刷新页面,则在同一个会话中,请求将发送到另一台服务器,因此我们得到空用户详细信息。
@GetMapping("/myRentals")
public ModelAndView myDetails(@AuthenticationPrincipal UserDetails user) {
logger.info("user:"+user);
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/")
.permitAll()
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl("/authenticateTheUser")
.permitAll()
.and()
.logout().permitAll()
.and()
.exceptionHandling().accessDeniedPage("/access-denied");
}
}
【问题讨论】:
标签: spring-mvc spring-security