• SpringBoot第三方登录JustAuth


    JustAuth流程

    1. 创建授权请求,并跳转到授权页面,以便用户进行认证和授权
    2. 生成一个随机的 stateId,用于标识本次授权请求
    3. 封装到 Map 中作为响应返回给客户端
    4. 处理授权成功后回调的请求
    5. 调用 AuthRequest 的 login() 方法完成授权
    6. AuthResponse 对象封装到 ResponseResult 中返回给客户端

    配置第三方应用

    我们以gitee为例子,在个人设置中找到第三方应用。

     

    创建应用

     填写申请第三方应用

    获取这两个关键密文 

    引入依赖

    1. me.zhyd.oauth
    2. JustAuth
    3. 1.16.5
    4. org.springframework.boot
    5. spring-boot-starter-thymeleaf

    配置properties文件

    1. spring.thymeleaf.cache=false
    2. spring.thymeleaf.prefix=classpath:/templates/
    3. spring.thymeleaf.suffix=.html
    4. gitee.ClientID=139dd95598cb6539e2e79edfdc67f29acf8ea52bbaa2f45c204efdb0d9bc0e24
    5. gitee.ClientSecret=f0158ab4d918dd2f9b8d2c427bed5ecfbb1c1ddf8941c519108ce33a17fc3bd9

    封装结果类

    1. import java.io.Serializable;
    2. @Data
    3. public class ResponseResult implements Serializable {
    4. private Boolean success;
    5. private Integer code;
    6. private String msg;
    7. private T data;
    8. public static ResponseResult ok(Object data) {
    9. ResponseResult result = new ResponseResult();
    10. result.setData(data);
    11. return result;
    12. }
    13. }

     

    login页面

    通过这个login-controller跳转到login页面 

    1. @Controller
    2. @RequestMapping("/login")
    3. public class LoginController {
    4. @RequestMapping("/home")
    5. public String login(Model model){
    6. return "login";
    7. }
    8. }

     在这个页面点击gitee登录可以返回后端接口,后端接口再跳转gitee授权页面,返回数据给前端

    1. <!DOCTYPE html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. <script type="text/javascript" src="/js/jquery-3.5.1.min.js"></script>
    7. </head>
    8. <body>
    9. <button id="bg">使用gitee登录</button>
    10. </body>
    11. <script type="text/javascript">
    12. $(function(){
    13. $("#bg").click(function(){
    14. $.get("/login-before",function(data,status){
    15. window.location=data.data.authorizePageUrl;
    16. });
    17. });
    18. });
    19. </script>
    20. </html>

    Oauth2controller

    后端完成获取授权码,发送授权码获取用户基本信息等操作 

    1. import com.example.demo.util.ResponseResult;
    2. import me.zhyd.oauth.config.AuthConfig;
    3. import me.zhyd.oauth.model.AuthCallback;
    4. import me.zhyd.oauth.model.AuthResponse;
    5. import me.zhyd.oauth.request.AuthGiteeRequest;
    6. import me.zhyd.oauth.request.AuthRequest;
    7. import org.slf4j.Logger;
    8. import org.slf4j.LoggerFactory;
    9. import org.springframework.beans.factory.annotation.Value;
    10. import org.springframework.web.bind.annotation.GetMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. import java.util.HashMap;
    13. import java.util.Map;
    14. import java.util.UUID;
    15. @RestController
    16. public class Oauth2Controller {
    17. @Value("${gitee.ClientID}")
    18. private String clientID;
    19. @Value("${gitee.ClientSecret}")
    20. private String clientSecret;
    21. private static final Logger LOG = LoggerFactory.getLogger(Oauth2Controller.class);
    22. @GetMapping("/login-before")
    23. public ResponseResult loginBeforByGitee(){
    24. // 创建授权request
    25. AuthRequest authRequest = getAuthRequest();
    26. String stateId = UUID.randomUUID().toString();
    27. LOG.info("stateId = " + stateId);
    28. // 跳转到授权页面
    29. String authorizePageUrl = authRequest.authorize(stateId);
    30. LOG.info("authorizePageUrl地址为"+authorizePageUrl);
    31. Map map = new HashMap<>();
    32. map.put("authorizePageUrl",authorizePageUrl);
    33. return ResponseResult.ok(map);
    34. }
    35. @GetMapping("/callback")
    36. public ResponseResult callback(String code,String state) {
    37. // gitee授权通过跳转到回调接口
    38. LOG.info("code = " + code);
    39. LOG.info("state = " + state);
    40. // 使用这个code去获取用户基本信息
    41. AuthResponse login = getAuthRequest().login(AuthCallback.builder().state(state).code(code).build());
    42. // 数据返回给前端
    43. return ResponseResult.ok(login);
    44. }
    45. private AuthRequest getAuthRequest(){
    46. // 创建授权request
    47. AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
    48. .clientId(clientID)
    49. .clientSecret(clientSecret)
    50. .redirectUri("http://127.0.0.1:7125/callback")
    51. .build());
    52. return authRequest;
    53. }
    54. }

    测试

    访问localhost:7125/login/home

    授权登录

     前端获取到基本信息

  • 相关阅读:
    Qt的.pro文件格式解析
    ASP.NET Core - 自定义中间件
    C语言运算符优先级
    牛客-TOP101-BM43
    指针篇章-(4)+qsort函数的模拟
    Windows下Docker搭建Flink集群
    Hystrix原理
    使用logger.error(“自定义错误信息描述“,e)将错误信息输出到日志文件上
    python+vue+elementui舞蹈教学视频评分系统_o4o1y
    Leetcode 3286. Find a Safe Walk Through a Grid
  • 原文地址:https://blog.csdn.net/qq_63431773/article/details/133519278