【问题标题】:Spring 5 Oauth2 - How to provide the check token URL in my Resource server?Spring 5 Oauth2 - 如何在我的资源服务器中提供检查令牌 URL?
【发布时间】:2021-01-07 23:17:55
【问题描述】:

我需要一些帮助..

我使用 Spring-security-oauth2 中的 @EnableAuthorizationServer 为授权类型“client_credentials”设置了一个 AuthorizationServer。能够创建、检查代币和一切有用的东西。

/oauth/令牌
/oauth/checkToken

已关注this sample for Authorization server

我有一个单独的项目,其中包含要保护的 REST API。我不能使用@EnableResourceServer,因为该项目使用 Spring 5.2.8 并且 spring-security-oauth2 2.5 在通过 Weblogic 部署时会导致冲突(因为它使用 4.x Spring jar 并且排除它们会导致更多问题),所以I am using this sample.

现在在这个示例中,我如何只提供一个 Checktoken url。此示例需要 JWT json 类型的文件,但我没有。我只是想保持简单,并使用我创建的授权服务器的校验令牌 url,类似于 @EnableResourceServer 的工作方式。(like provided here 除了没有@EnableResourceServer

我在哪里提供?任何即时帮助表示赞赏。

【问题讨论】:

    标签: java spring spring-security oauth-2.0 spring-security-oauth2


    【解决方案1】:

    我最终使用了 Spring 提供的 JWT 示例,它获取 JWT 公钥以在资源服务器上进行验证。 遵循Spring源码示例项目中提供的auth和resource server。

    在我们迁移到更好的 IDM 解决方案之前工作良好

    【讨论】:

      【解决方案2】:

      按照您的 ResourceServer 示例,这对我有用:

      @EnableWebSecurity
      public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
      
          @Value("${security.oauth2.resource.tokenInfoUri}") String tokenInfoUri;
          @Value("${security.oauth2.client.clientId}") String clientId;
          @Value("${security.oauth2.client.clientSecret}") String clientSecret;
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              // @formatter:off
              http
                      .authorizeRequests((authorizeRequests) ->
                              authorizeRequests
                                      .antMatchers(HttpMethod.GET, "/message/**").hasAuthority("SCOPE_message:read")
                                      .antMatchers(HttpMethod.POST, "/message/**").hasAuthority("SCOPE_message:write")
                                      .anyRequest().authenticated()
                      )
                      .oauth2ResourceServer(OAuth2ResourceServerConfigurer::opaqueToken);
              // @formatter:on
          }
      
          @Bean
          OpaqueTokenIntrospector opaqueTokenIntrospector() {
              return new NimbusOpaqueTokenIntrospector(tokenInfoUri,clientId,clientSecret);
          }
      }
      

      我使用了以下 spring 安全依赖项:

              <dependency>
                  <groupId>org.springframework.security</groupId>
                  <artifactId>spring-security-config</artifactId>
                  <version>5.3.4.RELEASE</version>
              </dependency>
              <dependency>
                  <groupId>org.springframework.security</groupId>
                  <artifactId>spring-security-oauth2-jose</artifactId>
                  <version>5.3.4.RELEASE</version>
              </dependency>
              <dependency>
                  <groupId>com.nimbusds</groupId>
                  <artifactId>oauth2-oidc-sdk</artifactId>
                  <version>8.22</version>
                  <scope>runtime</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.security</groupId>
                  <artifactId>spring-security-oauth2-resource-server</artifactId>
                  <version>5.3.4.RELEASE</version>
              </dependency>
      

      将您的 checkToken-Uri、client 和 clientSecret 放入您的 application.properties。

      【讨论】:

      • 谢谢@Jan Seidel,但我不希望在我的资源服务器中提供客户端 ID 和密码,呼叫我的客户端已经使用他们的客户端 ID 和密码向我的授权服务器调用(/oauth/token) 并且只使用不记名令牌调用我的 rest api,因此我无法访问他们的客户端 Idand 秘密,我想要一种方法来调用带有不记名令牌的 /oauth/checktoken 作为资源服务器中的参数。我们该怎么做?
      • 据我了解 OAuth2 协议,您的资源服务器是您授权本身的客户端。它将有自己的客户端 ID 和密码。用于请求令牌和检查令牌的客户端 ID 不必匹配。这就是我配置 OAuth2 环境的方式。
      • 谢谢@jan_tm,但问题是在我的情况下,客户端首先使用 /oauth/token 调用授权服务器获取令牌并仅在标头中提供不记名令牌来调用我的休息服务。我的资源服务器所要做的就是调用授权服务器并验证该承载令牌。这与他们提供的 JWT 令牌示例配合得非常好,但我不想要 JWT 令牌。
      猜你喜欢
      • 2017-12-15
      • 2020-12-29
      • 1970-01-01
      • 2015-06-18
      • 2023-02-22
      • 2017-05-06
      • 2016-02-03
      • 2020-05-18
      • 2019-10-03
      相关资源
      最近更新 更多