在Spring Boot中忽略特定Urls的承载令牌验证

维维克

我正在将微服务配置为使用JWK端点来验证JWT令牌签名的资源服务器。

我已将配置设置为允许GET该服务中的所有请求。所有其他请求均根据范围和角色得到保护。这是我正在使用的配置。

@EnableReactiveMethodSecurity
class SecurityConfig : WebFluxConfigurer {

    @Bean
    fun authenticationEntryPoint(): ServerAuthenticationEntryPoint {
        return JwtBearerTokenServerAuthenticationEntryPoint()
    }

    @Bean
    fun accessDeniedHandler(): ServerAccessDeniedHandler {
        return JwtTokenAccessDeniedHandler()
    }

    @Bean
    fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http
            .authorizeExchange()
            .pathMatchers(HttpMethod.GET).permitAll()
            .pathMatchers("/docs/**", "/v2/api-docs/**", "/").permitAll()
            // Client should have the required scope to write to products
            .pathMatchers(HttpMethod.POST).hasAuthority(PRODUCT_WRITE_SCOPE)
            .pathMatchers(HttpMethod.PUT).hasAuthority(PRODUCT_WRITE_SCOPE)
            .pathMatchers(HttpMethod.DELETE).hasAuthority(PRODUCT_WRITE_SCOPE)
            // health and info urls will be open(permitted to all) others will be checked for authorization
            .matchers(EndpointRequest.to(HealthEndpoint::class.java, InfoEndpoint::class.java)).permitAll()
            .anyExchange().authenticated()
            .and()
            .csrf().disable()
            .formLogin().disable()
            .oauth2ResourceServer()
            .authenticationEntryPoint(authenticationEntryPoint())
            .accessDeniedHandler(accessDeniedHandler())
            .jwt()
            .jwtAuthenticationConverter {
                jwtAuthenticationConverter(it)
            }

        return http.build()
    }

    private fun jwtAuthenticationConverter(jwt: Jwt): Mono<AbstractAuthenticationToken>? {
        val jwtAuthConverter = ReactiveJwtAuthenticationConverter()
        jwtAuthConverter.setJwtGrantedAuthoritiesConverter {
            val jwtGrantedAuthoritiesConverter = JwtAuthoritiesConverter()
            val reactiveJwtGrantedAuthoritiesConverterAdapter =
                ReactiveJwtGrantedAuthoritiesConverterAdapter(jwtGrantedAuthoritiesConverter)
            reactiveJwtGrantedAuthoritiesConverterAdapter.convert(it)
        }
        return jwtAuthConverter.convert(jwt)
    }

    companion object {
        private const val PRODUCT_WRITE_SCOPE = "SCOPE_product:write"
    }

}

我面临的问题是,如果我在GET请求的授权标头中发送了过期的令牌,令牌验证仍然会发生,并且我会收到令牌过期的错误。

有没有办法以仅在某些端点进行令牌验证而在其他端点忽略令牌验证的方式更改配置?

维维克

这是我为解决问题所做的工作。您可以选择指定安全配置应应用到的路径。这是指定代码段的代码段。

.securityMatcher {
                ServerWebExchangeMatchers.matchers(
                    ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, "/**"),
                    ServerWebExchangeMatchers.pathMatchers(HttpMethod.PUT, "/**"),
                    ServerWebExchangeMatchers.pathMatchers(HttpMethod.DELETE, "/**")
                ).matches(it)
            }

这是完整的配置。

@EnableReactiveMethodSecurity
class SecurityConfig : WebFluxConfigurer {

    @Bean
    fun authenticationEntryPoint(): ServerAuthenticationEntryPoint {
        return JwtBearerTokenServerAuthenticationEntryPoint()
    }

    @Bean
    fun accessDeniedHandler(): ServerAccessDeniedHandler {
        return JwtTokenAccessDeniedHandler()
    }

    @Bean
    fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
        http
            .securityMatcher {
                ServerWebExchangeMatchers.matchers(
                    ServerWebExchangeMatchers.pathMatchers(HttpMethod.POST, "/**"),
                    ServerWebExchangeMatchers.pathMatchers(HttpMethod.PUT, "/**"),
                    ServerWebExchangeMatchers.pathMatchers(HttpMethod.DELETE, "/**")
                ).matches(it)
            }
            .authorizeExchange()
            .pathMatchers("/docs/**", "/v2/api-docs/**", "/").permitAll()
            // Client should have the required scope to write to products
            .pathMatchers(HttpMethod.POST).hasAuthority(PRODUCT_WRITE_SCOPE)
            .pathMatchers(HttpMethod.PUT).hasAuthority(PRODUCT_WRITE_SCOPE)
            .pathMatchers(HttpMethod.DELETE).hasAuthority(PRODUCT_WRITE_SCOPE)
            // health and info urls will be open(permitted to all) others will be checked for authorization
            .matchers(EndpointRequest.to(HealthEndpoint::class.java, InfoEndpoint::class.java)).permitAll()
            .anyExchange().authenticated()
            .and()
            .csrf().disable()
            .formLogin().disable()
            .oauth2ResourceServer()
            .authenticationEntryPoint(authenticationEntryPoint())
            .accessDeniedHandler(accessDeniedHandler())
            .jwt()
            .jwtAuthenticationConverter {
                jwtAuthenticationConverter(it)
            }

        return http.build()
    }

    private fun jwtAuthenticationConverter(jwt: Jwt): Mono<AbstractAuthenticationToken>? {
        val jwtAuthConverter = ReactiveJwtAuthenticationConverter()
        jwtAuthConverter.setJwtGrantedAuthoritiesConverter {
            val jwtGrantedAuthoritiesConverter = JwtAuthoritiesConverter()
            val reactiveJwtGrantedAuthoritiesConverterAdapter =
                ReactiveJwtGrantedAuthoritiesConverterAdapter(jwtGrantedAuthoritiesConverter)
            reactiveJwtGrantedAuthoritiesConverterAdapter.convert(it)
        }
        return jwtAuthConverter.convert(jwt)
    }

    companion object {
        private const val PRODUCT_WRITE_SCOPE = "SCOPE_product:write"
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Java Spring Boot中从请求标头获取承载令牌?

来自分类Dev

在 Spring-boot 中忽略来自特定请求的字段

来自分类Dev

React App + Spring Boot-在Chrome中未设置Cookie中的JWT身份验证令牌

来自分类Dev

Keycloak:Spring Boot 项目作为承载并重用用户的令牌

来自分类Dev

Spring Zuul代理不接受承载令牌

来自分类Dev

在Spring Boot2中验证Ouath2 JWT令牌

来自分类Dev

Spring MVC验证被忽略

来自分类Dev

忽略Spring Boot中某些端点的授权

来自分类Dev

Weblogic中忽略了Spring-boot @ImportResource

来自分类Dev

忽略在 spring boot 中本地执行的测试

来自分类Dev

Spring Boot 方法验证

来自分类Dev

如何在Spring Security中撤消身份验证令牌?

来自分类Dev

在Spring Security中接收令牌的基本身份验证

来自分类Dev

在Spring Security中接收令牌的基本身份验证

来自分类Dev

Spring Boot忽略ObjectMapper模块

来自分类Dev

Spring Boot忽略主类

来自分类常见问题

Spring MVC与Spring Boot与Spring

来自分类Dev

如何在Spring Boot项目中忽略特定URL的Spring Security CSRF

来自分类Dev

注解中的Spring Boot JSR303消息代码被忽略

来自分类Dev

忽略Spring Boot API Requestbody中的空字段

来自分类常见问题

Spring MVC或Spring Boot

来自分类常见问题

Spring Boot验证Long值

来自分类Dev

Spring Boot验证Long值

来自分类Dev

Spring Boot 1.3 + OAuth:身份验证请求失败:BadCredentialsException:无法获取访问令牌

来自分类Dev

如何在 Spring Boot 中使用预定义的令牌绕过 Oauth2 身份验证?

来自分类Dev

Spring Boot 2 + OAuth2:配置令牌的身份验证代码交换

来自分类Dev

Spring Boot使用Spring Profile忽略Java配置类中的bean

来自分类Dev

Spring Boot使用Spring Profile忽略Java配置类中的bean

来自分类Dev

Spring Boot中的JSP

Related 相关文章

  1. 1

    如何在Java Spring Boot中从请求标头获取承载令牌?

  2. 2

    在 Spring-boot 中忽略来自特定请求的字段

  3. 3

    React App + Spring Boot-在Chrome中未设置Cookie中的JWT身份验证令牌

  4. 4

    Keycloak:Spring Boot 项目作为承载并重用用户的令牌

  5. 5

    Spring Zuul代理不接受承载令牌

  6. 6

    在Spring Boot2中验证Ouath2 JWT令牌

  7. 7

    Spring MVC验证被忽略

  8. 8

    忽略Spring Boot中某些端点的授权

  9. 9

    Weblogic中忽略了Spring-boot @ImportResource

  10. 10

    忽略在 spring boot 中本地执行的测试

  11. 11

    Spring Boot 方法验证

  12. 12

    如何在Spring Security中撤消身份验证令牌?

  13. 13

    在Spring Security中接收令牌的基本身份验证

  14. 14

    在Spring Security中接收令牌的基本身份验证

  15. 15

    Spring Boot忽略ObjectMapper模块

  16. 16

    Spring Boot忽略主类

  17. 17

    Spring MVC与Spring Boot与Spring

  18. 18

    如何在Spring Boot项目中忽略特定URL的Spring Security CSRF

  19. 19

    注解中的Spring Boot JSR303消息代码被忽略

  20. 20

    忽略Spring Boot API Requestbody中的空字段

  21. 21

    Spring MVC或Spring Boot

  22. 22

    Spring Boot验证Long值

  23. 23

    Spring Boot验证Long值

  24. 24

    Spring Boot 1.3 + OAuth:身份验证请求失败:BadCredentialsException:无法获取访问令牌

  25. 25

    如何在 Spring Boot 中使用预定义的令牌绕过 Oauth2 身份验证?

  26. 26

    Spring Boot 2 + OAuth2:配置令牌的身份验证代码交换

  27. 27

    Spring Boot使用Spring Profile忽略Java配置类中的bean

  28. 28

    Spring Boot使用Spring Profile忽略Java配置类中的bean

  29. 29

    Spring Boot中的JSP

热门标签

归档