【发布时间】:2014-12-10 19:02:03
【问题描述】:
我正在使用 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();
}
...
}
但是当我通过 Chrome-Postman-Plugin 测试 API 时,在第一次调用后,服务器从不需要用户凭据。我注意到创建了“JSESSIONID”cookie。
我的项目中没有其他安全配置。我想知道为什么会这样……
【问题讨论】:
-
您的配置类是否扩展了 WebSecurityConfigurerAdapter?
-
是的。我编辑了我的代码 sn-p。我想确保我的 api 服务器不管理任何会话..
-
好的,首先,让我们验证是否强制执行了身份验证。你可以尝试用这个替换你的配置:http.csrf() .disable() .authorizeRequests() .anyRequest().authenticated()
标签: java spring spring-security