首页 > 解决方案 > 资源服务器如何检查我使用 Spring Security 在另一台服务器中创建的 JWT 令牌?

问题描述

有一个模块启用了弹簧安全性。在此模块中,此配置允许我执行单点登录并将登录的用户保存在内存中。在 customSuccessHandler 中,我更改了令牌的值并将其保存(覆盖 sso 返回的令牌)。然后我将新令牌发送到 Angular 页面。

@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Autowired
    private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;

    
    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {


        
        httpSecurity.csrf().and().cors().disable().authorizeRequests()
        .anyRequest().authenticated()
        .and()
        .oauth2Login()
        .redirectionEndpoint()
        .baseUri("/baseUri")
        .and()
        .authorizationEndpoint()
        .baseUri("/oauth2/authorize")  
        .authorizationRequestRepository(customAuthorizationRequestRepository())
        .and()
        .successHandler(customAuthenticationSuccessHandler);
    
    }
    
    @Bean
    public AuthorizationRequestRepository<OAuth2AuthorizationRequest> customAuthorizationRequestRepository() {
        return new HttpSessionOAuth2AuthorizationRequestRepository();
    }

}

现在有另一个模块包含一些我想要保护的 API。我想发送前一个令牌(来自角度)并验证它发送到存储用户信息(访问令牌、主体等)的模块。

实际上,如果我不更改令牌(所以我使用由 sso 返回的令牌),我可以自动验证它设置属性:

spring.security.oauth2.resourceserver.jwt.jwk-set-uri= https://example.provider.com/oauth2/key/jwks

我可以用我的服务器复制类似的东西吗?

标签: javaspringauthenticationspring-securityjwt

解决方案


推荐阅读