浏览代码

Merge branch 'jc' of http://git.workervip.com/JC2407A/neko into yv

# Conflicts:
#	src/main/java/com/neko/service/impl/UserServiceImpl.java
p裴秀宇 4 天之前
父节点
当前提交
22ed3cc9b8

+ 22 - 0
pom.xml

@@ -61,6 +61,11 @@
             <artifactId>fastjson2</artifactId>
             <artifactId>fastjson2</artifactId>
             <version>2.0.35</version>
             <version>2.0.35</version>
         </dependency>
         </dependency>
+<!--        <dependency>-->
+<!--            <groupId>com.alibaba</groupId>-->
+<!--            <artifactId>fastjson</artifactId>-->
+<!--            <version>1.2.79</version>-->
+<!--        </dependency>-->
 
 
         <dependency>
         <dependency>
             <groupId>io.jsonwebtoken</groupId>
             <groupId>io.jsonwebtoken</groupId>
@@ -78,6 +83,23 @@
             <artifactId>spring-boot-starter-data-redis</artifactId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
         </dependency>
 
 
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.7.22</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>
+
+<!--        <dependency>-->
+<!--            <groupId>com.xkcoding.justauth</groupId>-->
+<!--            <artifactId>justauth-spring-boot-starter</artifactId>-->
+<!--            <version>1.4.0</version>-->
+<!--        </dependency>-->
+
     </dependencies>
     </dependencies>
     <dependencyManagement>
     <dependencyManagement>
         <dependencies>
         <dependencies>

+ 18 - 0
src/main/java/com/neko/config/GiteeProperties.java

@@ -0,0 +1,18 @@
+package com.neko.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Date 2025/5/19 15:26
+ * @Author neko
+ **/
+@Data
+@Component
+@ConfigurationProperties(prefix = "gitee")
+public class GiteeProperties {
+    private String clientId;
+    private String clientSecret;
+    private String redirectUri;
+}

+ 11 - 0
src/main/java/com/neko/config/ThirdlyLoginTypeConstant.java

@@ -0,0 +1,11 @@
+package com.neko.config;
+
+/**
+ * @Date 2025/5/19 15:37
+ * @Author neko
+ **/
+public class ThirdlyLoginTypeConstant {
+    public final static String  GITEE="gitee";
+    public final static String  GITEE_URL="https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code";
+    public final static String  GITEE_OAUTH_TOKEN_URL="https://gitee.com/oauth/token";
+}

+ 60 - 3
src/main/java/com/neko/controller/UserController.java

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

+ 23 - 0
src/main/java/com/neko/service/UserService.java

@@ -16,4 +16,27 @@ public interface UserService extends IService<User> {
     ResultVo phoenLogin(PhoneLoginDto phoneLoginDto);
     ResultVo phoenLogin(PhoneLoginDto phoneLoginDto);
 
 
     ResultVo yan(YanDto dto);
     ResultVo yan(YanDto dto);
+
+    /**
+     * 根据传入的所选择的第三方登录类型,返回认证的url
+     * @param loginType 第三方登录类型
+     * @return
+     */
+    String choiceLoginType(String loginType);
+    /**
+     *
+     * 根据用户的授权码和登录类型,获取第三方应用的 access_token
+     * @param loginType 第三方登录类型
+     * @param code 用户授权码
+     * @return
+     */
+    String getOauthToken(String loginType, String code);
+
+    /**
+     * 获取授权的用户信息
+     * @param loginType 第三方登录类型
+     * @param accessToken 认证通过的令牌
+     * @return
+     */
+    String getUserInfo(String loginType,String accessToken);
 }
 }

+ 2 - 2
src/main/java/com/neko/service/impl/UserServiceImpl.java

@@ -35,7 +35,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
 
 
     @Override
     @Override
     public ResultVo phoenLogin(PhoneLoginDto dto) {
     public ResultVo phoenLogin(PhoneLoginDto dto) {
-        if(dto.getPhone()==null){
+        if(dto.getPassword()==null){
             return ResultVo.error(ApiServiceExceptionEnum.USER_NOT_NULL);
             return ResultVo.error(ApiServiceExceptionEnum.USER_NOT_NULL);
         }
         }
         if(StringUtils.isEmpty(dto.getPassword())){
         if(StringUtils.isEmpty(dto.getPassword())){
@@ -87,7 +87,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
 
 
         }else{
         }else{
             log.info("密码登录");
             log.info("密码登录");
-            if(StringUtils.isEmpty(dto.getPassword())){
+            if(dto.getPassword()==null){
                 //判断密码是否为空
                 //判断密码是否为空
                 return ResultVo.error(ApiServiceExceptionEnum.PASSWORD_NOT_NULL);
                 return ResultVo.error(ApiServiceExceptionEnum.PASSWORD_NOT_NULL);
             }
             }

+ 2 - 2
src/main/java/com/neko/utils/ApiServiceExceptionEnum.java

@@ -6,8 +6,8 @@ public enum ApiServiceExceptionEnum implements BaseExceptionEnum{
 	LOGIN_OK(200,"登录成功"),
 	LOGIN_OK(200,"登录成功"),
 	LOGIN_ERROR(400,"登录失败"),
 	LOGIN_ERROR(400,"登录失败"),
 	USER_NOT_NULL(1001,"手机号不能为空"),
 	USER_NOT_NULL(1001,"手机号不能为空"),
-	YAN_NOT_NULL(1001,"验证码不能为空"),
-	PASSWORD_NOT_NULL(1001,"密码不能为空"),
+	YAN_NOT_NULL(1004,"验证码不能为空"),
+	PASSWORD_NOT_NULL(1005,"密码不能为空"),
 	YAN_ERROR(1002,"验证码不正确"),
 	YAN_ERROR(1002,"验证码不正确"),
 	YONGHU_NOT_NULL(1003,"用户不存在"),
 	YONGHU_NOT_NULL(1003,"用户不存在"),
 	PASSWORD_ERROR(1003,"密码错误"),
 	PASSWORD_ERROR(1003,"密码错误"),

+ 32 - 0
src/main/java/com/neko/utils/ThirdlyResult.java

@@ -0,0 +1,32 @@
+package com.neko.utils;
+
+import lombok.Data;
+
+/**
+ * @Date 2025/5/19 15:49
+ * @Author neko
+ **/
+@Data
+public class ThirdlyResult {
+    /**
+     * {
+     * "access_token":"f7xxxb71fd491b",
+     * "token_type":"bearer",
+     * "expires_in":86400,
+     * "refresh_token":"9f3098c7a8be09cdxxxxx53a2f69dccxxxx8e40f0800ced8",
+     * "scope":"user_info",
+     * "created_at":1661659283
+     * }
+     */
+    private String accessToken;
+
+    private String tokenType;
+
+    private Long expiresIn;
+
+    private String refreshToken;
+
+    private String scope;
+
+    private String createAt;
+}

+ 4 - 0
src/main/resources/application.yml

@@ -17,3 +17,7 @@ spring:
 mybatis-plus:
 mybatis-plus:
   mapper-locations: classpath:/mapper/*.xml
   mapper-locations: classpath:/mapper/*.xml
   type-aliases-package: com.neko.domain.pojo
   type-aliases-package: com.neko.domain.pojo
+gitee:
+  client-id: 289d512d93c5c666ad76a859bfcb996bfebed52303d66e9328d23b00d2af8377
+  client-secret: 65a22c04292599fc6c05f03e7905c017fb4f3f80f28386e28da9b02b4cf808fb
+  redirect-uri: http://localhost/login