Springboot整合第三方登录

Springboot整合第三方登录

为什么采用第三方登录

​ 采用第三方登录可以避免重新注册账号的繁琐,也不需要再为密码和昵称发愁,而第三方登录有一个比较好用的包,里面整合了多种第三方登录,开箱即用,非常方便。就是JustAuth,网址https://www.justauth.cn/。

整合第三方登录
创建应用

​ 这里采用gitee进行测试,因为gitee不需要其他的东西,只需要你有一个账号就可以。注册一个gitee账号后,到这里(https://gitee.com/oauth/applications)创建一个应用。

在这里插入图片描述

  • 应用名称,这个根据自己的需要填,我测试使用就填了一个测试demo1

  • 应用描述可填可不填

  • 应用主页需要填一个已经上线的项目地址(这个不一定要填自己的项目,因为既然要整和第三方登录,那项目大概率是没上线的,这里可以随便填一个可用的网址,某度什么的都可以)

  • 应用回调地址这个不能随便填,这个地址是之后用户授权后的回调地址,这是我测试时填的http://localhost:8080/oauth/callback

  • 权限 根据页面提示操作,默认勾选第一个就行

在这里插入图片描述

创建成功后,可以在我的应用里面找到这个应用,里面的ClientID和ClientSecret是待会要用到的。

导入依赖
<!--引入第三方登录依赖-->
<dependency>
    <groupId>me.zhyd.oauth</groupId>
    <artifactId>JustAuth</artifactId>
    <version>1.16.5</version>
</dependency>
<!--引入处理json依赖-->
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2</artifactId>
    <version>2.0.7</version>
</dependency>
创建controller类
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping("/oauth")
public class RestAuthController {

    @RequestMapping("/render")//调用此接口来进行授权认证
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest = getAuthRequest();
        response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
    }

    @RequestMapping("/callback")
    //授权认证成功后会自动调用此接口,这个接口是创建应用时填写的回调接口
    public Object login(AuthCallback callback) {
        //可以对对象进行操作,比如取出里面得名称跟邮箱信息等等
        //我这里取出了昵称和邮箱
        AuthRequest authRequest = getAuthRequest();
        AuthResponse login = authRequest.login(callback);
        JSONObject remoteData = JSONObject.parseObject(JSON.toJSONString(login));
        Object gitEEUser = remoteData.get("data");
        JSONObject user = JSONObject.parseObject(JSON.toJSONString(gitEEUser));
        Object nickname = user.get("nickname");
        Object email = user.get("email");
        HashMap<String,Object> map = new HashMap<>();
        map.put("nickname",nickname);
        map.put("email",email);
        //TODO 数据库操作
        return map;
    }

    private AuthRequest getAuthRequest() {
        return new AuthGiteeRequest(AuthConfig.builder()
                .clientId("Client ID")//这些参数按自己的填即可
                .clientSecret("Client Secret")
                .redirectUri("应用回调地址")//这就是上面的那个接口地址,也是创建应用时写的那个
                .build());
    }
}

测试访问 localhost:8080/oauth/rander 接口,授权认证后,显示昵称和邮箱

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值