Gateway网关统一鉴权实现

package com.java1234.constant;

/**
 * 系统级静态变量
 * @author java1234_小锋
 * @site www.java1234.com
 * @company Java知识分享网
 * @create 2019-08-13 上午 9:51
 */
public class SystemConstant {
   

    /**
     * token
     */
    public static final int JWT_ERRCODE_NULL = 4000;			//Token不存在
    public static final int JWT_ERRCODE_EXPIRE = 4001;			//Token过期
    public static final int JWT_ERRCODE_FAIL = 4002;			//验证不通过

    /**
     * JWT
     */
    public static final String JWT_SECERT = "8677df7fc3a34e26a61c034d5ec8245d";			//密匙
    public static final long JWT_TTL = 1000000;									//token有效时间
}

package com.java1234.entity;

import io.jsonwebtoken.Claims;

/**
 * jwt验证信息
 * @author java1234_小锋
 * @site www.java1234.com
 * @company Java知识分享网
 * @create 2019-08-13 上午 10:00
 */
public class CheckResult {
   

    private int errCode;

    private boolean success;

    private Claims claims;

    public int getErrCode() {
   
        return errCode;
    }

    public void setErrCode(int errCode) {
   
        this.errCode = errCode;
    }

    public boolean isSuccess() {
   
        return success;
    }

    public void setSuccess(boolean success) {
   
        this.success = success;
    }

    public Claims getClaims() {
   
        return claims;
    }

    public void setClaims(Claims claims) {
   
        this.claims = claims;
    }

}

package com.java1234.filter;

import com.java1234.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * 鉴权过滤器
 * @author java1234_小锋 (公众号:java1234)
 * @site www.java1234.vip
 * @company 南通小锋网络科技有限公司
 */
@Component
public class AuthorizeFilter implements GlobalFilter, Ordered {
   

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
   
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String path=request.getURI().getPath();
        System.out.println("path="+path);
        if(path.contains("/adminLogin"))

你可能感兴趣的:(分布式小程序电商,gateway)