Browse Source

添加密码登录策略

Gaokun Wang 1 month ago
parent
commit
a79dfb92fa

+ 2 - 2
eco-common/com-core/src/main/java/org/eco/vip/orm/exception/BusinessException.java

@@ -10,7 +10,7 @@ import cn.hutool.core.util.StrUtil;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import org.eco.vip.orm.exception.enums.BusinessErrorCode;
-import org.eco.vip.orm.utils.StringUtil;
+import org.eco.vip.orm.utils.StrUtils;
 
 /**
  * @description 业务逻辑异常
@@ -74,7 +74,7 @@ public final class BusinessException extends RuntimeException {
     public String getMessage() {
         if (StrUtil.isNotBlank(message)) {
             if (msgParams != null && msgParams.length > 0) {
-                return StringUtil.format(message, msgParams);
+                return StrUtils.format(message, msgParams);
             }
         }
         // 如果不传参数,直接返回

+ 1 - 1
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/JsonUtil.java → eco-common/com-core/src/main/java/org/eco/vip/orm/utils/JsonUtils.java

@@ -29,7 +29,7 @@ import java.util.List;
  * @date 2025/7/1 16:27
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public class JsonUtil extends JSONUtil {
+public class JsonUtils extends JSONUtil {
     private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class);
 
     public static ObjectMapper getObjectMapper() {

+ 10 - 0
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/SpringUtils.java

@@ -15,4 +15,14 @@ import cn.hutool.extra.spring.SpringUtil;
  * @date 2025/7/1 16:22
  */
 public final class SpringUtils extends SpringUtil {
+
+    /**
+     * 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
+     *
+     * @param name bean名称
+     * @return boolean
+     */
+    public static boolean containsBean(String name) {
+        return getBeanFactory().containsBean(name);
+    }
 }

+ 1 - 1
eco-common/com-core/src/main/java/org/eco/vip/orm/utils/StringUtil.java → eco-common/com-core/src/main/java/org/eco/vip/orm/utils/StrUtils.java

@@ -16,5 +16,5 @@ import lombok.extern.slf4j.Slf4j;
  * @date 2025/7/1 16:51
  */
 @Slf4j
-public class StringUtil extends StrUtil {
+public class StrUtils extends StrUtil {
 }

+ 2 - 2
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/auth/AuthController.java

@@ -12,7 +12,7 @@ import org.eco.vip.auth.domain.auth.vo.AuthParam;
 import org.eco.vip.auth.domain.auth.vo.AuthResponse;
 import org.eco.vip.auth.service.auth.IAdminAuthService;
 import org.eco.vip.orm.pojo.CommonResult;
-import org.eco.vip.orm.utils.JsonUtil;
+import org.eco.vip.orm.utils.JsonUtils;
 import org.eco.vip.orm.utils.ValidatorUtils;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -39,7 +39,7 @@ public class AuthController {
     @PostMapping("/login")
     @PermitAll
     public CommonResult<AuthResponse> login(@RequestBody String body) {
-        AuthParam authParamBody = JsonUtil.parseObject(body, AuthParam.class);
+        AuthParam authParamBody = JsonUtils.parseObject(body, AuthParam.class);
         // 校验参数
         ValidatorUtils.validate(authParamBody);
         return success(adminAuthService.login(authParamBody));

+ 18 - 3
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/domain/auth/vo/AuthResponse.java

@@ -11,8 +11,6 @@ import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
-import java.time.LocalDateTime;
-
 /**
  * @description AuthResponse
  *
@@ -25,11 +23,28 @@ import java.time.LocalDateTime;
 @Builder
 public class AuthResponse {
 
+    /**
+     * 用户编号
+     */
     private String userId;
 
+    /**
+     * 授权令牌
+     */
     private String accessToken;
 
+    /**
+     * 刷新令牌
+     */
     private String refreshToken;
 
-    private LocalDateTime expiresTime;
+    /**
+     * 过期时间
+     */
+    private Long expiresTime;
+
+    /**
+     * 刷新令牌 的有效期
+     */
+    private Long refreshExpire;
 }

+ 1 - 1
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/auth/AdminAuthService.java

@@ -25,7 +25,7 @@ public class AdminAuthService implements IAdminAuthService {
     private IUserService userService;
     @Override
     public AuthResponse login(AuthParam authParam) {
-        return null;
+        return IAuthStrategy.login(authParam, "password");
     }
 
 }

+ 47 - 0
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/auth/IAuthStrategy.java

@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.auth.service.auth;
+
+
+import org.eco.vip.auth.domain.auth.vo.AuthParam;
+import org.eco.vip.auth.domain.auth.vo.AuthResponse;
+import org.eco.vip.orm.exception.BusinessException;
+import org.eco.vip.orm.utils.SpringUtils;
+
+/**
+ * @description IAuthStrategy 授权类型
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 17:34
+ */
+public interface IAuthStrategy {
+    String BASE_NAME = "AuthStrategy";
+
+    /**
+     * 登录
+     *
+     * @param authParam      登录对象
+     * @param grantType 授权类型
+     * @return 登录验证信息
+     */
+    static AuthResponse login(AuthParam authParam, String grantType) {
+        // 授权类型和客户端id
+        String beanName = grantType + BASE_NAME;
+        if (!SpringUtils.containsBean(beanName)) {
+            throw new BusinessException("授权类型不正确!");
+        }
+        IAuthStrategy instance = SpringUtils.getBean(beanName);
+        return instance.login(authParam);
+    }
+
+    /**
+     * 登录
+     *
+     * @param authParam   登录对象
+     * @return 登录验证信息
+     */
+    AuthResponse login(AuthParam authParam);
+}

+ 29 - 0
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/auth/PasswordAuthStrategy.java

@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ *
+ */
+
+package org.eco.vip.auth.service.auth;
+
+
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.eco.vip.auth.domain.auth.vo.AuthParam;
+import org.eco.vip.auth.domain.auth.vo.AuthResponse;
+import org.springframework.stereotype.Service;
+
+/**
+ * @description PasswordAuthStrategy 密码登录策略实现类
+ *
+ * @author GaoKunW
+ * @date 2025/7/1 17:47
+ */
+@Slf4j
+@Service("password" + IAuthStrategy.BASE_NAME)
+@RequiredArgsConstructor
+public class PasswordAuthStrategy implements IAuthStrategy {
+    @Override
+    public AuthResponse login(AuthParam authParam) {
+        return null;
+    }
+}