【问题标题】:Redirecting to original URL for stateless session重定向到无状态会话的原始 URL
【发布时间】:2017-07-19 04:03:12
【问题描述】:

我正在尝试创建无状态安全性,从而将 JWT 令牌存储在 Cookie 而不是 SESSION 中。

问题是没有会话,SavedRequestAwareAuthenticationSuccessHandler 不知道原始请求(在身份验证页面弹出之前)。所以77 heresavedRequest 为空。

这看起来很奇怪,我想我做错了什么。如何允许页面重定向到登录无状态会话后请求的原始 URL?

  1. 我禁用会话

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    
           ....formLogin().loginPage("/login").permitAll().successHandler(authenticationSuccessHandler)
    
        }
    
  2. 然后我创建了一个自定义的 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


【解决方案1】:

在您的配置中,添加:

.requestCache().requestCache(new CookieRequestCache())

完整示例:

   protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().csrfTokenRepository(new CookieCsrfTokenRepository())
                .and()
                .authorizeRequests()
                .antMatchers("/about").permitAll()
                .antMatchers("/accounts").hasRole("ADMINISTRATOR")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
                .requestCache().requestCache(new CookieRequestCache())
                .and()
                .logout()
                .permitAll()
                .and()
                .sessionManagement()
                .maximumSessions(1)
                .sessionRegistry(sessionRegistry())
                .and()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .securityContext()
                .securityContextRepository(securityContextRepository);

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 2021-04-19
    • 1970-01-01
    • 2019-12-11
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    相关资源
    最近更新 更多