|
|
@ -5,10 +5,10 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.anji.plus.gaea.bean.ResponseBean;
|
|
|
|
import com.anji.plus.gaea.bean.ResponseBean;
|
|
|
|
import com.anji.plus.gaea.cache.CacheHelper;
|
|
|
|
import com.anji.plus.gaea.cache.CacheHelper;
|
|
|
|
import com.anji.plus.gaea.utils.JwtBean;
|
|
|
|
import com.anji.plus.gaea.utils.JwtBean;
|
|
|
|
|
|
|
|
import com.anjiplus.template.gaea.business.constant.BusinessConstant;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
@ -32,42 +32,16 @@ public class TokenFilter implements Filter {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private JwtBean jwtBean;
|
|
|
|
private JwtBean jwtBean;
|
|
|
|
|
|
|
|
|
|
|
|
/**跳过token验证和权限验证的url清单*/
|
|
|
|
// 跳过token验证和权限验证的url清单
|
|
|
|
@Value("#{'${customer.skip-authenticate-urls}'.split(',')}")
|
|
|
|
@Value("#{'${customer.skip-authenticate-urls}'.split(',')}")
|
|
|
|
private List<String> skipAuthenticateUrls;
|
|
|
|
private List<String> skipAuthenticateUrls;
|
|
|
|
private Pattern SKIP_AUTHENTICATE_PATTERN;
|
|
|
|
private Pattern SKIP_AUTHENTICATE_PATTERN;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
|
public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
|
Filter.super.init(filterConfig);
|
|
|
|
// 生成匹配正则,跳过token验证和权限验证的url
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 根据名单,生成正则
|
|
|
|
|
|
|
|
* @param skipUrlList
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Pattern fitByList(List<String> skipUrlList){
|
|
|
|
|
|
|
|
if(skipUrlList == null || skipUrlList.size() == 0){
|
|
|
|
|
|
|
|
return Pattern.compile(".*().*");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuffer patternString = new StringBuffer();
|
|
|
|
|
|
|
|
patternString.append(".*(");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
skipUrlList.stream().forEach(url ->{
|
|
|
|
|
|
|
|
patternString.append(url.trim());
|
|
|
|
|
|
|
|
patternString.append("|");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if(skipUrlList.size()>0){
|
|
|
|
|
|
|
|
patternString.deleteCharAt(patternString.length()-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
patternString.append(").*");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Pattern.compile(patternString.toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
|
|
|
private void postConstruct() {
|
|
|
|
|
|
|
|
SKIP_AUTHENTICATE_PATTERN = fitByList(skipAuthenticateUrls);
|
|
|
|
SKIP_AUTHENTICATE_PATTERN = fitByList(skipAuthenticateUrls);
|
|
|
|
|
|
|
|
Filter.super.init(filterConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -89,25 +63,32 @@ public class TokenFilter implements Filter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//获取token
|
|
|
|
//获取token
|
|
|
|
String authorization = request.getHeader("Authorization");
|
|
|
|
String token = request.getHeader("Authorization");
|
|
|
|
if (StringUtils.isBlank(authorization)) {
|
|
|
|
if (StringUtils.isBlank(token)) {
|
|
|
|
error(response);
|
|
|
|
error(response);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String username = jwtBean.getUsername(authorization);
|
|
|
|
// 判断token是否过期
|
|
|
|
// String uuid = jwtBean.getUUID(authorization);
|
|
|
|
String loginName = jwtBean.getUsername(token);
|
|
|
|
|
|
|
|
String tokenKey = String.format(BusinessConstant.GAEA_SECURITY_LOGIN_TOKEN, loginName);
|
|
|
|
if (!cacheHelper.exist(username)) {
|
|
|
|
String userKey = String.format(BusinessConstant.GAEA_SECURITY_LOGIN_USER, loginName);
|
|
|
|
|
|
|
|
if (!cacheHelper.exist(tokenKey)) {
|
|
|
|
error(response);
|
|
|
|
error(response);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cacheHelper.exist(userKey)) {
|
|
|
|
|
|
|
|
error(response);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String gaeaUserJsonStr = cacheHelper.stringGet(userKey);
|
|
|
|
|
|
|
|
|
|
|
|
// 延长有效期
|
|
|
|
// 延长有效期
|
|
|
|
cacheHelper.stringSetExpire(username, authorization, 3600);
|
|
|
|
cacheHelper.stringSetExpire(tokenKey, token, 3600);
|
|
|
|
|
|
|
|
cacheHelper.stringSetExpire(userKey, gaeaUserJsonStr, 3600);
|
|
|
|
|
|
|
|
|
|
|
|
//在线体验版本
|
|
|
|
//在线体验版本
|
|
|
|
if (username.equals("guest")
|
|
|
|
if (loginName.equals("guest")
|
|
|
|
&& !uri.endsWith("/dataSet/testTransform")
|
|
|
|
&& !uri.endsWith("/dataSet/testTransform")
|
|
|
|
&& !uri.endsWith("/reportDashboard/getData")
|
|
|
|
&& !uri.endsWith("/reportDashboard/getData")
|
|
|
|
&& !uri.startsWith("/dict")
|
|
|
|
&& !uri.startsWith("/dict")
|
|
|
@ -127,7 +108,6 @@ public class TokenFilter implements Filter {
|
|
|
|
|
|
|
|
|
|
|
|
//执行
|
|
|
|
//执行
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -135,6 +115,29 @@ public class TokenFilter implements Filter {
|
|
|
|
Filter.super.destroy();
|
|
|
|
Filter.super.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 根据名单,生成正则
|
|
|
|
|
|
|
|
* @param skipUrlList
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private Pattern fitByList(List<String> skipUrlList){
|
|
|
|
|
|
|
|
if(skipUrlList == null || skipUrlList.size() == 0){
|
|
|
|
|
|
|
|
return Pattern.compile(".*().*");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuffer patternString = new StringBuffer();
|
|
|
|
|
|
|
|
patternString.append(".*(");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
skipUrlList.stream().forEach(url ->{
|
|
|
|
|
|
|
|
patternString.append(url.trim());
|
|
|
|
|
|
|
|
patternString.append("|");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if(skipUrlList.size()>0){
|
|
|
|
|
|
|
|
patternString.deleteCharAt(patternString.length()-1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
patternString.append(").*");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Pattern.compile(patternString.toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void error(HttpServletResponse response) throws IOException {
|
|
|
|
private void error(HttpServletResponse response) throws IOException {
|
|
|
|
ResponseBean responseBean = ResponseBean.builder().code("50014").message("The Token has expired").build();
|
|
|
|
ResponseBean responseBean = ResponseBean.builder().code("50014").message("The Token has expired").build();
|
|
|
|
response.getWriter().print(JSONObject.toJSONString(responseBean));
|
|
|
|
response.getWriter().print(JSONObject.toJSONString(responseBean));
|
|
|
|