简体   繁体   English

Spring Google OAuth2 带刷新令牌

[英]Spring Google OAuth2 With Refresh Token

My current OAuth2 configuration doesn't return refresh token.我当前的 OAuth2 配置不返回刷新令牌。 My configuration is as basic as possible.我的配置尽可能基本。 How can I configure it so that it would return refresh_token as well.我该如何配置它,以便它也能返回 refresh_token 。 I'm authenticating user on the server side.我正在服务器端对用户进行身份验证。 So JS solutions don't work.所以JS解决方案不起作用。

I need the refresh token for RememberMe functionality.我需要 RememberMe 功能的刷新令牌。 Specifically I'm using oAuth2AuthorizedClientService.loadAuthorizedClient(clientId, principalName);具体来说,我正在使用oAuth2AuthorizedClientService.loadAuthorizedClient(clientId, principalName); method, to get access to the information that was retrieved from googles authentication server.方法,以访问从谷歌身份验证服务器检索到的信息。

 WebSecurityConfigurerAdapter {
 ...
 httpSecurity
        ...
          .and()
        .oauth2Login()
          .and()
        .rememberMe()
        ...

Application.yml:应用程序.yml:

security:
    oauth2:
      client:
        registration:
          google:
            clientId: ZZZ
            clientSecret: zzzz
            redirectUri: https://example.com/login/oauth2/code/google

My solution was to add OAuth2AuthorizationRequestResolver我的解决方案是添加OAuth2AuthorizationRequestResolver

In OAuth2AuthorizationRequestResolver I changed the customAuthorizationRequest (see below).OAuth2AuthorizationRequestResolver我更改了customAuthorizationRequest (见下文)。 Now it returns refresh token every time.现在它每次都返回刷新令牌。

private OAuth2AuthorizationRequest customAuthorizationRequest( OAuth2AuthorizationRequest authorizationRequest) {

    Map<String, Object> additionalParameters =new LinkedHashMap<>(authorizationRequest.getAdditionalParameters());
    additionalParameters.put("access_type", "offline");

    return OAuth2AuthorizationRequest.from(authorizationRequest)
            .additionalParameters(additionalParameters)
            .build();
}

also updated my WebSecurityConfigurerAdapter还更新了我的WebSecurityConfigurerAdapter

.oauth2Login()
    .authorizationEndpoint()
    .authorizationRequestResolver(
            new CustomAuthorizationRequestResolver(
                    this.clientRegistrationRepository))
    .and()
    .and()
.rememberMe()

if somebody finds simpler solution do post: :)如果有人找到更简单的解决方案,请发布::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM