spring - Spring Boot + REST API + 社交登录
问题描述
我需要使用 Spring Boot 开发一些后端应用程序(最好是 2.0,但 1.5 也可以),这将允许使用 facebook 和 google 进行 oauth 登录。我有以下要求:
- 后端将启用使用 google 或 facebook 登录(并且在第一次登录时后端必须记住 DB 中的用户),
- 后端将仅发布其余端点,
- 前端将登录到 facebook 或 google,然后它将令牌发送到后端,后端将在 facebook 或 google 中验证令牌(显然它取决于令牌提供者)并返回一些 json 数据。
我怎样才能做到这一点?我看过很多教程,但都假设后端和前端连接到一个应用程序中。由于某些原因,我真的不想要那个解决方案:这个后端可能会为少数应用程序发布数据。你能帮我吗?我的意思是一些教程,代码仓库等......谢谢你的建议
解决方案
Google 和 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;
}
脸书: 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;
}
推荐阅读
- reactjs - 对象不支持属性或方法“endsWith”,但我不使用它
- javascript - 如何从 FileReader() 输出值
- css - CSS Target 2nd td 仅在前 3 行
- html - ngModal 不适用于 ngFor 中的输入类型复选框
- angular - 模板引用变量在 ngFor 循环中未定义
- java - Mockito:使用列表
作为参数 - c++ - 为什么我会得到 std::exp 的特定于平台的结果?
- python - 连接到 sqlite 数据库时出现 sqlalchemy.exc.InvalidRequestError
- ruby-on-rails - 乘客 3.x 独立二进制文件连接超时
- python - 如何在张量流中制作自定义激活函数