作者 朱兆平

添加匿名者访问开放路由的过滤规则

package com.example.gateway.filter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.gateway.model.PERMISSION;
import com.example.gateway.model.ROLE;
import com.example.gateway.model.ResponseBean;
import com.example.gateway.model.USERS;
import com.example.gateway.util.JsonToBean;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -24,6 +27,7 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.management.relation.Role;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
... ... @@ -132,6 +136,11 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
}
public boolean UrlFilter(ServerHttpRequest request) {
//先鉴权匿名者可访问的接口
if (anonymousUrlFilter(request)){
return true;
}
boolean flag = false;
String token = "";
if (!request.getPath().toString().contains(LOGIN)) {
... ... @@ -154,4 +163,30 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
}
return flag;
}
public boolean anonymousUrlFilter(ServerHttpRequest request){
//内部服务接口,不允许外部访问
if(pathMatcher.match("/**/anonymous/**", request.getPath().toString())) {
return true;
}
String redisKey = "ROLE_anonymous_routers";
String json = stringRedisTemplate.opsForValue().get(redisKey);
if(json!=null && !json.isEmpty()){
JSONObject jsonObject = JSONObject.parseObject(json);
List<ROLE> roleList = JSONObject.toJavaObject(jsonObject,List.class);
if(roleList!=null && !roleList.isEmpty()){
for (ROLE role: roleList){
List<PERMISSION> permissionList= role.getPermissions();
if(permissionList!=null && !permissionList.isEmpty()){
for (PERMISSION permission : permissionList) {
if (pathMatcher.match(permission.getUrl(), request.getPath().toString())) {
return true;
}
}
}
}
}
}
return false;
}
}
... ...