|
@@ -1,13 +1,17 @@
|
|
package com.neko.controller;
|
|
package com.neko.controller;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
import com.neko.domain.dto.userDto.PhoneLoginDto;
|
|
import com.neko.domain.dto.userDto.PhoneLoginDto;
|
|
import com.neko.domain.dto.userDto.YanDto;
|
|
import com.neko.domain.dto.userDto.YanDto;
|
|
import com.neko.service.UserService;
|
|
import com.neko.service.UserService;
|
|
import com.neko.utils.ResultVo;
|
|
import com.neko.utils.ResultVo;
|
|
|
|
+import com.neko.utils.ThirdlyResult;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @Date 2025/5/18 21:11
|
|
* @Date 2025/5/18 21:11
|
|
@@ -15,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|
**/
|
|
**/
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/user")
|
|
@RequestMapping("/user")
|
|
|
|
+@Slf4j
|
|
public class UserController {
|
|
public class UserController {
|
|
@Autowired
|
|
@Autowired
|
|
UserService userService;
|
|
UserService userService;
|
|
@@ -39,4 +44,56 @@ public class UserController {
|
|
return userService.yan(dto);
|
|
return userService.yan(dto);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 1、用户点击使用 Gitee 作为第三方登录,后台重定向至gitee认证页面
|
|
|
|
+ *
|
|
|
|
+ * @param loginType
|
|
|
|
+ * @param response
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("/gitee_login/{loginType}")
|
|
|
|
+ public void thirdlyLogin(@PathVariable("loginType") String loginType, HttpServletResponse response) throws IOException {
|
|
|
|
+ String url = userService.choiceLoginType(loginType);
|
|
|
|
+ log.info(url);
|
|
|
|
+ response.sendRedirect(url);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 回调地址 这里就对应着我们在gitee创建第三方应用时填写的回调地址 这里会传回一个 code ,这个code 就是用户认证通过的授权码
|
|
|
|
+
|
|
|
|
+ * http://127.0.0.1:8089/oauth/gitee/callback?code=xxxxebe2a67ba13xxxx925615aa89041
|
|
|
|
+ * 其中 code 为用户授权码,由gitee授权服务器调用回调地址携带而来
|
|
|
|
+ *
|
|
|
|
+ * @param loginType
|
|
|
|
+ * @param code
|
|
|
|
+ * @param state 它并非是必填项,gitee调用回调地址时,其实没有携带这个参数,但是在oauth中有讲到,当时就写上去了。
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @ResponseBody
|
|
|
|
+ @GetMapping("/{loginType}/callback")
|
|
|
|
+ public String redirectUri(@PathVariable("loginType") String loginType, String code, String state) {
|
|
|
|
+ log.info("code==>{}", code);
|
|
|
|
+ //1、拿到这个code之后,我们要再向 gitee 服务起请求,获取 access_token 请注意这是一个post 请求
|
|
|
|
+ // https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
|
|
|
|
+ String result = userService.getOauthToken(loginType, code);
|
|
|
|
+ ThirdlyResult thirdlyResult = (ThirdlyResult) JSON.parseObject(result, ThirdlyResult.class);
|
|
|
|
+ /**
|
|
|
|
+ * {
|
|
|
|
+ * "access_token":"f7d851b2cdxx1fd491b",
|
|
|
|
+ * "token_type":"bearer",
|
|
|
|
+ * "expires_in":86400,
|
|
|
|
+ * "refresh_token":"9f3098c7a8be09cd15xxcc38fb3dxxxb8e40f0800ced8",
|
|
|
|
+ * "scope":"user_info",
|
|
|
|
+ * "created_at":1661659283
|
|
|
|
+ * }
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 我这里只是测试获取用户信息的请求
|
|
|
|
+ * 如果需要实现登录的话,自己加上业务逻辑即可
|
|
|
|
+ */
|
|
|
|
+ // 2、拿到 access_token 以后发送一个get 请求,获取用户信息
|
|
|
|
+ String userInfo = userService.getUserInfo(loginType, thirdlyResult.getAccessToken());
|
|
|
|
+ return userInfo;
|
|
|
|
+ }
|
|
}
|
|
}
|