SpringWebFlux 实现登陆过滤问题

Yahocen 发布于 2018/09/15 19:13
阅读 7K+
收藏 0
@Component
@Order(1)
public class LoginFilter extends ForwardedHeaderFilter {



    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //WebSession session  = exchange.getSession().block();

        URI reuri = request.getURI();

        exchange.getSession().

        exchange.getSession().flatMap(session -> {

            //判断访问路径   判断是否登陆
            if(reuri.getPath().contains("/admin") && null == session.getAttribute("admin")){
                //拼接登陆页面路径
                StringBuffer rpuri = new StringBuffer();
                rpuri.append(reuri.getScheme());
                rpuri.append("://");
                rpuri.append(reuri.getAuthority());
                rpuri.append("/admin/login");
                System.out.println(reuri.getPath());
                URI uri = UriComponentsBuilder.fromHttpUrl(rpuri.toString()).build().toUri();

                //创建返回值
                ServerWebExchange withChangedUri = exchange.mutate().request(builder -> {
                    builder.uri(uri);
                }).build();

                //返回登陆页面
                return chain.filter(withChangedUri);
            }

            //继续执行
            return super.filter(exchange, chain);
        });

        //继续执行
        return super.filter(exchange, chain);
    }
}

刚开始使用 大家的登陆检测是如何写得   弄了一下午  差文档搜百度就写成这样子了 还是不管用运行后 

exchange.getSession().flatMap(ses

这一段是不执行的  但是把判断拿出来

exchange.getSession()

不知道如何取值   有些蒙圈

加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部