wuxw
2019-04-25 d4e1929dcab147030d3bcae89b1801250fd6a5da
java110-core/src/main/java/com/java110/core/factory/AuthenticationFactory.java
@@ -15,8 +15,10 @@
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.core.context.ApiDataFlow;
import com.java110.core.context.DataFlow;
import org.apache.commons.codec.digest.DigestUtils;
@@ -27,10 +29,7 @@
import java.security.*;
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;
import java.util.*;
/**
 *
@@ -38,6 +37,16 @@
 * Created by wuxw on 2018/4/23.
 */
public class AuthenticationFactory {
    private final static String PASSWD_SALT= "hc@java110";
    /**
     * 用户密码 md5签名
     * @param inStr
     * @return
     */
    public static String passwdMd5(String inStr) throws NoAuthorityException{
        return md5(md5(inStr+PASSWD_SALT));
    }
    /**
     * md5签名
@@ -61,7 +70,26 @@
        if(dataFlow == null){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"MD5签名过程中出现错误");
        }
        String reqInfo = dataFlow.getTransactionId() + dataFlow.getAppId() + dataFlow.getReqBusiness().toJSONString()+dataFlow.getAppRoutes().get(0).getSecurityCode();
        String reqInfo = dataFlow.getTransactionId() +dataFlow.getAppId();
        reqInfo +=  ((dataFlow.getReqBusiness() == null || dataFlow.getReqBusiness().size() == 0)
                                            ?dataFlow.getReqData() :dataFlow.getReqBusiness().toJSONString());
        reqInfo += dataFlow.getAppRoutes().get(0).getSecurityCode();
        return md5(reqInfo);
    }
    /**
     * dataFlow 对象签名
     * @param dataFlow
     * @return
     */
    public static String apiDataFlowMd5(ApiDataFlow dataFlow) throws NoAuthorityException{
        if(dataFlow == null){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"MD5签名过程中出现错误");
        }
        String reqInfo = dataFlow.getTransactionId() + dataFlow.getRequestTime() + dataFlow.getAppId();
        reqInfo +=  "GET,DELETE".equals(dataFlow.getRequestHeaders().get(CommonConstant.HTTP_METHOD))?
                dataFlow.getRequestHeaders().get("REQUEST_URL") :dataFlow.getReqData();
        reqInfo += dataFlow.getAppRoutes().get(0).getSecurityCode();
        return md5(reqInfo);
    }
@@ -91,6 +119,20 @@
        }else {
            orders.put("sign", AuthenticationFactory.md5(orders.getString("transactionId"), orders.getString("responseTime"),
                    business == null ?"":business.toJSONString(), dataFlow.getAppRoutes().get(0).getSecurityCode()));
        }
    }
    /**
     * 添加 sign
     * @param dataFlow
     * @param headers
     */
    public static void putSign(DataFlow dataFlow,Map<String,String> headers){
        if(dataFlow == null || dataFlow.getAppRoutes() == null || dataFlow.getAppRoutes().size() == 0 || StringUtil.isNullOrNone(dataFlow.getAppRoutes().get(0).getSecurityCode())) {
            headers.put("resSign","");
        }else {
            headers.put("resSign", AuthenticationFactory.md5(dataFlow.getTransactionId(), headers.get("responseTime"),
                    dataFlow.getResData(), dataFlow.getAppRoutes().get(0).getSecurityCode()));
        }
    }
@@ -284,6 +326,28 @@
    }
    /**
     * 删除Token
     * @param token
     * @return
     * @throws Exception
     */
    public static void deleteToken(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)){ //说明redis中jdi 已经失效
            JWTCache.removeValue(jdi);
        }
    }
    /**
     * 校验Token
     * @param token
     * @return