From 2ece6f6cd23d7b717881dd54a9fc74877096ecee Mon Sep 17 00:00:00 2001
From: wuxw7 <wuxw7@asiainfo.com>
Date: 星期一, 07 五月 2018 23:13:59 +0800
Subject: [PATCH] 控制服务实现登录功能 和菜单展示,以及中心服务bug修复
---
java110-common/src/main/java/com/java110/common/factory/AuthenticationFactory.java | 98 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 97 insertions(+), 1 deletions(-)
diff --git a/java110-common/src/main/java/com/java110/common/factory/AuthenticationFactory.java b/java110-common/src/main/java/com/java110/common/factory/AuthenticationFactory.java
index c3ba9de..bb99e99 100644
--- a/java110-common/src/main/java/com/java110/common/factory/AuthenticationFactory.java
+++ b/java110-common/src/main/java/com/java110/common/factory/AuthenticationFactory.java
@@ -2,10 +2,20 @@
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
+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.exceptions.JWTVerificationException;
+import com.auth0.jwt.interfaces.Claim;
+import com.auth0.jwt.interfaces.DecodedJWT;
+import com.java110.common.cache.JWTCache;
import com.java110.common.cache.MappingCache;
+import com.java110.common.constant.CommonConstant;
import com.java110.common.constant.MappingConstant;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.exception.NoAuthorityException;
+import com.java110.common.util.DateUtil;
import com.java110.common.util.StringUtil;
import com.java110.entity.center.DataFlow;
import org.apache.commons.codec.digest.DigestUtils;
@@ -18,6 +28,9 @@
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
/**
*
@@ -60,7 +73,7 @@
* @return
*/
public static String md5(String transactionId,String appId,String businesses,String code){
- return md5(transactionId+appId+businesses).toLowerCase();
+ return md5(transactionId+appId+businesses+code).toLowerCase();
}
/**
@@ -223,6 +236,89 @@
return keyPairGenerator.generateKeyPair();
}
+ /**
+ * 鐢ㄦ埛瀵嗙爜
+ * @param userPwd
+ * @return
+ */
+ public static String md5UserPassword(String userPwd){
+ String userPasswordSecret = MappingCache.getValue(MappingConstant.KEY_USER_PASSWORD_SECRET);
+ if(StringUtil.isNullOrNone(userPasswordSecret)){
+ userPasswordSecret = CommonConstant.DEFAULT_USER_PWD_SECRET;
+ }
+ return md5(md5(userPwd + userPasswordSecret));
+ }
+
+ /**
+ * 鍒涘缓token
+ * @return
+ */
+ public static String createAndSaveToken(Map<String,String> info) throws Exception{
+
+ if(!info.containsKey(CommonConstant.LOGIN_USER_ID)){
+ throw new InvalidParameterException("鍙傛暟涓病鏈夊寘鍚細"+CommonConstant.LOGIN_USER_ID);
+ }
+
+ String jdi = UUID.randomUUID().toString().replace("-","");
+ String jwtSecret = MappingCache.getValue(MappingConstant.KEY_JWT_SECRET);
+ if(StringUtil.isNullOrNone(jwtSecret)){
+ jwtSecret = CommonConstant.DEFAULT_JWT_SECRET;
+ }
+ Algorithm algorithm = Algorithm.HMAC256(jwtSecret);
+ JWTCreator.Builder jwt= JWT.create();
+ for(String key:info.keySet()){
+ if(CommonConstant.LOGIN_USER_ID.equals(key)){
+ continue;
+ }
+ jwt.withClaim(key,info.get(key));
+ }
+ String expireTime = MappingCache.getValue(MappingConstant.KEY_JWT_EXPIRE_TIME);
+ if(StringUtil.isNullOrNone(expireTime)){
+ expireTime = CommonConstant.DEFAULT_JWT_EXPIRE_TIME;
+ }
+ //淇濆瓨token Id
+ JWTCache.setValue(jdi,info.get(CommonConstant.LOGIN_USER_ID),Integer.parseInt(expireTime));
+ jwt.withIssuer("java110");
+ jwt.withJWTId(jdi);
+ return jwt.sign(algorithm);
+ }
+
+ /**
+ * 鏍¢獙Token
+ * @param token
+ * @return
+ * @throws Exception
+ */
+ public static Map<String, String> verifyToken(String token) throws Exception{
+ String jwtSecret = MappingCache.getValue(MappingConstant.KEY_JWT_SECRET);
+ if(StringUtil.isNullOrNone(jwtSecret)){
+ jwtSecret = CommonConstant.DEFAULT_JWT_SECRET;
+ }
+ Algorithm algorithm = Algorithm.HMAC256(jwtSecret);
+ JWTVerifier verifier = JWT.require(algorithm).withIssuer("java110").build();
+ DecodedJWT jwt = verifier.verify(token);
+ String jdi = jwt.getId();
+ //淇濆瓨token Id
+ String userId = JWTCache.getValue(jdi);
+ if(StringUtil.isNullOrNone(userId)){
+ throw new JWTVerificationException("鐢ㄦ埛杩樻湭鐧诲綍");
+ }
+ String expireTime = MappingCache.getValue(MappingConstant.KEY_JWT_EXPIRE_TIME);
+ if(StringUtil.isNullOrNone(expireTime)){
+ expireTime = CommonConstant.DEFAULT_JWT_EXPIRE_TIME;
+ }
+ //鍒锋柊杩囨椂鏃堕棿
+ JWTCache.resetExpireTime(jdi,Integer.parseInt(expireTime));
+ Map<String, Claim> claims = jwt.getClaims();
+ // Add the claim to request header
+ Map<String,String> paramOut = new HashMap<String, String>();
+ for(String key : claims.keySet()){
+ paramOut.put(key,claims.get(key).asString());
+ }
+ paramOut.put(CommonConstant.LOGIN_USER_ID,userId);
+ return paramOut;
+ }
+
/***********************************JWT start***************************************/
--
Gitblit v1.8.0