zoukankan      html  css  js  c++  java
  • SpringBoot 利用过滤器Filter修改请求url地址

    要求:

    代码中配置的url路径为http://127.0.0.1/api/associates/queryAssociatesInfo

    现在要求http://127.0.0.1/associates/queryAssociatesInfo也可以同样访问同一个conroller下面的method,并且要求参数全部跟随

    代码:

    package com.shitou.huishi.framework.filter;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServletResponseWrapper;
    
    /**
     * 修改请求路由,当进入url为/a/b时,将其url修改为/api/a/b
     * Created by qhong on 2018/5/16 13:27
     **/
    public class UrlFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
        @Override
        public void destroy() {
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest httpRequest = (HttpServletRequest)request;
            HttpServletResponseWrapper httpResponse = new HttpServletResponseWrapper((HttpServletResponse) response);
            System.out.println(httpRequest.getRequestURI());
            String path=httpRequest.getRequestURI();
            if(path.indexOf("/api/")<0){
                path="/api"+path;
                System.out.println(path);
                httpRequest.getRequestDispatcher(path).forward(request,response);
            }
           else {
                chain.doFilter(request,response);
    
            }
            return;
        }
    }

    这个类必须继承Filter类,这个是Servlet的规范。有了过滤器类以后,以前的web项目可以在web.xml中进行配置,但是spring boot项目并没有web.xml这个文件,那怎么配置?在Spring boot中,我们需要FilterRegistrationBean来完成配置。

    其实现过程如下:

    package com.shitou.huishi.framework.filter;
    
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * Created by qhong on 2018/5/16 15:28
     **/
    @Configuration
    public class FilterConfig {
    
        @Bean
        public FilterRegistrationBean registFilter() {
            FilterRegistrationBean registration = new FilterRegistrationBean();
            registration.setFilter(new UrlFilter());
            registration.addUrlPatterns("/*");
            registration.setName("UrlFilter");
            registration.setOrder(1);
            return registration;
        }
    
    }

    https://www.cnblogs.com/paddix/p/8365558.html

  • 相关阅读:
    Java后端面试题大汇总,冲刺金三银四
    面试官:小伙子,Mybatis的本质和原理说一下
    面试官问:大量的 TIME_WAIT 状态 TCP 连接,对业务有什么影响?怎么处理?
    便捷搭建 Zookeeper 服务器的方法,好用,收藏~
    10 个冷门但又非常实用的 Docker 使用技巧
    mitmproxy 抓包工具(1)
    基于alpine创建Scrapy镜像
    强大的输入框-应用快速启动uTools
    Interceptor、Filter、Servlet的区别
    利用三层判断sql数据库中编码是否已经存在(个人拙作,不喜勿喷)
  • 原文地址:https://www.cnblogs.com/hongdada/p/9046376.html
Copyright © 2011-2022 走看看