Rob*_*erd 7 java spring spring-security
TL; DR
是否可以基于每个请求在Spring(安全性)中控制会话创建策略?
长版......
我一直在为我们的应用程序使用普通登录表单用户身份验证 一些控制器是@RestControllers,到目前为止,cookie跟踪的默认用户会话允许它正常工作.
(即当XHR请求来自页面时,请求将通过以前登录的用户进行身份验证,因为浏览器会像往常一样发送JSESSIONID cookie)
我现在想允许从休息客户端而不是浏览器调用一些@RestController端点,所以我创建了一个API令牌认证方案 - 这很好.
清理的最后一点是REST调用生成一个会话,如果可能的话我想避免.
我无法将会话策略设置为永远(因为我仍然依赖于我的网络用户的会话).
我试过IF_REQUIRED无济于事.
我查看了HttpSessionSecurityContextRepository,但它包装了请求,每当刷新响应时都会创建一个会话.
(参见下面的stacktrace)
是否可以在其他地方根据请求挂钩会话管理?
我可以根据Authentication对象的类类型轻松区分请求的类型.
at myapp.cfg.WebConfig$1.sessionCreated(WebConfig.java:74)
at io.undertow.servlet.core.ApplicationListeners.sessionCreated(ApplicationListeners.java:300)
at io.undertow.servlet.core.SessionListenerBridge.sessionCreated(SessionListenerBridge.java:56)
at io.undertow.server.session.SessionListeners.sessionCreated(SessionListeners.java:52)
at io.undertow.server.session.InMemorySessionManager.createSession(InMemorySessionManager.java:187)
at io.undertow.servlet.spec.ServletContextImpl.getSession(ServletContextImpl.java:741)
at io.undertow.servlet.spec.HttpServletRequestImpl.getSession(HttpServletRequestImpl.java:370)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:270)
at org.springframework.security.web.context.HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper.createNewSessionIfAllowed(HttpSessionSecurityContextRepository.java:427)
at org.springframework.security.web.context.HttpSessionSecurityContextRepository$SaveToSessionResponseWrapper.saveContext(HttpSessionSecurityContextRepository.java:364)
at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.onResponseCommitted(SaveContextOnUpdateOrErrorResponseWrapper.java:85)
at org.springframework.security.web.util.OnCommittedResponseWrapper.doOnResponseCommitted(OnCommittedResponseWrapper.java:245)
at org.springframework.security.web.util.OnCommittedResponseWrapper.access$000(OnCommittedResponseWrapper.java:33)
at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.flush(OnCommittedResponseWrapper.java:512)
at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.flush(OnCommittedResponseWrapper.java:513)
at com.fasterxml.jackson.core.json.UTF8JsonGenerator.flush(UTF8JsonGenerator.java:1050)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:953)
Run Code Online (Sandbox Code Playgroud)
将您的安全配置拆分为表单登录(基于会话的 API 访问)和无状态 API 令牌身份验证方案的单独部分。
例子:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Order(1)
@Configuration
class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**")
.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic().realmName("API") // your API token authentication scheme
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and()
.exceptionHandling().authenticationEntryPoint(new Http401AuthenticationEntryPoint("Form realm=\"API\"")); // prevent basic authentication popup in browser
}
}
@Order(2)
@Configuration
class DefaultSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.logout().logoutSuccessUrl("/login").permitAll();
}
}
}
Run Code Online (Sandbox Code Playgroud)
替换.httpBasic().realmName("API")为您自己的身份验证方案。
使用 例如 调用您的 API并验证响应中curl -v ...没有标头。Set-Cookie否则,您的代码会自行创建一个 http 会话。
| 归档时间: |
|
| 查看次数: |
2094 次 |
| 最近记录: |