使用Spring Security OAuth2进行访问令牌请求的JWT承载交换

时间:2016-01-15 16:09:29

标签: spring oauth spring-security spring-boot jwt

我的工作设置包括:

  • 授权服务(AS)
  • 资源服务(RS)
  • 依赖方(RP)

实现基于Spring Boot with Spring Security(OAuth2)。我有以下工作2LA流程:

  1. RP可以使用client_secretgrant_type=client_credentials向AS发送访问令牌请求。
  2. AS使用访问令牌响应RP。
  3. RP能够使用所述访问令牌向RS发出授权请求。
  4. RS可以使用AS上的/check_token端点验证访问令牌。
  5. 问题我需要对我的AS做出哪些更改,以便在上面的步骤1中接受基于JWT的访问令牌请求?

    请注意,我不需要基于JWT的访问令牌。只有RP对AS的初始请求才能成为基于JWT的请求。

    相关问题:How to use Spring OAuth2 JWT Token?

    澄清我想知道为了使用Spring Security OAuth2库从RP接受JWT,我需要编写code。我在哪里将RP的公钥添加到AS以及在哪里将RP的私钥添加到OAuth2休息模板?

    RP OAuth2客户端配置:

    @Configuration
    @EnableOAuth2Client
    open class OAuth2ClientConfiguration {
    
        val tokenUrl = "http://localhost:8180/oauth/token"
    
        @Bean
        open fun resource(): OAuth2ProtectedResourceDetails {
            return ClientCredentialsResourceDetails().apply {
                clientId = "demo-rp"
                clientSecret = "rp-secret"
                grantType = "client_credentials"
                scope = listOf("quotes")
                accessTokenUri = tokenUrl
            }
        }
    
        @Bean
        open fun restTemplate(): OAuth2RestOperations {
            return OAuth2RestTemplate(
                    resource(),
                    DefaultOAuth2ClientContext(DefaultAccessTokenRequest()))
        }
    
    }
    

    AS OAuth2配置:

    @Configuration
    @EnableAuthorizationServer
    open class OAuth2Configuration : AuthorizationServerConfigurerAdapter() {
    
        @Autowired
        val authenticationManager: AuthenticationManager? = null
    
        override fun configure(security: AuthorizationServerSecurityConfigurer) {
            // @formatter:off
            security
            .tokenKeyAccess("isAnonymous() || hasAuthority('ROLE_TRUSTED_CLIENT')")
            .checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')")
            // @formatter:on
        }
    
        override fun configure(clients: ClientDetailsServiceConfigurer) {
            // @formatter:off
            clients.inMemory()
            .withClient("demo-rp")
                .authorizedGrantTypes("client_credentials")
                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                .scopes("quotes")
                .secret("rp-secret")
                .accessTokenValiditySeconds(60)
            .and()
            .withClient("demo-rs")
                .authorizedGrantTypes("client_credentials")
                .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                .secret("rs-secret")
                .accessTokenValiditySeconds(60)
            // @formatter:on
        }
    
        override fun configure(endpoints: AuthorizationServerEndpointsConfigurer) {
            // @formatter:off
            endpoints
            .authenticationManager(authenticationManager)
            // @formatter:on
        }
    }
    

2 个答案:

答案 0 :(得分:1)

我最终使用Nimbus OAuth2 SDK编写了一个发送基于JWT的访问令牌请求的客户端。这是RP的一部分。

在AS中,我添加了JwtTokenEndpointAuthenticationFilter来检查ServletRequest并使用手写的AuthenticationProvider执行身份验证。

示例AS代码here。 示例RP代码here

此解决方案的灵感来自here

答案 1 :(得分:0)

  

请注意,我不需要基于JWT的访问令牌。只有RP对AS的初始请求才能成为基于JWT的请求。

无论将生成什么类型​​的OAuth 2.0,每个流中的

Access Token个组件交互都是相同的。例如,在Password Grant流程中,客户端向Authorization Server发送请求并获取访问令牌,如果他幸运的话。授权服务器使用内存中令牌或JWT或持久令牌这一事实并未改变此特定流程中的交互。