修正了common包增加cookie连接的bug

This commit is contained in:
ajaxfan
2021-03-24 17:03:20 +08:00
parent 28a6e599d7
commit 15de420589
5 changed files with 46 additions and 41 deletions

View File

@ -1,18 +1,21 @@
package com.chinaunicom.mall.ebtp.common.config;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.AUTHORIZATION_HEADER;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.CURRENT_ROLE_CODE;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.TOKEN_PREFIX;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.COOKIE_TOKEN_CODE;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.Cookie;
import java.util.Arrays;
import java.util.Optional;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.*;
import feign.RequestInterceptor;
import feign.RequestTemplate;
/**
* 通过拦截器来为header注入token
@ -20,36 +23,36 @@ import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.
@Configuration
public class FeignConfig implements RequestInterceptor {
/**
* @param template
*/
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
/**
* @param template
*/
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) {
final String header = attributes.getRequest().getHeader(AUTHORIZATION_HEADER);// 提取request头信息
if (null != attributes) {
final String header = attributes.getRequest().getHeader(AUTHORIZATION_HEADER);// 提取request头信息
// 检查请求头是否包含 Bearer 前缀
if (StringUtils.startsWith(header, TOKEN_PREFIX)) {
String authToken = RegExUtils.replaceAll(header, TOKEN_PREFIX, "");// 提取 token 信息
// 检查请求头是否包含 Bearer 前缀
if (StringUtils.startsWith(header, TOKEN_PREFIX)) {
String authToken = RegExUtils.replaceAll(header, TOKEN_PREFIX, "");// 提取 token 信息
template.header(AUTHORIZATION_HEADER, String.format("%s%s", TOKEN_PREFIX, authToken));
} else {
//检查cookie
Optional<Cookie> optional = Arrays.stream(attributes.getRequest().getCookies())
.filter(cookie -> StringUtils.equals(cookie.getName(), "mall3_token"))
.findAny();
template.header(AUTHORIZATION_HEADER, String.format("%s%s", TOKEN_PREFIX, authToken));
} else {// 检查cookie
Optional.ofNullable(attributes.getRequest().getCookies()).ifPresent(cookies -> {
Stream.of(cookies).filter(item -> StringUtils.equals(item.getName(), COOKIE_TOKEN_CODE)).findFirst()
.ifPresent(token -> {
template.header(AUTHORIZATION_HEADER, String.format("%s%s", TOKEN_PREFIX, token));
});
});
}
final String currentRoleCode = attributes.getRequest().getHeader(CURRENT_ROLE_CODE);// 提取request头信息
optional.ifPresent(o -> template.header(AUTHORIZATION_HEADER, String.format("%s%s", TOKEN_PREFIX, o)));
}
final String currentRoleCode = attributes.getRequest().getHeader(CURRENT_ROLE_CODE);// 提取request头信息
// 检查请求头是否包含 currentRoleCode
if (StringUtils.isNotEmpty(currentRoleCode)) {
template.header(CURRENT_ROLE_CODE, currentRoleCode);
}
}
}
// 检查请求头是否包含 currentRoleCode
if (StringUtils.isNotEmpty(currentRoleCode)) {
template.header(CURRENT_ROLE_CODE, currentRoleCode);
}
}
}
}