Scenes
SpringCloud- use routing gateway unified access interface (with code to download):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102733039
After the above has been achieved using a routing gateway unified access interface, use the following routing gateway service filtering.
Note:
Blog:
https://blog.csdn.net/badao_liumang_qizhi
public concern number of
programs overbearing ape
acquisition-related programming e-books, tutorials and push for free download.
achieve
Zuul previously used for routing unified network management functions. There is also a revelation Zuul function is filtering service.
Only need to inherit ZuulFilter class and add annotations on @Component class on it.
New filter bag, under the new category in the project package package LoginFilter
com.badao.hello.spring.cloud.zuul.filter Package; Import com.netflix.zuul.ZuulFilter; Import com.netflix.zuul.context.RequestContext; Import com.netflix.zuul.exception.ZuulException; Import org.slf4j .Logger; Import org.slf4j.LoggerFactory; Import org.springframework.stereotype.Component; Import the javax.servlet.http.HttpServletRequest; Import java.io.IOException; @Component public class LoginFilter the extends ZuulFilter { Private static Final Logger = Logger of LoggerFactory .getLogger (. LoginFilter class ); / * * * filter type configuration, there are four different types of filter life cycle * 1. pre: before the routing * 2. routing: The routing * 3. post: The routes * 4. error: error sending calls * @return * / @Override public String filterType () { return " pre " ; } / * * * sequentially arranged filter * @return * / @Override public int filterOrder () { return 0 ; } / * * * configuration is to be filtered: true / required, false / not required * @return * / @Override public Boolean shouldFilter () { return to true; } / ** * Specific service code filter * @return * @throws ZuulException * / @Override public Object RUN () throws ZuulException { // Get HttpServletRequest object the RequestContext context = RequestContext.getCurrentContext (); HttpServletRequest Request = context.getRequest (); logger.info ( " {} {} >>> " , request.getMethod (), request.getRequestURL () toString ().); // Get request parameter token-- token String = request.getParameter token ( " token " ); IF (token ==null ) { logger.warn ( " the Token IS empty " ); // S is set Zuul response to false context.setSendZuulResponse ( to false ); // set the response status code 401 does not have permission context.setResponseStatusCode ( 401 ); the try { / / display content to the page context.getResponse () the getWriter () Write (.. " NO Authority " ); } the catch (IOException E) { } } the else { logger.info ("OK"); } return null; } }
Note:
Four methods need to be rewritten, the following specific effect.
In a specific filtration method, there is a token parameter is determined whether there is a request, it is not without permission.
filterType
Filter type configuration, there are four different types of filter life cycle
1. pre: before the route
2. routing: The routing
3. post: The routes
4. error: error sending call
filter order
Filtered sequentially arranged, the smaller the value of the front.
shouldFilter
Is configured to be filtered: true / need, false / do not need.
Object run() throws ZuulException
Specific service code filter
So far as the structure of the whole service system
We turn starts Eureka services, service providers, service consumers two, zuul of Application startup class
Then point your browser to:
http://localhost:8769/api/a/hi?message=HelloZuul
If you go this time with a token request parameter, open your browser and enter again:
http://localhost:8769/api/a/hi?message=HelloZuul&token=badao