Talk about the background: if there is a user service after the user login, generate a token to the client, the user needs this token each time the request, so each time will be in the Gateway gateways check, check through the gateway from token to parse the UserID, The userid is then delivered to each service.
For example, there is now a Java service and a PHP service, the URLs that are accessed from the gateway are http://127.0.0.1:8201/java/and http://127.0.0.1:8201/php/, and now only for the PHP service validation, first look at
Spring Cloud Gateway's Official document address: Http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html#_ Addrequestheader_gatewayfilter_factory
One, need to customize gatewayfilterfactory inherit Abstractgatewayfilterfactory abstract class, the code is as follows:
PackageCn.taxiong.tx_api_gateway_server.filter;ImportOrg.springframework.cloud.gateway.filter.GatewayFilter;Importorg.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;ImportOrg.springframework.core.io.buffer.DataBuffer;Importorg.springframework.http.HttpHeaders;ImportOrg.springframework.http.server.reactive.ServerHttpResponse;ImportReactor.core.publisher.Mono;/*** JWT Verified filter * *@author[email protected] * @create 2018-09-09 pm 10:05 **/ Public classJwtcheckgatewayfilterfactoryextendsAbstractgatewayfilterfactory<jwtcheckgatewayfilterfactory.config> { Publicjwtcheckgatewayfilterfactory () {Super(Config.class); } @Override Publicgatewayfilter apply (config config) {return(Exchange, chain),{String Jwttoken= Exchange.getrequest (). Getheaders (). GetFirst ("Authorization"); //verifying the legitimacy of Jwttoken if(Jwttoken! =NULL) { //Legal//Pass the user ID as a parameter returnchain.filter (Exchange); } //Non-compliant (response to non-logged-in exceptions)Serverhttpresponse response =Exchange.getresponse (); //Set HeadersHttpheaders httpheaders =response.getheaders (); Httpheaders.add ("Content-type", "Application/json; Charset=utf-8 "); Httpheaders.add ("Cache-control", "No-store, No-cache, Must-revalidate, max-age=0"); //Set BodyString warningstr = "Not logged on or login timed out"; DataBuffer Bodydatabuffer=response.bufferfactory (). Wrap (Warningstr.getbytes ()); returnResponse.writewith (Mono.just (Bodydatabuffer)); }; } Public Static classConfig {//Put The configuration properties for your filter here }}
View Code
Second, you need to inject the custom gatewayfilterfactory into spring
PackageCn.taxiong.tx_api_gateway_server.config;Importcn.taxiong.tx_api_gateway_server.filter.JwtCheckGatewayFilterFactory;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/*** Application Configuration * *@author[email protected] * @create 2018-09-09 pm 10:57 **/@Configuration Public classAppConfig {@Bean Publicjwtcheckgatewayfilterfactory jwtcheckgatewayfilterfactory () {return Newjwtcheckgatewayfilterfactory (); }}
III. services that configure custom filters in the configuration file for the Gateway Service
This service is only configured for PHP, and the Java service does not use this filter rule
Springcloud First Experience: How Gateway Gateways service does token verification