1、feign配置排除健康检查、prometheus

2、user校验日志级别修改
This commit is contained in:
liuh
2022-03-03 10:16:06 +08:00
parent 5671a7e496
commit 7eaec0ffc1
4 changed files with 13 additions and 11 deletions

View File

@ -13,4 +13,6 @@ public interface Constants {
public static final String TOKEN_EXPIRED = "90401"; public static final String TOKEN_EXPIRED = "90401";
public static final String REMOTE_ACCESS_FAILURE = "90500"; public static final String REMOTE_ACCESS_FAILURE = "90500";
String ACTUATOR_HEALTH = "actuator/health";
String ACTUATOR_PROMETHEUS = "actuator/prometheus";
} }

View File

@ -57,7 +57,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
final FilterChain filterChain) throws ServletException, IOException { final FilterChain filterChain) throws ServletException, IOException {
String api = request.getRequestURI(); String api = request.getRequestURI();
String method = request.getMethod(); String method = request.getMethod();
if (!StringUtils.contains(api, "actuator/prometheus")) { if (!StringUtils.contains(api, ACTUATOR_PROMETHEUS)) {
log.info("--------" + method + " - " + api + "?" + Optional.ofNullable(request.getQueryString()).orElse("")); log.info("--------" + method + " - " + api + "?" + Optional.ofNullable(request.getQueryString()).orElse(""));
} }
// 清空上下文中的缓存信息, 防止二次请求时数据异常 (如此, 每次有新的请求进入都会进行token的验证) // 清空上下文中的缓存信息, 防止二次请求时数据异常 (如此, 每次有新的请求进入都会进行token的验证)
@ -87,8 +87,8 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
.findFirst()); .findFirst());
if (optionalCookie.isPresent()) { if (optionalCookie.isPresent()) {
setAuthentication(currentRoleCode, optionalCookie.get().getValue(), isWhite); setAuthentication(currentRoleCode, optionalCookie.get().getValue(), isWhite);
} else { } else if (!api.contains(ACTUATOR_HEALTH) && !api.contains(ACTUATOR_PROMETHEUS)) {
log.warn("cookie中没有token信息"); log.warn("cookie中没有token信息:{}",api);
} }
} }
@ -109,8 +109,8 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
private boolean checkWhiteList(String method, String methodType) { private boolean checkWhiteList(String method, String methodType) {
return Optional.ofNullable(allows.getApis()).orElseGet(ArrayList::new) return Optional.ofNullable(allows.getApis()).orElseGet(ArrayList::new)
.parallelStream().anyMatch(reg -> Pattern.compile(reg).matcher(methodType + "." + method).matches()) .parallelStream().anyMatch(reg -> Pattern.compile(reg).matcher(methodType + "." + method).matches())
|| method.contains("actuator/health") //服务的就绪检测 || method.contains(ACTUATOR_HEALTH) //服务的就绪检测
|| method.contains("actuator/prometheus"); //prometheus检测 || method.contains(ACTUATOR_PROMETHEUS); //prometheus检测
} }
@ -133,7 +133,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
private Authentication getAuthentication(final String token, final String currentRoleCode, private Authentication getAuthentication(final String token, final String currentRoleCode,
final boolean isWhite) { final boolean isWhite) {
BaseCacheUser userInfo = client.getUserInfo(token); BaseCacheUser userInfo = client.getUserInfo(token);
log.info("getUserInfo:{}",userInfo.toString()); log.debug("getUserInfo:{}",userInfo.toString());
// 对象为空, 则说明网络异常feign已熔断 // 对象为空, 则说明网络异常feign已熔断
if (Objects.isNull(userInfo)) { if (Objects.isNull(userInfo)) {
if (!isWhite) { if (!isWhite) {

View File

@ -41,7 +41,7 @@ public class UserInfoServiceImpl implements UserInfoService {
* @return * @return
*/ */
private BaseCacheUser convertToBusinessModel(SecurityEntity raw) { private BaseCacheUser convertToBusinessModel(SecurityEntity raw) {
log.info("userinfo: {}", raw); log.debug("userinfo: {}", raw);
// 对象为空, 则说明网络异常feign已熔断 // 对象为空, 则说明网络异常feign已熔断
if (Objects.isNull(raw)) { if (Objects.isNull(raw)) {
throw new RemoteTimeoutException(REMOTE_ACCESS_FAILURE); throw new RemoteTimeoutException(REMOTE_ACCESS_FAILURE);

View File

@ -53,7 +53,7 @@ public class FeignConfig implements RequestInterceptor {
public void apply(RequestTemplate template) { public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
log.info("--------feign url- " + template.url()); log.info("--------feign url- " + template.url());
String method = template.method();
if (Objects.nonNull(attributes)) { if (Objects.nonNull(attributes)) {
if (isNonExistsWhiteList(template.url())) { if (isNonExistsWhiteList(template.url())) {
injectToken(template, attributes); injectToken(template, attributes);
@ -65,9 +65,9 @@ public class FeignConfig implements RequestInterceptor {
template.removeHeader(CURRENT_ROLE_CODE); template.removeHeader(CURRENT_ROLE_CODE);
template.header(CURRENT_ROLE_CODE, currentRoleCode); template.header(CURRENT_ROLE_CODE, currentRoleCode);
} }
} { } else if (!method.contains(ACTUATOR_HEALTH) && !method.contains(ACTUATOR_PROMETHEUS)) {
String accessToken = getAccessToken(); String accessToken = getAccessToken();
log.info("token=======accessToken===" + accessToken); log.info("attributes is null=======accessToken===" + accessToken);
template.header(HttpHeaders.AUTHORIZATION, String.format("%s%s", TOKEN_PREFIX, accessToken)); template.header(HttpHeaders.AUTHORIZATION, String.format("%s%s", TOKEN_PREFIX, accessToken));
} }
} }
@ -115,7 +115,7 @@ public class FeignConfig implements RequestInterceptor {
template.header(AUTHORIZATION_HEADER, String.format("%s%s", TOKEN_PREFIX, authToken)); template.header(AUTHORIZATION_HEADER, String.format("%s%s", TOKEN_PREFIX, authToken));
} else { } else {
String accessToken = getAccessToken(); String accessToken = getAccessToken();
log.info("token=======accessToken==="+accessToken); log.info("no Token no Cookie=======accessToken==="+accessToken);
template.header(HttpHeaders.AUTHORIZATION,String.format("%s%s", TOKEN_PREFIX, accessToken)); template.header(HttpHeaders.AUTHORIZATION,String.format("%s%s", TOKEN_PREFIX, accessToken));
} }
} }