我嘗試配置 Spring Security,但遇到一個(gè)問題。這是我的 SessionAuthenticationFilter:public class SessionAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpSession session = request.getSession(); User user = (User) session.getAttribute("user"); if (nonNull(user)) { SimpleGrantedAuthority authority = new SimpleGrantedAuthority(user.getRole()); Authentication authentication = new UsernamePasswordAuthenticationToken(user.getName(), null, singletonList(authority)); SecurityContextHolder.getContext().setAuthentication(authentication); } filterChain.doFilter(request, response); }}這是我的安全配置:@Configuration@EnableWebSecurity@EnableJdbcHttpSession@EnableGlobalMethodSecurity(prePostEnabled = true)public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public SessionAuthenticationFilter sessionFilter() { return new SessionAuthenticationFilter(); } @Bean public HttpSessionIdResolver httpSessionIdResolver() { return HeaderHttpSessionIdResolver.xAuthToken(); } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .formLogin().disable() .cors() .and() .httpBasic() .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .addFilterBefore(sessionFilter(), SessionManagementFilter.class) .authorizeRequests() .antMatchers( "/login" }}在 SessionAuthenticationFilter 內(nèi)部 HttpSession 是正確的,但是當(dāng)我嘗試獲取此會(huì)話時(shí),我將獲取其他會(huì)話。為什么?我了解這是創(chuàng)建 Spring Security 的。它是如何固定的?
1 回答

qq_遁去的一_1
TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
您的問題可能與此有關(guān):.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
根據(jù) Spring Security Docs,Spring Security 永遠(yuǎn)不會(huì)創(chuàng)建一個(gè)HttpSession
,也永遠(yuǎn)不會(huì)使用它來獲取SecurityContext
會(huì)話創(chuàng)建策略設(shè)置為STATELESS
.
嘗試將政策更改為SessionCreationPolicy.ALWAYS
添加回答
舉報(bào)
0/150
提交
取消