|
|
|
@ -26,16 +26,14 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
@ConfigurationProperties(prefix = "jwt.filter")
|
|
|
|
|
@ConfigurationProperties(prefix = "jwt.filter") //should_not_filter是通过这句话注入读取进内存的
|
|
|
|
|
public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${jwt.secret}")
|
|
|
|
|
private String secret;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
@Autowired
|
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
|
|
|
|
List<String> should_not_filter = new ArrayList<>();
|
|
|
|
|
|
|
|
|
@ -60,6 +58,7 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
private void init() {
|
|
|
|
|
//这是贪吃蛇吗?自己吃自己?
|
|
|
|
|
ArrayList<String> newList = new ArrayList<>();
|
|
|
|
|
for (String s : should_not_filter) {
|
|
|
|
|
newList.add(s.replaceAll("\\*\\*", "[^\\\\s]*"));
|
|
|
|
@ -84,7 +83,7 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
String path = request.getRequestURI();
|
|
|
|
|
String contextPath = request.getContextPath();
|
|
|
|
|
String pathInfo = path.replaceFirst(contextPath, "");
|
|
|
|
|
|
|
|
|
|
//不过滤的跳过
|
|
|
|
|
boolean matches = false;
|
|
|
|
|
for (String s : should_not_filter) {
|
|
|
|
|
Pattern pattern = Pattern.compile(s);
|
|
|
|
@ -92,6 +91,7 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
matches = matcher.matches();
|
|
|
|
|
if (matches) break;
|
|
|
|
|
}
|
|
|
|
|
//否则是需要过滤的
|
|
|
|
|
return !matches;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,9 +102,9 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
//Map<String, List<String>> requestQueryParams = ctx.getRequestQueryParams();
|
|
|
|
|
HttpServletRequest request = ctx.getRequest();
|
|
|
|
|
String Authorization = request.getHeader("Authorization");
|
|
|
|
|
if (Authorization == null){
|
|
|
|
|
if (Authorization == null) {
|
|
|
|
|
try {
|
|
|
|
|
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
|
|
|
|
|
BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
|
|
|
|
|
StringBuilder responseStrBuilder = new StringBuilder();
|
|
|
|
|
String inputStr;
|
|
|
|
|
while ((inputStr = streamReader.readLine()) != null)
|
|
|
|
@ -114,10 +114,9 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String uri =ctx.getRequest().getRequestURI();
|
|
|
|
|
// String uri =ctx.getRequest().getRequestURI();
|
|
|
|
|
if (Authorization == null || Authorization.length() == 0) {
|
|
|
|
|
Response<TokenInfoVO> resp = EncodeUtil.tokenErrorResp("Token 错误");
|
|
|
|
|
Object json = JSONObject.toJSON(resp);
|
|
|
|
@ -131,14 +130,14 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
String token = Authorization.replaceFirst(JwtUtil.AUTHORIZATION_STARTER, "");
|
|
|
|
|
Claims cs = JwtUtil.getClaimsFromToken(token, secret);
|
|
|
|
|
|
|
|
|
|
if(cs == null || cs.size() == 0) {
|
|
|
|
|
if (cs == null || cs.size() == 0) {
|
|
|
|
|
Response<TokenInfoVO> resp = EncodeUtil.tokenErrorResp("Token 错误");
|
|
|
|
|
Object json = JSONObject.toJSON(resp);
|
|
|
|
|
ctx.setSendZuulResponse(false);
|
|
|
|
|
ctx.setResponseBody(json.toString());
|
|
|
|
|
ctx.getResponse().setContentType("text/json; charset=utf-8");
|
|
|
|
|
ctx.setResponseStatusCode(200);
|
|
|
|
|
System.out.println("cs:"+Authorization);
|
|
|
|
|
System.out.println("cs:" + Authorization);
|
|
|
|
|
return ctx.getResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -155,8 +154,8 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//验证数据是否过期
|
|
|
|
|
String checkToken = tokenService.getToken(TokenService.ONLINE_OPERRATOR+operatorId);
|
|
|
|
|
if(StringUtils.isEmpty(checkToken)){
|
|
|
|
|
String checkToken = tokenService.getToken(TokenService.ONLINE_OPERRATOR + operatorId);
|
|
|
|
|
if (StringUtils.isEmpty(checkToken)) {
|
|
|
|
|
Response<TokenInfoVO> resp = EncodeUtil.tokenErrorResp("Token 错误");
|
|
|
|
|
Object json = JSONObject.toJSON(resp);
|
|
|
|
|
ctx.setSendZuulResponse(false);
|
|
|
|
@ -164,9 +163,9 @@ public class ZuulPreFilter extends com.netflix.zuul.ZuulFilter {
|
|
|
|
|
ctx.getResponse().setContentType("text/json; charset=utf-8");
|
|
|
|
|
ctx.setResponseStatusCode(200);
|
|
|
|
|
return ctx.getResponse();
|
|
|
|
|
}else{ //保持token心跳
|
|
|
|
|
if(checkToken.equals(token)){
|
|
|
|
|
}else{
|
|
|
|
|
} else { //保持token心跳
|
|
|
|
|
if (checkToken.equals(token)) {
|
|
|
|
|
} else {
|
|
|
|
|
Response<TokenInfoVO> resp = EncodeUtil.tokenErrorResp("Token 错误");
|
|
|
|
|
Object json = JSONObject.toJSON(resp);
|
|
|
|
|
ctx.setSendZuulResponse(false);
|
|
|
|
|