Procházet zdrojové kódy

修改获取token方式

bihuisong před 1 rokem
rodič
revize
24e2f11fca

+ 19 - 16
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SecurityController.java

@@ -1,18 +1,23 @@
 package com.ruoyi.web.controller.system;
 
 
+import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.Result;
+import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.framework.jwt.utils.JwtUtils;
 import com.ruoyi.system.domain.TokenRequest;
+import com.ruoyi.system.service.ISysUserService;
 import com.ruoyi.web.controller.tool.RsaUtil;
 import com.ruoyi.web.controller.tool.SecretKeyBo;
 import com.ruoyi.web.controller.tool.StringUtils;
 import com.ruoyi.common.redis.RedisService;
 import org.apache.shiro.authc.AuthenticationException;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import javax.annotation.Resource;
 
 import java.security.NoSuchAlgorithmException;
+import java.util.Objects;
 import java.util.concurrent.TimeUnit;
 
 
@@ -23,7 +28,8 @@ public class SecurityController {
 
     @Resource
     private RedisService redisService;
-
+    @Autowired
+    private ISysUserService userService;
 
     /**
      * 获取秘钥接口
@@ -52,28 +58,25 @@ public class SecurityController {
      * 获取token
      */
     @PostMapping("/getToken")
-    public Result<?> getToken(@RequestBody TokenRequest form) {
-        Result<Object> result = new Result<>();
+    @ResponseBody
+    public AjaxResult getToken(@RequestBody TokenRequest form) {
         //解密密码字符串
-//        String privateKey = redisService.getCacheObject("remoteSecretKey");
-//        try {
-//            form.setPassword(RsaUtil.decrypt(form.getPassword(), privateKey));
-//        } catch (Exception e) {
-//            result.setCode(40002);
-//            result.setMsg("获取token失败");
-//            return result;
-//        }
-        String token = JwtUtils.createToken(form.getUsername(), form.getPassword());
+        String privateKey = redisService.getCacheObject("remoteSecretKey");
+        try {
+            RsaUtil.decrypt(form.getUsername(), privateKey);
+        } catch (Exception e) {
+            return AjaxResult.error("获取token失败").put("data", null).put("code",40002);
+        }
+        SysUser user = userService.selectUserByLoginName(form.getUsername());
+        String token = JwtUtils.createToken(form.getUsername(), user.getPassword());
         try {
-            return Result.ok(token);
+            return Objects.requireNonNull(AjaxResult.success("获取成功").put("data", token)).put("code",200);
         } catch (AuthenticationException e) {
             String msg = "用户或密码错误";
             if (com.ruoyi.common.utils.StringUtils.isNotEmpty(e.getMessage())) {
                 msg = e.getMessage();
             }
-            result.setCode(40000);
-            result.setMsg(msg);
-            return result;
+            return AjaxResult.error(msg).put("data", null).put("code",40000);
         }
     }
 

+ 46 - 54
ruoyi-common/src/main/java/com/ruoyi/common/core/domain/AjaxResult.java

@@ -2,6 +2,7 @@ package com.ruoyi.common.core.domain;
 
 import java.util.HashMap;
 import java.util.Objects;
+
 import com.ruoyi.common.utils.StringUtils;
 
 /**
@@ -9,39 +10,47 @@ import com.ruoyi.common.utils.StringUtils;
  *
  * @author ruoyi
  */
-public class AjaxResult extends HashMap<String, Object>
-{
+public class AjaxResult extends HashMap<String, Object> {
     private static final long serialVersionUID = 1L;
 
-    /** 状态码 */
+    /**
+     * 状态码
+     */
     public static final String CODE_TAG = "code";
 
-    /** 返回内容 */
+    /**
+     * 返回内容
+     */
     public static final String MSG_TAG = "msg";
 
-    /** 数据对象 */
+    /**
+     * 数据对象
+     */
     public static final String DATA_TAG = "data";
 
     /**
      * 状态类型
      */
-    public enum Type
-    {
-        /** 成功 */
+    public enum Type {
+        /**
+         * 成功
+         */
         SUCCESS(0),
-        /** 警告 */
+        /**
+         * 警告
+         */
         WARN(301),
-        /** 错误 */
+        /**
+         * 错误
+         */
         ERROR(500);
         private final int value;
 
-        Type(int value)
-        {
+        Type(int value) {
             this.value = value;
         }
 
-        public int value()
-        {
+        public int value() {
             return this.value;
         }
     }
@@ -49,18 +58,16 @@ public class AjaxResult extends HashMap<String, Object>
     /**
      * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
      */
-    public AjaxResult()
-    {
+    public AjaxResult() {
     }
 
     /**
      * 初始化一个新创建的 AjaxResult 对象
      *
      * @param type 状态类型
-     * @param msg 返回内容
+     * @param msg  返回内容
      */
-    public AjaxResult(Type type, String msg)
-    {
+    public AjaxResult(Type type, String msg) {
         super.put(CODE_TAG, type.value);
         super.put(MSG_TAG, msg);
     }
@@ -69,15 +76,13 @@ public class AjaxResult extends HashMap<String, Object>
      * 初始化一个新创建的 AjaxResult 对象
      *
      * @param type 状态类型
-     * @param msg 返回内容
+     * @param msg  返回内容
      * @param data 数据对象
      */
-    public AjaxResult(Type type, String msg, Object data)
-    {
+    public AjaxResult(Type type, String msg, Object data) {
         super.put(CODE_TAG, type.value);
         super.put(MSG_TAG, msg);
-        if (StringUtils.isNotNull(data))
-        {
+        if (StringUtils.isNotNull(data)) {
             super.put(DATA_TAG, data);
         }
     }
@@ -87,8 +92,7 @@ public class AjaxResult extends HashMap<String, Object>
      *
      * @return 成功消息
      */
-    public static AjaxResult success()
-    {
+    public static AjaxResult success() {
         return AjaxResult.success("操作成功");
     }
 
@@ -97,8 +101,7 @@ public class AjaxResult extends HashMap<String, Object>
      *
      * @return 成功消息
      */
-    public static AjaxResult success(Object data)
-    {
+    public static AjaxResult success(Object data) {
         return AjaxResult.success("操作成功", data);
     }
 
@@ -108,20 +111,18 @@ public class AjaxResult extends HashMap<String, Object>
      * @param msg 返回内容
      * @return 成功消息
      */
-    public static AjaxResult success(String msg)
-    {
+    public static AjaxResult success(String msg) {
         return AjaxResult.success(msg, null);
     }
 
     /**
      * 返回成功消息
      *
-     * @param msg 返回内容
+     * @param msg  返回内容
      * @param data 数据对象
      * @return 成功消息
      */
-    public static AjaxResult success(String msg, Object data)
-    {
+    public static AjaxResult success(String msg, Object data) {
         return new AjaxResult(Type.SUCCESS, msg, data);
     }
 
@@ -131,20 +132,18 @@ public class AjaxResult extends HashMap<String, Object>
      * @param msg 返回内容
      * @return 警告消息
      */
-    public static AjaxResult warn(String msg)
-    {
+    public static AjaxResult warn(String msg) {
         return AjaxResult.warn(msg, null);
     }
 
     /**
      * 返回警告消息
      *
-     * @param msg 返回内容
+     * @param msg  返回内容
      * @param data 数据对象
      * @return 警告消息
      */
-    public static AjaxResult warn(String msg, Object data)
-    {
+    public static AjaxResult warn(String msg, Object data) {
         return new AjaxResult(Type.WARN, msg, data);
     }
 
@@ -153,8 +152,7 @@ public class AjaxResult extends HashMap<String, Object>
      *
      * @return
      */
-    public static AjaxResult error()
-    {
+    public static AjaxResult error() {
         return AjaxResult.error("操作失败");
     }
 
@@ -164,20 +162,18 @@ public class AjaxResult extends HashMap<String, Object>
      * @param msg 返回内容
      * @return 警告消息
      */
-    public static AjaxResult error(String msg)
-    {
+    public static AjaxResult error(String msg) {
         return AjaxResult.error(msg, null);
     }
 
     /**
      * 返回错误消息
      *
-     * @param msg 返回内容
+     * @param msg  返回内容
      * @param data 数据对象
      * @return 警告消息
      */
-    public static AjaxResult error(String msg, Object data)
-    {
+    public static AjaxResult error(String msg, Object data) {
         return new AjaxResult(Type.ERROR, msg, data);
     }
 
@@ -186,8 +182,7 @@ public class AjaxResult extends HashMap<String, Object>
      *
      * @return 结果
      */
-    public boolean isSuccess()
-    {
+    public boolean isSuccess() {
         return Objects.equals(Type.SUCCESS.value, this.get(CODE_TAG));
     }
 
@@ -196,8 +191,7 @@ public class AjaxResult extends HashMap<String, Object>
      *
      * @return 结果
      */
-    public boolean isWarn()
-    {
+    public boolean isWarn() {
         return Objects.equals(Type.WARN.value, this.get(CODE_TAG));
     }
 
@@ -206,21 +200,19 @@ public class AjaxResult extends HashMap<String, Object>
      *
      * @return 结果
      */
-    public boolean isError()
-    {
+    public boolean isError() {
         return Objects.equals(Type.ERROR.value, this.get(CODE_TAG));
     }
 
     /**
      * 方便链式调用
      *
-     * @param key 键
+     * @param key   
      * @param value 值
      * @return 数据对象
      */
     @Override
-    public AjaxResult put(String key, Object value)
-    {
+    public AjaxResult put(String key, Object value) {
         super.put(key, value);
         return this;
     }

+ 12 - 24
ruoyi-framework/src/main/java/com/ruoyi/framework/jwt/utils/JwtUtils.java

@@ -1,6 +1,7 @@
 package com.ruoyi.framework.jwt.utils;
 
 import java.util.Date;
+
 import com.auth0.jwt.JWT;
 import com.auth0.jwt.JWTVerifier;
 import com.auth0.jwt.algorithms.Algorithm;
@@ -11,55 +12,42 @@ import com.auth0.jwt.interfaces.DecodedJWT;
 
 /**
  * jwt 工具类
- * 
+ *
  * @author ruoyi
  */
-public class JwtUtils
-{
+public class JwtUtils {
     private static final long EXPIRE_TIME = 30 * 60 * 1000;
 
     private static final String CLAIM_NAME = "username";
 
-    public static String createToken(String username, String password)
-    {
+    public static String createToken(String username, String password) {
         return createToken(username, password, EXPIRE_TIME);
     }
 
-    public static String createToken(String username, String password, long expireTime)
-    {
+    public static String createToken(String username, String password, long expireTime) {
         Date date = new Date(System.currentTimeMillis() + expireTime);
         // 加密处理密码
         Algorithm algorithm = Algorithm.HMAC256(password);
         return JWT.create().withClaim(CLAIM_NAME, username).withExpiresAt(date).sign(algorithm);
     }
 
-    public static void verify(String username, String dbPwd, String token)
-    {
+    public static void verify(String username, String dbPwd, String token) {
         Algorithm algorithm = Algorithm.HMAC256(dbPwd);
         JWTVerifier jwtVerifier = JWT.require(algorithm).withClaim(CLAIM_NAME, username).build();
-        try
-        {
+        try {
             jwtVerifier.verify(token);
-        }
-        catch (TokenExpiredException e)
-        {
+        } catch (TokenExpiredException e) {
             throw new TokenExpiredException("token已过期");
-        }
-        catch (JWTVerificationException e)
-        {
+        } catch (JWTVerificationException e) {
             throw new JWTVerificationException("token验证失败");
         }
     }
 
-    public static String getUserName(String token)
-    {
-        try
-        {
+    public static String getUserName(String token) {
+        try {
             DecodedJWT jwt = JWT.decode(token);
             return jwt.getClaim(CLAIM_NAME).asString();
-        }
-        catch (JWTDecodeException e)
-        {
+        } catch (JWTDecodeException e) {
             return null;
         }
     }