正在显示
2 个修改的文件
包含
15 行增加
和
11 行删除
1 | package com.example.gateway.filter; | 1 | package com.example.gateway.filter; |
2 | 2 | ||
3 | -import com.alibaba.fastjson.JSON; | ||
4 | import com.alibaba.fastjson.JSONArray; | 3 | import com.alibaba.fastjson.JSONArray; |
5 | import com.alibaba.fastjson.JSONObject; | 4 | import com.alibaba.fastjson.JSONObject; |
6 | import com.example.gateway.model.PERMISSION; | 5 | import com.example.gateway.model.PERMISSION; |
7 | import com.example.gateway.model.ROLE; | 6 | import com.example.gateway.model.ROLE; |
8 | -import com.example.gateway.model.ResponseBean; | ||
9 | -import com.example.gateway.model.USERS; | ||
10 | import com.example.gateway.util.JsonToBean; | 7 | import com.example.gateway.util.JsonToBean; |
8 | +import lombok.extern.slf4j.Slf4j; | ||
11 | import org.reactivestreams.Publisher; | 9 | import org.reactivestreams.Publisher; |
12 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
13 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; | 11 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; |
@@ -27,17 +25,15 @@ import org.springframework.util.AntPathMatcher; | @@ -27,17 +25,15 @@ import org.springframework.util.AntPathMatcher; | ||
27 | import org.springframework.web.server.ServerWebExchange; | 25 | import org.springframework.web.server.ServerWebExchange; |
28 | import reactor.core.publisher.Flux; | 26 | import reactor.core.publisher.Flux; |
29 | import reactor.core.publisher.Mono; | 27 | import reactor.core.publisher.Mono; |
30 | - | ||
31 | -import javax.management.relation.Role; | ||
32 | import java.nio.charset.Charset; | 28 | import java.nio.charset.Charset; |
33 | -import java.util.ArrayList; | ||
34 | import java.util.List; | 29 | import java.util.List; |
35 | 30 | ||
36 | /** | 31 | /** |
37 | - * @author | 32 | + * @author MRZ |
38 | * @time 2019-09-09 12:13 | 33 | * @time 2019-09-09 12:13 |
39 | */ | 34 | */ |
40 | @Component | 35 | @Component |
36 | +@Slf4j | ||
41 | public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | 37 | public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { |
42 | 38 | ||
43 | private static final String LOGIN = "user-center/login"; | 39 | private static final String LOGIN = "user-center/login"; |
@@ -94,7 +90,7 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -94,7 +90,7 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
94 | /** | 90 | /** |
95 | * 测试返回中文乱码 | 91 | * 测试返回中文乱码 |
96 | */ | 92 | */ |
97 | - ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(response) { | 93 | + ServerHttpResponseDecorator decoratedResponseWindows = new ServerHttpResponseDecorator(response) { |
98 | @Override | 94 | @Override |
99 | public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { | 95 | public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { |
100 | if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) { | 96 | if (HttpStatus.OK.equals(getStatusCode()) && body instanceof Flux) { |
@@ -126,7 +122,7 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -126,7 +122,7 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
126 | return super.writeWith(body); | 122 | return super.writeWith(body); |
127 | } | 123 | } |
128 | }; | 124 | }; |
129 | - return chain.filter(exchange.mutate().response(decoratedResponse).build()); | 125 | + return chain.filter(exchange.mutate().response(decoratedResponseWindows).build()); |
130 | // | 126 | // |
131 | // return chain.filter(exchange); | 127 | // return chain.filter(exchange); |
132 | } | 128 | } |
@@ -149,19 +145,26 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -149,19 +145,26 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
149 | if(rqHeader.containsKey("Authorization")){ | 145 | if(rqHeader.containsKey("Authorization")){ |
150 | token = rqHeader.get("Authorization").toString(); | 146 | token = rqHeader.get("Authorization").toString(); |
151 | token = token.substring(1, token.length() - 1); | 147 | token = token.substring(1, token.length() - 1); |
148 | + log.info("[TOKEN]-Request Authorization INFO is:[{}]",token); | ||
152 | } | 149 | } |
153 | } | 150 | } |
154 | String redisKey = token.replace("Bearer ", ""); | 151 | String redisKey = token.replace("Bearer ", ""); |
152 | + log.info("[REDIS-KEY]-is:[{}]",redisKey); | ||
155 | String json = stringRedisTemplate.opsForValue().get(redisKey); | 153 | String json = stringRedisTemplate.opsForValue().get(redisKey); |
156 | if (json != null) { | 154 | if (json != null) { |
157 | List<PERMISSION> permissionList = JsonToBean.jsonToUser(json); | 155 | List<PERMISSION> permissionList = JsonToBean.jsonToUser(json); |
158 | for (PERMISSION permission : permissionList) { | 156 | for (PERMISSION permission : permissionList) { |
157 | + log.trace("访问url:[{}]<->权限[{}]",request.getPath().toString(),permission.getUrl()); | ||
159 | if (pathMatcher.match(permission.getUrl(), request.getPath().toString())) { | 158 | if (pathMatcher.match(permission.getUrl(), request.getPath().toString())) { |
159 | + log.info("[FILTER]-[URL:{}]->鉴权成功",request.getPath().toString()); | ||
160 | flag = true; | 160 | flag = true; |
161 | break; | 161 | break; |
162 | } | 162 | } |
163 | } | 163 | } |
164 | + }else { | ||
165 | + log.info("[FILTER]-没有对应token的redis缓存,鉴权失败"); | ||
164 | } | 166 | } |
167 | + log.info("[FILTER]-[URL:{}]->鉴权失败",request.getPath().toString()); | ||
165 | return flag; | 168 | return flag; |
166 | } | 169 | } |
167 | 170 | ||
@@ -189,12 +192,13 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | @@ -189,12 +192,13 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered { | ||
189 | } | 192 | } |
190 | } | 193 | } |
191 | }catch (Exception e){ | 194 | }catch (Exception e){ |
192 | - e.printStackTrace(); | 195 | + log.error("[ANONYMOUS-FILTER]匿名者过滤规则审核出错->{}",e.toString()); |
193 | return false; | 196 | return false; |
194 | } | 197 | } |
195 | 198 | ||
196 | 199 | ||
197 | } | 200 | } |
201 | + log.info("[ANONYMOUS-FILTER]-匿名者过滤器为适配到符合条件的规则-PATH:[{}]",request.getPath().toString()); | ||
198 | return false; | 202 | return false; |
199 | } | 203 | } |
200 | } | 204 | } |
-
请 注册 或 登录 后发表评论