reg*_*i88 2 social rest spring oauth
我需要使用 spring boot 开发一些后端应用程序(最好是 2.0,但 1.5 也可以),这将允许使用 facebook 和 google 进行 oauth 登录。我有以下要求:
我怎样才能做到这一点?我看过很多教程,但它们都假设后端和前端连接到一个应用程序中。出于某些原因,我真的不想要那个解决方案:这个后端将适当地为少数应用程序发布数据。你能帮我解决这个问题吗?我的意思是一些教程,代码仓库等......感谢建议
小智 7
谷歌和 Facebook 提供了分步详细信息,以与他们的登录信息集成并验证带有支持的令牌。您可以按照以下步骤了解详情。
谷歌:https : //developers.google.com/identity/sign-in/web/sign-in https://developers.google.com/identity/sign-in/web/backend-auth
进行休息调用https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=以集成和验证带有后备的令牌,传递使用前端谷歌网络插件成功登录的 accessToken 并存储信息或使用您的数据库进行验证。
public String getGoogleTokenInfo(String accessToken) throws BadRequestException {
log.debug("Calling Google API to get token info");
RestTemplate restTemplate = new RestTemplate();
String googleResponse = null;
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString("https://www.googleapis.com/oauth2/v3/tokeninfo").queryParam("id_token", accessToken);
log.debug("google login uri {}", uriBuilder.toUriString());
googleResponse = restTemplate.getForObject(uriBuilder.toUriString(), String.class);
log.info("Gmail user authenticated successfully, details [{}]", googleResponse.toString());
} catch (HttpClientErrorException e) {
log.error("Not able to authenticate from Google");
try {
JsonNode error = new ObjectMapper().readValue(e.getResponseBodyAsString(), JsonNode.class);
log.error(error.toString());
throw new BadRequestException("Invalid access token");
} catch (IOException mappingExp) {
throw new BadRequestException("Invalid user");
}
} catch (Exception exp) {
log.error("User is not authorized to login into system", exp);
throw new BadRequestException("Invalid user");
}
return googleResponse;
}
Run Code Online (Sandbox Code Playgroud)
脸书:https : //developers.facebook.com/docs/facebook-login/web#example
通过传递使用 facebook 前端 Web 插件成功登录的 accessToken进行休息调用https://graph.facebook.com/me?access_token=以验证令牌并获取个人资料信息并将信息存储到您的数据库.
public String getFacebookProfileInfo(final String accessToken) throws BadRequestException {
log.debug("Calling Facebook API to validate and get profile info");
RestTemplate restTemplate = new RestTemplate();
String facebook = null;
// field names which will be retrieved from facebook
final String fields = "id,email,first_name,last_name";
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString("https://graph.facebook.com/me")
.queryParam("access_token", accessToken).queryParam("fields", fields);
log.debug("Facebook profile uri {}", uriBuilder.toUriString());
facebook = restTemplate.getForObject(uriBuilder.toUriString(), String.class);
log.info("Facebook user authenticated and profile fetched successfully, details [{}]", facebook.toString());
} catch (HttpClientErrorException e) {
log.error("Not able to authenticate from Facebook");
throw new BadRequestException("Invalid access token");
} catch (Exception exp) {
log.error("User is not authorized to login into system", exp);
throw new BadRequestException("Invalid user";
}
return facebook;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3053 次 |
| 最近记录: |