当前位置: 首页 > 知识库问答 >
问题:

Spring Cloud Gateway Oauth2Login在成功登录时返回JWT令牌而不是会话Cookie

缪兴腾
2023-03-14

如果之前有人问过这个问题,我很抱歉,但我一直无法找到答案。

我试图设置Spring Cloud Gateway作为OAuth2客户机,通过Keycloak身份验证服务器对用户进行身份验证/登录。我可以使用下面的代码片段来实现这一点:

安全配置

@Configuration
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
public class SecurityConfig {
    
    private final GatewayAuthenticationSuccessHandler gatewayAuthenticationSuccessHandler;
    
    public SecurityConfig(GatewayAuthenticationSuccessHandler gatewayAuthenticationSuccessHandler) {
        this.gatewayAuthenticationSuccessHandler = gatewayAuthenticationSuccessHandler;
    }
    
    @Bean
    public SecurityWebFilterChain securityWebFilterChain(
            ServerHttpSecurity http, 
            ReactiveClientRegistrationRepository clientRegistrationRepository) {
        
        http
            .authorizeExchange()
            .pathMatchers("/ui/**").permitAll()
            .anyExchange().authenticated()
                .and()
            .oauth2Login().authenticationSuccessHandler(gatewayAuthenticationSuccessHandler)
                .and()
            .oauth2ResourceServer().jwt();
          
        http.logout(
                logout ->
                    logout.logoutSuccessHandler(
                        new OidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository)));
        http.logout().logoutUrl("/logout");
              
        http.csrf().disable();
        http.httpBasic().disable();
        http.formLogin().disable();

        return http.build();
    }
    
}
@Component
public class GatewayAuthenticationSuccessHandler implements ServerAuthenticationSuccessHandler {
    
    private ServerRedirectStrategy redirectStrategy = new DefaultServerRedirectStrategy();
    
    @Value("${my.frontend_url}")
    private String DEFAULT_LOGIN_SUCCESS_URL;
      
    @Override
    public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
        URI url = URI.create(DEFAULT_LOGIN_SUCCESS_URL); 
        return this.redirectStrategy.sendRedirect(webFilterExchange.getExchange(), url);
    }

}

我使用的是spring boot版本2.2.8和spring cloud版本hoxton.sr6

共有1个答案

徐洋
2023-03-14

不确定这是否有帮助,但尝试将SessionPolicy作为无状态添加到webfilter链中,如下所示,它应该会起作用。

http.SessionManagement().SessionCreationPolicy(SessionCreationPolicy.Stateless)

另外,如果将配置类扩展为WebSecurityConfigurerAdapter,可以尝试使用NullAuthenticatedSessionStrategy重写sessionAuthenticationStrategy。

override fun sessionAuthenticationStrategy(): SessionAuthenticationStrategy {
    return NullAuthenticatedSessionStrategy()
}
 类似资料:
  • 我有一台远程计算机,正在尝试使用以下命令登录postgres数据库(通过

  • 返回三个字段作为颁发的临时安全凭据。 AWS访问密钥ID 前两个字段的格式与用户的访问密钥相同,但第三个字段AWS_SESSION_TOKEN是临时凭证的专用字段。 我有两个问题: 如果AWS_会话_令牌表示/编码临时有效性,为什么我们仍然需要前两个字段(因为过期后,我们仍然需要另一个AWS_会话_令牌)?

  • 我尝试用angular2前端实现jwt令牌。当我尝试使用Postman接收带有post方法的令牌时,我接收到授权令牌,但在Angular中这样做返回空响应对象。这里是我使用的Angular服务的代码片段。 问题是,当我尝试记录时,令牌是空的,与响应相同。对于代码的后端部分,我遵循了jwt令牌的这个实现。

  • 嗨,我正在创建使用 REST API endpoint与服务器端通信的移动本机应用程序。 我以前有开发本机客户端的经验,但我将简单的令牌(随机生成的字符串)存储在存储用户信息的同一表中的数据库中。所以它就像浏览器中使用的会话,但每个请求的标头中都有令牌,而不是 cookie。 最近我发现了JWT令牌。这似乎是保护私有endpoint的好方法。您可以从移动客户端请求令牌,前提是您通过登录并获得生成的

  • 所以我的问题是:什么是“客户端登录超时”,什么是一个好的默认设置?对我来说,一个完美的答案是在工作流失败时从用户的角度描述工作流(比如用户在点击电子邮件验证链接之前喝了一分钟咖啡)和/或进一步阅读的链接

  • 我有一个关于JWT的SSO流的简单问题 假设我们有单独的授权服务器,它为客户端应用程序/服务器和资源服务器提供JWT,客户端尝试使用该令牌访问该服务器。 问题是,资源服务器应该自己验证令牌(例如,与Auth server共享私有证书),还是应该请求Auth server为每个客户端请求验证JWT?