Springcloud First Experience: How Gateway Gateways service does token verification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.