我希望create-session="stateless"在我的webapp中实现无状态弹簧安全性的最终目的是改变它,但事实并非如此.
随着这种变化,Spring安全性似乎无法正常工作,因为(我的假设)spring安全性不会在会话中存储任何内容,也无法对安全的Web请求进行身份验证.
我如何利用这种无状态功能?
我似乎还没有找到关于如何为无状态webapp实现无状态弹簧安全性的任何相关示例.
谢谢 !
我使用以下配置对我的Web应用程序进行了有效的JWT身份验证:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.authenticationEntryPoint(
(req, rsp, e) -> p.sendError(HttpServletResponse.SC_UNAUTHORIZED))
.and()
.addFilter(new UsernamePasswordAuthenticationFilter(authenticationManager(),
jwtConfig))
.addFilterAfter(new JwtTokenAuthenticationFilter(jwtConfig),
UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers(HttpMethod.POST, jwtConfig.getUri()).permitAll()
.anyRequest().authenticated();
}
Run Code Online (Sandbox Code Playgroud)
因为SessionCreationPolicy.STATELESS我期待Spring不会创建会话本身.但是,如果我访问任何其他资源/login,我仍然会在响应标头中看到以下条目:
set-cookie: JSESSIONID=...; Path=/; HttpOnly
Run Code Online (Sandbox Code Playgroud)
有人可以解释它来自哪里(也许不是来自Spring),如果它仍然来自Spring,那么需要改变什么?
编辑:
在我的控制器中进行测试,会话仍按照上述令牌所示进行注入.我仍然不知道这是从哪里来的.
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void create(HttpSession session) {
if (session != null) {
System.out.println("Session is existing"); // executes
}
}
Run Code Online (Sandbox Code Playgroud)