【问题标题】:Spring boot security - allowing user requests with expired JWT tokenSpring Boot 安全性 - 允许使用过期 JWT 令牌的用户请求
【发布时间】:2021-08-27 16:06:33
【问题描述】:

我有一些用户正在使用有效的 JWT 令牌访问某些 API,但由于他们的任务所花费的时间比令牌过期时间长得多,当他们再次访问 API 时,JWT 令牌已经过期。他们不应该刷新他们的令牌,即使令牌过期,他们也必须使用相同的令牌并访问某些 API。

我正在使用spring.security.oauth2.resourceserver 向我们的授权服务器进行身份验证。

所以,我想要实现的是,如果令牌来源正确且格式良好,即使令牌已过期,我也需要接受请求。

是的,我知道这似乎不是一个好方法,但如果可能,请教育我。

我已经从here 中读到了ClientHttpRequestInterceptor,并深入研究了resource server 的源代码,但找不到合适的方法。

【问题讨论】:

    标签: spring-boot spring-security jwt spring-security-oauth2 spring-security-rest


    【解决方案1】:

    我完全建议您不要这样做,因为如果某个恶意用户从某人那里窃取了 JWT,他将能够永远请求受保护的资源,因为令牌不会过期。在这种情况下您应该做的是刷新令牌,请前往docs for refreshing the access token

    但出于知识目的,可以提供自定义 JwtDecoder,它可以按照您想要的方式验证 JWT。 您应该使用您的特定配置公开JwtDecoder 类型的@Bean,如下所示:

    @Bean
    public JwtDecoder jwtDecoder() {
        OAuth2TokenValidator<Jwt> myCustomJwtValidator = new MyImplementationOfOAuth2TokenValidator();
        NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withPublicKey(myPublicKey).build();
        jwtDecoder.setJwtValidator(myCustomJwtValidator);
        return jwtDecoder;
    }
    

    您可以在documentation 上找到更多详细信息。

    【讨论】:

    • 感谢您的建议。正如我所指出的,我只需要一些 API。这就是为什么我需要在那个时候获得一个请求并继续执行它。例如,类似的东西; .jwt().decoder(jwtDecoder()).and().authenticationEntryPoint(new AuthenticationEntryPoint() { @Override public void begin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) 抛出 IOException, ServletException { log.info("sadasdad" ); } });我在日志点需要的是 res.proceed
    猜你喜欢
    • 2020-07-18
    • 2017-12-27
    • 2020-09-25
    • 2021-03-30
    • 2015-02-20
    • 2020-08-20
    • 2021-06-17
    • 2019-02-05
    • 2020-05-09
    相关资源
    最近更新 更多