File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
Chapter9-1-5/api-gateway/src/main/java/com/didispace Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 11package com .didispace ;
22
3+ import com .didispace .filter .AccessFilter ;
34import org .springframework .boot .builder .SpringApplicationBuilder ;
45import org .springframework .cloud .client .SpringCloudApplication ;
56import org .springframework .cloud .netflix .zuul .EnableZuulProxy ;
7+ import org .springframework .context .annotation .Bean ;
68
79@ EnableZuulProxy
810@ SpringCloudApplication
@@ -12,4 +14,9 @@ public static void main(String[] args) {
1214 new SpringApplicationBuilder (Application .class ).web (true ).run (args );
1315 }
1416
17+ @ Bean
18+ public AccessFilter accessFilter () {
19+ return new AccessFilter ();
20+ }
21+
1522}
Original file line number Diff line number Diff line change 1+ package com .didispace .filter ;
2+
3+ import com .netflix .zuul .ZuulFilter ;
4+ import com .netflix .zuul .context .RequestContext ;
5+ import org .slf4j .Logger ;
6+ import org .slf4j .LoggerFactory ;
7+
8+ import javax .servlet .http .HttpServletRequest ;
9+
10+ public class AccessFilter extends ZuulFilter {
11+
12+ private static Logger log = LoggerFactory .getLogger (AccessFilter .class );
13+
14+ @ Override
15+ public String filterType () {
16+ return "pre" ;
17+ }
18+
19+ @ Override
20+ public int filterOrder () {
21+ return 0 ;
22+ }
23+
24+ @ Override
25+ public boolean shouldFilter () {
26+ return true ;
27+ }
28+
29+ @ Override
30+ public Object run () {
31+ RequestContext ctx = RequestContext .getCurrentContext ();
32+ HttpServletRequest request = ctx .getRequest ();
33+
34+ log .info (String .format ("%s request to %s" , request .getMethod (), request .getRequestURL ().toString ()));
35+
36+ Object accessToken = request .getParameter ("accessToken" );
37+ if (accessToken == null ) {
38+ log .warn ("access token is empty" );
39+ ctx .setSendZuulResponse (false );
40+ ctx .setResponseStatusCode (401 );
41+ return null ;
42+ }
43+ log .info ("access token ok" );
44+ return null ;
45+ }
46+
47+ }
You can’t perform that action at this time.
0 commit comments