| | |
| | | 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; |
| | | |
| | |
| | | * 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签名 |
| | |
| | | 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); |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 删除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 |