具有授权代码授权的 Spring Security OAuth2 客户端 - 如何处理令牌请求?

时间:2020-12-19 08:46:09

标签: spring-boot spring-security

我正在使用具有 spring-boot-starter-oauth2-clientspring-boot-starter-security 依赖项的 2.3.4 版构建 Spring Boot 应用程序。

我正在尝试实施 JIRA Tempo plugin OAuth support

我使用以下属性使其部分工作:

spring.security.oauth2.client.registration.tempo.redirect-uri=http://localhost:8080
spring.security.oauth2.client.registration.tempo.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.tempo.client-id=<the-client-id>
spring.security.oauth2.client.registration.tempo.client-secret=<the-client-secret>
spring.security.oauth2.client.registration.tempo.provider=tempo

spring.security.oauth2.client.provider.tempo.authorization-uri=https://mycompany.atlassian.net/plugins/servlet/ac/io.tempo.jira/oauth-authorize/?access_type=tenant_user
spring.security.oauth2.client.provider.tempo.token-uri=https://api.tempo.io/oauth/token/

和这个配置:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests(expressionInterceptUrlRegistry -> expressionInterceptUrlRegistry.anyRequest().authenticated())
            .oauth2Login();
    }

当我访问 http://localhost:8080 时,它重定向到 JIRA/Tempo 并在那里显示批准对话框以授予对我的应用程序的 Tempo 数据的访问权限。我可以授予访问权限,但之后,它只会再次重定向到该页面,而不是显示我自己的应用程序。

通过调试,我注意到有一个重定向到 http://localhost:8080/?code=.... 但 Spring Security 没有处理它。我还需要配置什么?

我也尝试在 DefaultAuthorizationCodeTokenResponseClient 中设置一些断点,但它们从未被命中。

更新:

我将 redirect-uri 更改为:

spring.security.oauth2.client.registration.tempo.redirect-uri={baseUrl}/{action}/oauth2/code/{registrationId}

(我将 Tempo 中的重定向 URI 设置更改为 http://localhost:8080/login/oauth2/code/tempo)。

现在重定向回我的本地主机,但它失败并显示 authorization_request_not_found

更新 2:

authorization_request_not_found 的原因似乎与 HttpSessionOAuth2AuthorizationRequestRepository.removeAuthorizationRequest(HttpServletRequest request)authorizationRequests 中的 stateParameters 不匹配。

debugger screenshot

注意一个以 = 结尾,另一个以 %3D 结尾,这使得它们匹配。 = 的 URL 编码为 %3D 可能并非巧合。我不清楚这是 Spring Security 的问题,还是 Tempo 资源服务器的问题,还是我的配置错误。

1 个答案:

答案 0 :(得分:0)

redirect-uri 属性不应指向应用程序的根目录,而应指向处理过滤器,即处理重定向后的代码。

对您来说最好的做法是暂时不理会redirect-uri。然后它将默认为 /login/oauth2/code/* 并且此 Url 由 org.springframework.security.oauth2.client.web.OAuth2LoginAuthenticationFilter 处理。