• Spring Cloud Gateway全局过滤器GlobalFilter:返回消息和重定向


    Spring Cloud Gateway的全局过滤器GlobalFilter,对所有的请求生效,可以用来做权限控制,拦截到非法请求后如何返回自定义信息和将请求重定向到指定URL。

    一、返回401状态码和提示信息

    只要将自定义的GlobalFilter声明成Spring Bean就会自动生效,Ordered接口用来指定拦截器生效顺序(数字越小优先级越高)。

    这里假设用来验证权限的key是authToken。

    import java.net.URI;
    import java.nio.charset.StandardCharsets;
    
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
    import org.springframework.core.Ordered;
    import org.springframework.core.io.buffer.DataBuffer;
    import org.springframework.http.HttpHeaders;
    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 org.springframework.web.util.UriComponentsBuilder;
    
    import com.alibaba.fastjson.JSONObject;
    
    @Component
    public class AuthFilter implements GlobalFilter, Ordered {
    
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            String token = exchange.getRequest().getQueryParams().getFirst("authToken");
            //返回401状态码和提示信息
            if (StringUtils.isBlank(token)) {
                ServerHttpResponse response = exchange.getResponse();
                JSONObject message = new JSONObject();
                message.put("status", -1);
                message.put("data", "鉴权失败");
                byte[] bits = message.toJSONString().getBytes(StandardCharsets.UTF_8);
                DataBuffer buffer = response.bufferFactory().wrap(bits);
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                //指定编码,否则在浏览器中会中文乱码
                response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
                return response.writeWith(Mono.just(buffer));
            }
            return chain.filter(exchange);
        }
    
        @Override
        public int getOrder() {
            return -100;
        }
    }
    

      

    二、重定向(redirect)到指定页面

    对于浏览器,通常是发现没有权限后跳转到登录页面。响应状态码需要为HttpStatus.SEE_OTHER(303)。

    重定向(redirect)会丢失之前请求的参数,对于需要转发到目标URL的参数,需手工添加。

    import java.net.URI;
    import java.nio.charset.StandardCharsets;
    
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
    import org.springframework.core.Ordered;
    import org.springframework.core.io.buffer.DataBuffer;
    import org.springframework.http.HttpHeaders;
    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;
    
    @Component
    public class AuthFilter implements GlobalFilter, Ordered {
    
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            String token = exchange.getRequest().getQueryParams().getFirst("authToken");
            //重定向(redirect)到登录页面
            if (StringUtils.isBlank(token)) {
                String url = "http://想跳转的网址";
                ServerHttpResponse response = exchange.getResponse();
                //303状态码表示由于请求对应的资源存在着另一个URI,应使用GET方法定向获取请求的资源
                response.setStatusCode(HttpStatus.SEE_OTHER);
                response.getHeaders().set(HttpHeaders.LOCATION, url);
                return response.setComplete();
            }
            return chain.filter(exchange);
        }
    
        @Override
        public int getOrder() {
            return -100;
        }
    }
    

      

  • 相关阅读:
    【LuoguP4770】[NOI2018] 你的名字
    【LuoguP5171】Earthquake
    【LuoguP3747】[六省联考2017] 相逢是问候
    【LuoguP4916】魔力环
    YOLO2:实时目标检测视频教程,视频演示, Android Demo ,开源教学项目,论文。
    谷歌发布 TensorFlow Lite [官方网站,文档]
    Chinese-Text-Classification,用卷积神经网络基于 Tensorflow 实现的中文文本分类。
    Chinese-Text-Classification:Tensorflow CNN 模型实现的中文文本分类器[不分词版]
    Hinton's paper Dynamic Routing Between Capsules 的 Tensorflow , Keras ,Pytorch实现
    谷歌开发者:看可口可乐公司是怎么玩转TensorFlow的?
  • 原文地址:https://www.cnblogs.com/wangbin96/p/12531935.html
Copyright © 2020-2023  润新知