栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot URL Rewrite

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot URL Rewrite

背景

后台接收的请求,希望把请求URL做重定向,改变原有的请求,此篇可以帮助你解决这个问题,可以用来改变常规URL重定向,也可以隐藏所访问的静态资源

参考地址

http://blog.jdriven.com/2016/02/urlrewritefilter-load-configuration-with-spring-resourceloader/

代码实现部分

实现这个功能需要以下几个步骤

  • pom中引用jar
  • 增加一个配制类
  • resource文件夹下增加一个配置文件

以下对上述步骤展开具体说明

pom中引用jar
  
        
            org.tuckey
            urlrewritefilter
            4.0.4
        
增加一个配制类
import java.io.IOException;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;

@Configuration
public class UrlRewriteFilterConfig extends UrlRewriteFilter {

    private static final String URL_REWRITE = "classpath:/urlrewrite.xml";

    // Inject the Resource from the given location
    @Value(URL_REWRITE)
    private Resource resource;

    // Override the loadUrlRewriter method, and write your own implementation
    @Override
    protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
        try {
            // Create a UrlRewrite Conf object with the injected resource
            Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(),
                    "@@systemID@@"); //最后的参数是自己系统的标识ID即可
            checkConf(conf);
        } catch (IOException ex) {
            throw new ServletException("Unable to load URL rewrite configuration file from " + URL_REWRITE, ex);
        }
    }
}
增加需要重定向的配置文件





    
    
        /test/
        /
    

    
    
        ^/test/list/(w+)/(w+).html
        /test/list/$1/$2/
    

    
    
        ^/test11/(w+).html$
        /realTime/$1/
    

    
    
        ^/static/(w+).html$
        /static/$1/
    


测试

上述完成后就可以了,so easy,可以自己测试一下了,这里就不截图了,亲试过,好用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/894329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号