记得上下班打卡 | git大法好,push需谨慎

Commit 2a7b7c56 authored by 张国柄's avatar 张国柄

鉴权调整;

parent b34ae040
......@@ -24,6 +24,11 @@
<artifactId>liquidnet-common-cache-redis</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.liquidnet</groupId>
<artifactId>liquidnet-api-feign-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
package com.liquidnet.common.web.filter;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.core.JwtValidator;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.commons.lang.util.DESUtils;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.feign.auth.rsc.FeignAuthorityClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.DigestUtils;
......@@ -25,12 +25,12 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
@Slf4j
@Data
@Component
@ConfigurationProperties(prefix = "global-auth")
public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
private List<String> includeUrlPattern;
private static final Logger log = LoggerFactory.getLogger(GlobalAuthorityInterceptor.class);
//private List<String> includeUrlPattern;
private List<String> excludeUrlPattern;
private static final String CONTENT_TYPE = "application/json;charset=utf-8";
......@@ -38,16 +38,12 @@ public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
private static final String TOKEN_KICK = "40002";
private static final String TOKEN_INVALID = "40003";
// private static final String KYLIN_STATION_JWT_VALID = "/*/station/**";
private final static AntPathMatcher antPathMatcher = new AntPathMatcher();
@Autowired
Environment env;
@Autowired
JwtValidator jwtValidator;
@Autowired
RedisUtil redisUtil;
FeignAuthorityClient feignAuthorityClient;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
......@@ -68,11 +64,9 @@ public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
request.setAttribute(CurrentUtil.uTag, JsonUtils.toJson(claims));
} catch (ExpiredJwtException expiredJwtEx) {
responseCode = TOKEN_INVALID;
log.error("Ex.ExpiredJwtException:{},responseCode:{}", expiredJwtEx.getMessage(), responseCode);
log.error("Ex.ExpiredJwtException:{},responseCode:{}", expiredJwtEx.getMessage(), responseCode = TOKEN_INVALID);
} catch (Exception ex) {
responseCode = TOKEN_ILLEGAL;
log.error("Ex.Exception:{},responseCode:{}", ex.getMessage(), responseCode);
log.error("Ex.Exception:{},responseCode:{}", ex.getMessage(), responseCode = TOKEN_ILLEGAL);
}
} else {
responseCode = TOKEN_ILLEGAL;
......@@ -84,13 +78,11 @@ public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
}
}
if (StringUtils.isNotEmpty(responseCode)) {
this.responseHandler(response, responseCode);
log.warn("Authority failed:{},uri:[{}],authorization:{}", responseCode, uri, authorization);
return false;
return this.responseHandlerRefuse(response, responseCode);
}
if (StringUtils.isEmpty(currentUid)) {
this.responseHandler(response, TOKEN_ILLEGAL);
return false;
return this.responseHandlerRefuse(response, TOKEN_ILLEGAL);
}
if (this.authorityHandler(response, uri, token, currentUid, claims)) {
return true;
......@@ -98,6 +90,10 @@ public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
return false;
}
public void setExcludeUrlPattern(List<String> excludeUrlPattern) {
this.excludeUrlPattern = excludeUrlPattern;
}
private void responseHandler(HttpServletResponse response, String responseCode) throws IOException {
ResponseDto<Object> responseDto = ResponseDto.failure(ErrorMapping.get(responseCode));
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
......@@ -106,44 +102,12 @@ public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
response.getWriter().write(JsonUtils.toJson(responseDto));
}
private boolean authorityHandler(HttpServletResponse response, String uri, String token, String currentUid, Claims claims) throws IOException {
// if (antPathMatcher.match(KYLIN_STATION_JWT_VALID, uri)) {// 专业版APP
// // adam:identity:sso:${uid}:MD5(${token})=${1-在线|0-离线}
// String ssoUidM5TokenKey = jwtValidator.getMsoRedisKey()
// .concat(currentUid).concat(":").concat(DigestUtils.md5DigestAsHex(token.getBytes(StandardCharsets.UTF_8)));
// Integer online = (Integer) redisUtil.get(ssoUidM5TokenKey);
// if (null == online || online != 1) {
// // 已离线
// this.responseHandler(response, TOKEN_INVALID);
//
// return false;
// } else {
// return true;
// }
// } else {
// // adam:identity:sso:${uid}=MD5(${token})
// String ssoKey = jwtValidator.getSsoRedisKey().concat(currentUid), md5Token;
//
// if (StringUtils.isEmpty(md5Token = (String) redisUtil.get(ssoKey))) {
// // 已离线
// this.responseHandler(response, TOKEN_INVALID);
//
// return false;
// } else {
// // 与在线TOKEN比对
// if (md5Token.equals(DigestUtils.md5DigestAsHex(token.getBytes(StandardCharsets.UTF_8)))) {
// // 一致则放行
// return true;
// } else {
// // 不一致则被踢下线
// this.responseHandler(response, TOKEN_KICK);
//
// return false;
// }
// }
// }
private boolean responseHandlerRefuse(HttpServletResponse response, String responseCode) throws IOException {
this.responseHandler(response, responseCode);
return false;
}
/*private boolean authorityHandler(HttpServletResponse response, String uri, String token, String currentUid, Claims claims) throws IOException {
String tokenType = (String) claims.get(CurrentUtil.TOKEN_TYPE);
switch (tokenType) {
case CurrentUtil.TOKEN_TYPE_VAL_STATION:// 专业版APP
......@@ -185,5 +149,52 @@ public class GlobalAuthorityInterceptor extends HandlerInterceptorAdapter {
this.responseHandler(response, TOKEN_ILLEGAL);
return false;
}
}*/
private boolean authorityHandler(HttpServletResponse response, String uri, String token, String currentUid, Claims claims) throws IOException {
String tokenType = (String) claims.get(CurrentUtil.TOKEN_TYPE);
switch (tokenType) {
case CurrentUtil.TOKEN_TYPE_VAL_STATION:// [专业版APP] adam:identity:sso:${uid}:MD5(${token})=${1-在线|0-离线}
String ssoUidM5TokenKey = jwtValidator.getMsoRedisKey()
.concat(currentUid).concat(":").concat(DigestUtils.md5DigestAsHex(token.getBytes(StandardCharsets.UTF_8)));
String val = this.getAccessToken(ssoUidM5TokenKey);
Integer online = null == val ? null : Integer.valueOf(val);
return null != online && online == 1 || this.responseHandlerRefuse(response, TOKEN_INVALID);
case CurrentUtil.TOKEN_TYPE_VAL_USER:// adam:identity:sso:${uid}=MD5(${token})
String ssoKey = jwtValidator.getSsoRedisKey().concat(currentUid);
String md5Token = this.getAccessToken(ssoKey);
return StringUtils.isEmpty(md5Token) ? this.responseHandlerRefuse(response, TOKEN_INVALID)
: (md5Token.equals(DigestUtils.md5DigestAsHex(token.getBytes(StandardCharsets.UTF_8))) || this.responseHandlerRefuse(response, TOKEN_KICK));
default:
log.warn("Authority failed:{} (Unknown token type).uri:[{}],token:{}", TOKEN_ILLEGAL, uri, token);
return this.responseHandlerRefuse(response, TOKEN_ILLEGAL);
}
}
/**
* 查取服务器令牌票据
*
* @param ssokey 用户令牌KEY
* @return String 令牌票据
*/
public String getAccessToken(String ssokey) {
String val = null;
try {
ResponseDto<String> check = feignAuthorityClient.check(DESUtils.DES().encrypt(ssokey));
if (check.isSuccess()) {
String valEncrypt = check.getData();
if (!StringUtils.isEmpty(valEncrypt)) {
val = DESUtils.DES().decrypt(valEncrypt);
}
}
} catch (Exception e) {
log.error("Authority.check exception", e);
}
return val;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>liquidnet-bus-feign</artifactId>
<groupId>com.liquidnet</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>liquidnet-api-feign-auth</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package com.liquidnet.service.feign.auth.rsc;
import com.liquidnet.service.base.ResponseDto;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Component
@FeignClient(name = "liquidnet-service-adam",
contextId = "FeignAuthorityClient", path = "adam",
url = "${liquidnet.service.adam.url}",
fallback = FallbackFactory.Default.class)
public interface FeignAuthorityClient {
@PostMapping("ath/check")
ResponseDto<String> check(@RequestParam(value = "seal") String seal);
}
......@@ -11,13 +11,14 @@
<artifactId>liquidnet-bus-feign</artifactId>
<packaging>pom</packaging>
<modules>
<module>liquidnet-api-feign-auth</module>
<module>liquidnet-api-feign-adam</module>
<module>liquidnet-api-feign-kylin</module>
<module>liquidnet-api-feign-dragon</module>
<module>liquidnet-api-feign-platform</module>
<module>liquidnet-api-feign-sweet</module>
<module>liquidnet-api-feign-chime</module>
<!-- <module>liquidnet-api-feign-sequence</module>-->
<!-- <module>liquidnet-api-feign-sequence</module>-->
<!-- <module>liquidnet-api-feign-example</module>-->
<!-- <module>liquidnet-api-feign-account</module>-->
<!-- <module>liquidnet-api-feign-bank</module>-->
......
package com.liquidnet.service.adam.config;
import com.liquidnet.common.web.config.WebMvcConfig;
import com.liquidnet.common.web.filter.GlobalAuthorityInterceptor;
import com.liquidnet.service.adam.interceptor.AdamAuthorityInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
......@@ -9,11 +9,11 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@Configuration
public class AdamWebMvcConfig extends WebMvcConfig {
@Autowired
GlobalAuthorityInterceptor globalAuthorityInterceptor;
AdamAuthorityInterceptor adamAuthorityInterceptor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(globalAuthorityInterceptor).addPathPatterns("/**");
registry.addInterceptor(adamAuthorityInterceptor).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
package com.liquidnet.service.adam.controller;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.util.DESUtils;
import com.liquidnet.service.base.ResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("ath")
public class GlobalAuthorityController {
@Autowired
RedisUtil redisUtil;
@PostMapping("check")
public ResponseDto<String> check(@RequestParam String seal) {
try {
String sealDecrypt = DESUtils.DES().decrypt(seal);
Object valObj = redisUtil.get(sealDecrypt);
String valEncrypt = null;
if (null != valObj) {
if (valObj instanceof String) {
valEncrypt = DESUtils.DES().encrypt((String) valObj);
} else if (valObj instanceof Integer) {
valEncrypt = DESUtils.DES().encrypt(valObj.toString());
}
}
return ResponseDto.success(valEncrypt);
} catch (Exception e) {
log.error("auth.check decrypt exception", e);
return ResponseDto.failure("Invalid seal");
}
}
}
package com.liquidnet.service.adam.interceptor;
import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.common.web.filter.GlobalAuthorityInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AdamAuthorityInterceptor extends GlobalAuthorityInterceptor {
private static final Logger log = LoggerFactory.getLogger(GlobalAuthorityInterceptor.class);
@Autowired
RedisUtil redisUtil;
@Override
public String getAccessToken(String ssokey) {
String val = null;
try {
val = (String) redisUtil.get(ssokey);
} catch (Exception e) {
log.error("Authority.check exception", e);
}
return val;
}
}
......@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
......@@ -13,6 +14,7 @@ import java.util.Arrays;
@EnableAsync
@Slf4j
@EnableFeignClients
@SpringBootApplication(scanBasePackages = {"com.liquidnet"})
public class ServiceKylinApplication implements CommandLineRunner {
@Autowired
......
......@@ -5,12 +5,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.core.env.Environment;
import java.net.InetAddress;
import java.util.Arrays;
@Slf4j
@EnableFeignClients
@SpringBootApplication(scanBasePackages = {"com.liquidnet"})
public class ServiceOrderApplication implements CommandLineRunner {
@Autowired
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment