xujunwei пре 3 година
родитељ
комит
6fc8c54310

+ 0 - 38
framework-starter/mrxu-starter-redisson/src/main/java/com/mrxu/framework/starter/token/JwtLoginConfig.java

@@ -1,38 +0,0 @@
-package com.mrxu.framework.starter.token;
-
-import lombok.Data;
-import org.springframework.context.annotation.Configuration;
-
-
-@Data
-@Configuration
-public class JwtLoginConfig {
-
-    /**
-     * jwt认证加密私钥(Base64加密)
-     */
-    private String encryptJWTKey = "U0JBUElKV1RkV2FuZzkyNjQ1NA==";
-
-    /**
-     * @Author: xujunwei
-     * @Date: 2021/8/23
-     * @Description: 不需要登录的url
-     */
-    private String unLoginUrl = "";
-
-    /**
-     * @Author: xujunwei
-     * @Date: 2021/8/23
-     * @Description: session有效期默认30分钟,如果30分钟无任何操作则session过期
-     */ 
-    private long SESSION_EXPIRE_TIME = 1000l*60*30;
-
-    /**
-     * @Author: xujunwei
-     * @Date: 2021/8/23
-     * @Description: token有效时长默认20秒,操过20秒则重新生成
-     */
-    private long TOKEN_EXPIRE_TIME = 1000l*20;
-
-
-}

+ 0 - 64
framework-starter/mrxu-starter-redisson/src/main/java/com/mrxu/framework/starter/token/JwtUtil.java

@@ -1,64 +0,0 @@
-package com.mrxu.framework.starter.token;
-
-import com.auth0.jwt.JWT;
-import com.auth0.jwt.JWTCreator;
-import com.auth0.jwt.JWTVerifier;
-import com.auth0.jwt.algorithms.Algorithm;
-import com.auth0.jwt.interfaces.DecodedJWT;
-import com.mrxu.framework.common.util.BusinessException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.UnsupportedEncodingException;
-import java.util.Date;
-
-
-public class JwtUtil {
-
-    private static Logger logger = LoggerFactory.getLogger(JwtUtil.class);
-
-    public static String sign(String key,String value,long expireTime,String secret) {
-        try {
-            Date date = new Date(System.currentTimeMillis() + expireTime);
-            Algorithm algorithm = Algorithm.HMAC256(secret);
-            JWTCreator.Builder builder = JWT.create();
-            builder.withClaim(key,value);
-            builder.withExpiresAt(date);
-            return builder.sign(algorithm);
-        }
-        catch (UnsupportedEncodingException e) {
-            logger.error("JWT 密码 {} 编码错误:{}",secret,e);
-            throw new BusinessException("错误编码错误");
-        }
-
-    }
-
-    public static void verify(String token,String secret) {
-        try {
-            Algorithm algorithm = Algorithm.HMAC256(secret);
-            JWTVerifier verifier = JWT.require(algorithm).build();
-            verifier.verify(token);
-        }
-        catch (UnsupportedEncodingException e) {
-            logger.error("JWT 密码 {} 编码错误:{}",secret,e);
-            throw new BusinessException("错误编码错误");
-        }
-    }
-
-    public static String getValue(String token, String key) {
-        DecodedJWT jwt = JWT.decode(token);
-        return jwt.getClaim(key).asString();
-    }
-
-
-
-    public static void main(String[] args) {
-        String token = sign("memberId","92341398766528923",
-                10*1000,"mrxu");
-        logger.info("生成token:{}",token);
-        verify(token,"mrxu");
-        String value = getValue(token,"memberId");
-        logger.info("获取token值:{}",value);
-    }
-
-}

+ 0 - 75
framework-starter/mrxu-starter-redisson/src/main/java/com/mrxu/framework/starter/token/SecurityFilter.java

@@ -1,75 +0,0 @@
-package com.mrxu.framework.starter.token;
-
-import com.auth0.jwt.exceptions.TokenExpiredException;
-import com.mrxu.framework.boot.entity.RpcRequestInfo;
-import com.mrxu.framework.common.util.StrFunc;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.server.reactive.ServerHttpRequest;
-import org.springframework.util.AntPathMatcher;
-import org.springframework.util.PathMatcher;
-import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
-import org.springframework.web.server.ServerWebExchange;
-import org.springframework.web.server.WebFilterChain;
-import reactor.core.publisher.Mono;
-
-import javax.annotation.PostConstruct;
-import java.net.URI;
-
-public class SecurityFilter extends HiddenHttpMethodFilter {
-
-    public static final String TOKEN_KEY = "token";
-
-    @Autowired
-    private JwtLoginConfig jtbAuthProperties;
-
-    @Autowired
-    private TokenManager tokenManager;
-
-    private String[] unLoginUrl;
-
-    private final PathMatcher pathMatcher = new AntPathMatcher();
-
-    @PostConstruct
-    private void init() {
-        if(StrFunc.isNotEmpty(jtbAuthProperties.getUnLoginUrl())) {
-            unLoginUrl = jtbAuthProperties.getUnLoginUrl().split(",");
-        }
-    }
-
-    @Override
-    public Mono filter(ServerWebExchange exchange, WebFilterChain chain) {
-        ServerHttpRequest request = exchange.getRequest();
-        URI uri = request.getURI();
-        String path = uri.getPath();
-        if(!needLogin(path)) {
-            return chain.filter(exchange);
-        }
-        String tokenStr = request.getHeaders().getFirst(TOKEN_KEY);
-        try {
-            String userId = tokenManager.getUserId(tokenStr);
-            RpcRequestInfo.get().setUsername(userId);
-            return null;
-        } catch (TokenExpiredException e) {
-            return null;
-        }
-    }
-
-    private String getNewToken() {
-        return null;
-    }
-
-    /**
-     * @Author: xujunwei
-     * @Date: 2021/8/23
-     * @Description: 判断url是否需要登录
-     */
-    private boolean needLogin(String path) {
-        for (String url : unLoginUrl) {
-            if (pathMatcher.match(url, path)) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-}

+ 24 - 0
framework-starter/mrxu-starter-redisson/src/main/java/com/mrxu/framework/starter/token/Token.java

@@ -0,0 +1,24 @@
+package com.mrxu.framework.starter.token;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+@ApiModel("系统token")
+public class Token implements Serializable {
+
+    // 有效期单位秒
+    private Integer effectTime;
+
+    // redis key
+    private String key;
+
+    // 来源
+    private String source;
+
+    // token 值
+    private String value;
+
+}

+ 0 - 26
framework-starter/mrxu-starter-redisson/src/main/java/com/mrxu/framework/starter/token/TokenManager.java

@@ -1,26 +0,0 @@
-package com.mrxu.framework.starter.token;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-@Component
-public class TokenManager {
-
-    public static final String USER_KEY = "userId";
-
-    @Autowired
-    private JwtLoginConfig jtbAuthProperties;
-
-    public String createToken(String userKey) {
-        return JwtUtil.sign(USER_KEY,userKey,
-                jtbAuthProperties.getTOKEN_EXPIRE_TIME(),
-                jtbAuthProperties.getEncryptJWTKey());
-    }
-
-    public String getUserId(String token) {
-        JwtUtil.verify(token,jtbAuthProperties.getEncryptJWTKey());
-        return JwtUtil.getValue(token,USER_KEY);
-    }
-
-
-}

+ 34 - 0
framework-starter/mrxu-starter-redisson/src/main/java/com/mrxu/framework/starter/token/TokenService.java

@@ -0,0 +1,34 @@
+package com.mrxu.framework.starter.token;
+
+import com.mrxu.framework.common.util.MrxuId;
+import com.mrxu.framework.starter.redisson.RedisCache;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
+@Slf4j
+public class TokenService {
+
+    private final RedisCache redisCache;
+
+    private final  String region = "token";
+
+    public Token create(String source, String value, Integer effectTime) {
+        Token token = new Token();
+        token.setKey(MrxuId.getId());
+        token.setSource(source);
+        token.setValue(value);
+        token.setEffectTime(effectTime);
+        log.debug("key:{},value:{}",token.getKey(),token.getValue());
+        redisCache.set(region,token.getKey(),token,effectTime+1);
+        return token;
+    }
+
+    public Token getToken(String tokenKey) {
+        return redisCache.get(region,tokenKey);
+    }
+
+}