正在显示
1 个修改的文件
包含
35 行增加
和
0 行删除
1 | package com.example.gateway.filter; | 1 | package com.example.gateway.filter; |
2 | 2 | ||
3 | import com.alibaba.fastjson.JSON; | 3 | import com.alibaba.fastjson.JSON; |
4 | +import com.alibaba.fastjson.JSONObject; | ||
4 | import com.example.gateway.model.PERMISSION; | 5 | import com.example.gateway.model.PERMISSION; |
6 | +import com.example.gateway.model.ROLE; | ||
5 | import com.example.gateway.model.ResponseBean; | 7 | import com.example.gateway.model.ResponseBean; |
8 | +import com.example.gateway.model.USERS; | ||
6 | import com.example.gateway.util.JsonToBean; | 9 | import com.example.gateway.util.JsonToBean; |
7 | import org.reactivestreams.Publisher; | 10 | import org.reactivestreams.Publisher; |
8 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -24,6 +27,7 @@ import org.springframework.web.server.ServerWebExchange; | @@ -24,6 +27,7 @@ import org.springframework.web.server.ServerWebExchange; | ||
24 | import reactor.core.publisher.Flux; | 27 | import reactor.core.publisher.Flux; |
25 | import reactor.core.publisher.Mono; | 28 | import reactor.core.publisher.Mono; |
26 | 29 | ||
30 | +import javax.management.relation.Role; | ||
27 | import java.nio.charset.Charset; | 31 | import java.nio.charset.Charset; |
28 | import java.util.ArrayList; | 32 | import java.util.ArrayList; |
29 | import java.util.List; | 33 | import java.util.List; |
@@ -132,6 +136,11 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -132,6 +136,11 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
132 | } | 136 | } |
133 | 137 | ||
134 | public boolean UrlFilter(ServerHttpRequest request) { | 138 | public boolean UrlFilter(ServerHttpRequest request) { |
139 | + //先鉴权匿名者可访问的接口 | ||
140 | + if (anonymousUrlFilter(request)){ | ||
141 | + return true; | ||
142 | + } | ||
143 | + | ||
135 | boolean flag = false; | 144 | boolean flag = false; |
136 | String token = ""; | 145 | String token = ""; |
137 | if (!request.getPath().toString().contains(LOGIN)) { | 146 | if (!request.getPath().toString().contains(LOGIN)) { |
@@ -154,4 +163,30 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -154,4 +163,30 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
154 | } | 163 | } |
155 | return flag; | 164 | return flag; |
156 | } | 165 | } |
166 | + | ||
167 | + public boolean anonymousUrlFilter(ServerHttpRequest request){ | ||
168 | + //内部服务接口,不允许外部访问 | ||
169 | + if(pathMatcher.match("/**/anonymous/**", request.getPath().toString())) { | ||
170 | + return true; | ||
171 | + } | ||
172 | + String redisKey = "ROLE_anonymous_routers"; | ||
173 | + String json = stringRedisTemplate.opsForValue().get(redisKey); | ||
174 | + if(json!=null && !json.isEmpty()){ | ||
175 | + JSONObject jsonObject = JSONObject.parseObject(json); | ||
176 | + List<ROLE> roleList = JSONObject.toJavaObject(jsonObject,List.class); | ||
177 | + if(roleList!=null && !roleList.isEmpty()){ | ||
178 | + for (ROLE role: roleList){ | ||
179 | + List<PERMISSION> permissionList= role.getPermissions(); | ||
180 | + if(permissionList!=null && !permissionList.isEmpty()){ | ||
181 | + for (PERMISSION permission : permissionList) { | ||
182 | + if (pathMatcher.match(permission.getUrl(), request.getPath().toString())) { | ||
183 | + return true; | ||
184 | + } | ||
185 | + } | ||
186 | + } | ||
187 | + } | ||
188 | + } | ||
189 | + } | ||
190 | + return false; | ||
191 | + } | ||
157 | } | 192 | } |
-
请 注册 或 登录 后发表评论