【发布时间】:2017-07-19 04:03:12
【问题描述】:
我正在尝试创建无状态安全性,从而将 JWT 令牌存储在 Cookie 而不是 SESSION 中。
问题是没有会话,SavedRequestAwareAuthenticationSuccessHandler 不知道原始请求(在身份验证页面弹出之前)。所以77 heresavedRequest 为空。
这看起来很奇怪,我想我做错了什么。如何允许页面重定向到登录无状态会话后请求的原始 URL?
-
我禁用会话
@Override protected void configure(HttpSecurity http) throws Exception { http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) ....formLogin().loginPage("/login").permitAll().successHandler(authenticationSuccessHandler) } -
然后我创建了一个自定义的 AuthenticationSuccessHandler,它扩展了 SavedRequestAwareAuthenticationSuccessHandler。我将其注册为successHandler(上图)。
@Component public class JwtCookieAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { Cookie cookie = ... CREATE A COOKIE WITH A JWT response.addCookie(cookie); super.onAuthenticationSuccess(request, response, authentication); } }
编辑: 这是我的依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
</dependencies>
【问题讨论】:
-
您好,您找到解决方案了吗?同样的问题:/
标签: spring-security