首页 > 解决方案 > Spring Boot + REST API + 社交登录

问题描述

我需要使用 Spring Boot 开发一些后端应用程序(最好是 2.0,但 1.5 也可以),这将允许使用 facebook 和 google 进行 oauth 登录。我有以下要求:

我怎样才能做到这一点?我看过很多教程,但都假设后端和前端连接到一个应用程序中。由于某些原因,我真的不想要那个解决方案:这个后端可能会为少数应用程序发布数据。你能帮我吗?我的意思是一些教程,代码仓库等......谢谢你的建议

标签: springrestoauthsocial

解决方案


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;
    }

推荐阅读