jys*_*hin 27 java spring spring-security
我正在使用spring boot开发Restful API服务器.我将项目配置为使用基本身份验证,如下所示.
@ComponentScan
@EnableAutoConfiguration
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER).and()
.csrf().disable()
.authorizeRequests().anyRequest().hasRole("USER").and()
.httpBasic();
}
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我通过Chrome-Postman-Plugin测试API时,在第一次调用之后,服务器永远不需要用户凭据.我注意到'JSESSIONID'cookie已创建.
我的项目中没有其他安全配置.我想知道为什么会这样......
Luk*_*ada 42
你尝试过使用过吗SessionCreationPolicy.STATELESS?之间存在细微的差别STATELESS,并NEVER在春季文档:
STATELESS:Spring Security永远不会创建它HttpSession,它永远不会用它来获取SecurityContext.
NEVER:Spring Security永远不会创建HttpSession,但如果它已经存在,将使用HttpSession.
所以我建议你清除所有的cookie,切换到STATELESS并再试一次.可能是你已经HttpSession转换到了NEVER.
小智 6
它对我有用“所以我建议你清除所有 cookie,将其切换到 STATELESS,然后重试。当你切换到 NEVER 时,可能已经有一个 HttpSession。”
@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
.csrf().disable()
.authorizeRequests()
.anyRequest()
.authenticated().and().httpBasic();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11160 次 |
| 最近记录: |