Spring google oauth2 не возвращает access_token - PullRequest
0 голосов
/ 27 мая 2020

Я пытаюсь реализовать OAuth2 с Google в моем приложении Spring Boot, и когда я извлекаю объект Principle, то, что я получаю, дается в последнюю очередь.

Вот метод, который я использую для извлечения пользователь:

@GetMapping("/user")
    public Principal user(Principal user) {
        return user;
    }

В ответе я не могу найти access_token или что-то в этом роде, хотя я нахожу ha sh его и id_token .

Итак, как мне его получить?

{
  "authorities": [
    {
      "authority": "ROLE_USER",
      "attributes": {
        "at_hash": "jVwoBv3AueI07-VJqKN0-A",
        "sub": "108161422188958518395",
        "email_verified": true,
        "iss": "https://accounts.google.com",
        "given_name": "test",
        "locale": "en",
        "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
        "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
        "aud": [
          "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
        ],
        "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
        "name": "test testing",
        "exp": 1590535843,
        "family_name": "testing",
        "iat": 1590532243,
        "email": "test@gamil.com"
      },
      "idToken": {
        "tokenValue": "123",
        "issuedAt": 1590532243,
        "expiresAt": 1590535843,
        "claims": {
          "at_hash": "jVwoBv3AueI07-VJqKN0-A",
          "sub": "108161422188958518395",
          "email_verified": true,
          "iss": "https://accounts.google.com",
          "given_name": "test",
          "locale": "en",
          "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
          "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
          "aud": [
            "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
          ],
          "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
          "name": "test testing",
          "exp": 1590535843,
          "family_name": "testing",
          "iat": 1590532243,
          "email": "test@gamil.com"
        },
        "subject": "108161422188958518395",
        "authorizedParty": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
        "accessTokenHash": "jVwoBv3AueI07-VJqKN0-A",
        "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
        "audience": [
          "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
        ],
        "issuer": "https://accounts.google.com",
        "authenticatedAt": null,
        "authorizationCodeHash": null,
        "authenticationContextClass": null,
        "authenticationMethods": null,
        "address": {
          "formatted": null,
          "streetAddress": null,
          "locality": null,
          "region": null,
          "postalCode": null,
          "country": null
        },
        "locale": "en",
        "zoneInfo": null,
        "fullName": "test testing",
        "familyName": "testing",
        "nickName": null,
        "updatedAt": null,
        "website": null,
        "middleName": null,
        "birthdate": null,
        "gender": null,
        "givenName": "test",
        "email": "test@gamil.com",
        "phoneNumber": null,
        "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
        "profile": null,
        "emailVerified": true,
        "phoneNumberVerified": null,
        "preferredUsername": null
      },
      "userInfo": null
    },
    {
      "authority": "SCOPE_https://www.googleapis.com/auth/userinfo.email"
    },
    {
      "authority": "SCOPE_https://www.googleapis.com/auth/userinfo.profile"
    },
    {
      "authority": "SCOPE_openid"
    }
  ],
  "details": {
    "remoteAddress": "0:0:0:0:0:0:0:1",
    "sessionId": "D3DCD92FB24A25CFC8B64160DD5C155B"
  },
  "authenticated": true,
  "principal": {
    "authorities": [
      {
        "authority": "ROLE_USER",
        "attributes": {
          "at_hash": "jVwoBv3AueI07-VJqKN0-A",
          "sub": "108161422188958518395",
          "email_verified": true,
          "iss": "https://accounts.google.com",
          "given_name": "test",
          "locale": "en",
          "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
          "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
          "aud": [
            "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
          ],
          "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
          "name": "test testing",
          "exp": 1590535843,
          "family_name": "testing",
          "iat": 1590532243,
          "email": "test@gamil.com"
        },
        "idToken": {
          "tokenValue": "123",
          "issuedAt": 1590532243,
          "expiresAt": 1590535843,
          "claims": {
            "at_hash": "jVwoBv3AueI07-VJqKN0-A",
            "sub": "108161422188958518395",
            "email_verified": true,
            "iss": "https://accounts.google.com",
            "given_name": "test",
            "locale": "en",
            "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
            "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
            "aud": [
              "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
            ],
            "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
            "name": "test testing",
            "exp": 1590535843,
            "family_name": "testing",
            "iat": 1590532243,
            "email": "test@gamil.com"
          },
          "subject": "108161422188958518395",
          "authorizedParty": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
          "accessTokenHash": "jVwoBv3AueI07-VJqKN0-A",
          "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
          "audience": [
            "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
          ],
          "issuer": "https://accounts.google.com",
          "authenticatedAt": null,
          "authorizationCodeHash": null,
          "authenticationContextClass": null,
          "authenticationMethods": null,
          "address": {
            "formatted": null,
            "streetAddress": null,
            "locality": null,
            "region": null,
            "postalCode": null,
            "country": null
          },
          "locale": "en",
          "zoneInfo": null,
          "fullName": "test testing",
          "familyName": "testing",
          "nickName": null,
          "updatedAt": null,
          "website": null,
          "middleName": null,
          "birthdate": null,
          "gender": null,
          "givenName": "test",
          "email": "test@gamil.com",
          "phoneNumber": null,
          "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
          "profile": null,
          "emailVerified": true,
          "phoneNumberVerified": null,
          "preferredUsername": null
        },
        "userInfo": null
      },
      {
        "authority": "SCOPE_https://www.googleapis.com/auth/userinfo.email"
      },
      {
        "authority": "SCOPE_https://www.googleapis.com/auth/userinfo.profile"
      },
      {
        "authority": "SCOPE_openid"
      }
    ],
    "attributes": {
      "at_hash": "jVwoBv3AueI07-VJqKN0-A",
      "sub": "108161422188958518395",
      "email_verified": true,
      "iss": "https://accounts.google.com",
      "given_name": "test",
      "locale": "en",
      "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
      "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
      "aud": [
        "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
      ],
      "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
      "name": "test testing",
      "exp": 1590535843,
      "family_name": "testing",
      "iat": 1590532243,
      "email": "test@gamil.com"
    },
    "idToken": {
      "tokenValue": "123",
      "issuedAt": 1590532243,
      "expiresAt": 1590535843,
      "claims": {
        "at_hash": "jVwoBv3AueI07-VJqKN0-A",
        "sub": "108161422188958518395",
        "email_verified": true,
        "iss": "https://accounts.google.com",
        "given_name": "test",
        "locale": "en",
        "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
        "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
        "aud": [
          "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
        ],
        "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
        "name": "test testing",
        "exp": 1590535843,
        "family_name": "testing",
        "iat": 1590532243,
        "email": "test@gamil.com"
      },
      "subject": "108161422188958518395",
      "authorizedParty": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
      "accessTokenHash": "jVwoBv3AueI07-VJqKN0-A",
      "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
      "audience": [
        "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
      ],
      "issuer": "https://accounts.google.com",
      "authenticatedAt": null,
      "authorizationCodeHash": null,
      "authenticationContextClass": null,
      "authenticationMethods": null,
      "address": {
        "formatted": null,
        "streetAddress": null,
        "locality": null,
        "region": null,
        "postalCode": null,
        "country": null
      },
      "locale": "en",
      "zoneInfo": null,
      "fullName": "test testing",
      "familyName": "testing",
      "nickName": null,
      "updatedAt": null,
      "website": null,
      "middleName": null,
      "birthdate": null,
      "gender": null,
      "givenName": "test",
      "email": "test@gamil.com",
      "phoneNumber": null,
      "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
      "profile": null,
      "emailVerified": true,
      "phoneNumberVerified": null,
      "preferredUsername": null
    },
    "userInfo": null,
    "claims": {
      "at_hash": "jVwoBv3AueI07-VJqKN0-A",
      "sub": "108161422188958518395",
      "email_verified": true,
      "iss": "https://accounts.google.com",
      "given_name": "test",
      "locale": "en",
      "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
      "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
      "aud": [
        "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
      ],
      "azp": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
      "name": "test testing",
      "exp": 1590535843,
      "family_name": "testing",
      "iat": 1590532243,
      "email": "test@gamil.com"
    },
    "name": "108161422188958518395",
    "subject": "108161422188958518395",
    "expiresAt": 1590535843,
    "authorizedParty": "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com",
    "accessTokenHash": "jVwoBv3AueI07-VJqKN0-A",
    "nonce": "4ByS7UbIYimkIOJvHUznNQGZ42GxgYh9tCM1K4vKfYc",
    "audience": [
      "693417856482-slejtj57gb2nrcehe82ih45o2vdhkj5t.apps.googleusercontent.com"
    ],
    "issuer": "https://accounts.google.com",
    "issuedAt": 1590532243,
    "authenticatedAt": null,
    "authorizationCodeHash": null,
    "authenticationContextClass": null,
    "authenticationMethods": null,
    "address": {
      "formatted": null,
      "streetAddress": null,
      "locality": null,
      "region": null,
      "postalCode": null,
      "country": null
    },
    "locale": "en",
    "zoneInfo": null,
    "fullName": "test testing",
    "familyName": "testing",
    "nickName": null,
    "updatedAt": null,
    "website": null,
    "middleName": null,
    "birthdate": null,
    "gender": null,
    "givenName": "test",
    "email": "test@gamil.com",
    "phoneNumber": null,
    "picture": "https://lh3.googleusercontent.com/a-/AOh14Gh6AHe4iGXQ9XKVgfVV35w31UbSzKs84xUYuNNS=s96-c",
    "profile": null,
    "emailVerified": true,
    "phoneNumberVerified": null,
    "preferredUsername": null
  },
  "authorizedClientRegistrationId": "google",
  "credentials": "",
  "name": "108161422188958518395"
}

1 Ответ

0 голосов
/ 01 июня 2020

Внутренний контроллер:

@Autowired
private GoogleOAuth2 googleOAuth2;
...
@GetMapping("/token")
    public String token() {
        System.out.println(googleOAuth2.getAccessToken());
        return googleOAuth2.getAccessToken();
}
...

Класс Google OAuth2:

public class GoogleOAuth2 extends ApiBinding {

    private String accessToken;

    public GoogleOAuth2(String accessToken) {
        super(accessToken);
        this.accessToken=accessToken;
    }

    public String getAccessToken() {
        return accessToken;
    }
}

И настройте OAuth2 с этим классом. @Configuration аннотация для класса, @Bean,@RequestScope для метода настройки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...