java 集成 Google 登录
步骤:2 google 授权配置2.1 配置网站2.2 配置投屏3.2 授权码回调接口3.3 获取授权码描述: 运行后,直接访问,获取授权码
·
1 Oauth2.0
步骤:
1 请求授权服务获取授权code。
2 用授权code请求token。
3 用token请求用户信息。
ps:oauth2.0 详情不懂的可以自己百度,也可以去b站学习。
2 google 授权配置
2.1 配置网站
https://console.cloud.google.com/
2.2 配置投屏
2.3 凭据
3 JAVA 代码
3.1 pom
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.22.2</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.32.1</version>
</dependency>
3.2 授权码回调接口
@RestController
public class GoogleController {
@GetMapping("/code")
public String getCode(HttpServletRequest request) {
return request.getParameter("code");
}
}
3.3 获取授权码
描述: 运行后,直接访问,获取授权码
public static void main(String[] args) throws GeneralSecurityException, IOException {
GoogleClientSecrets clientSecrets = new GoogleClientSecrets();
GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
details.setClientId("############");
details.setClientSecret("###################");
clientSecrets.setInstalled(details);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "openid");
// 创建验证流程对象
GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow
.Builder(httpTransport, jsonFactory, clientSecrets, scopes)
// AccessType为离线offline,才能获得Refresh Token
.setAccessType("offline").build();
// 返回跳转登录请求
String uri = googleAuthorizationCodeFlow.newAuthorizationUrl().setRedirectUri("http://localhost:8080/code").build();
System.out.println(uri);
}
3.4 根据授权码获取用户信息
public static void main(String[] args) throws GeneralSecurityException, IOException {
GoogleClientSecrets clientSecrets = new GoogleClientSecrets();
GoogleClientSecrets.Details details = new GoogleClientSecrets.Details();
details.setClientId("########################");
details.setClientSecret("############");
clientSecrets.setInstalled(details);
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
// 创建验证流程对象
List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "openid");
GoogleAuthorizationCodeFlow googleAuthorizationCodeFlow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, scopes)
// AccessType为离线offline,才能获得Refresh Token
.setAccessType("offline").build();
GoogleAuthorizationCodeTokenRequest tokenRequest = googleAuthorizationCodeFlow.newTokenRequest("4/0AX4XfWh225UarpB-nVkCbOdx5kbPvhjHOlN9t2UgEi2ljlw_QWiaPGLOxx6tzyMI8Ac1rA");
tokenRequest.setRedirectUri("http://localhost:8080/code");
tokenRequest.setGrantType("authorization_code");
GoogleTokenResponse tokenResponse = tokenRequest.execute();
String token = tokenResponse.getAccessToken();
if (StringUtils.isNotBlank(tokenResponse.getIdToken())) {
GoogleIdTokenVerifier idTokenVerifier = new GoogleIdTokenVerifier.Builder(googleAuthorizationCodeFlow.getTransport(), googleAuthorizationCodeFlow.getJsonFactory()).build();
idTokenVerifier.verify(tokenResponse.getIdToken());
GoogleIdToken googleIdToken = idTokenVerifier.verify(tokenResponse.getIdToken());
if (googleIdToken != null && googleIdToken.getPayload() != null) {
googleIdToken.getPayload().getEmail();
System.out.println(googleIdToken.getPayload().toString());
}
}
}
更多推荐
所有评论(0)