首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Springboot URL Rewrite

Springboot URL Rewrite

作者头像
用户11690639
发布2026-06-17 21:45:01
发布2026-06-17 21:45:01
1080
举报

背景

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

参考地址

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

代码实现部分

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

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

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

pom中引用jar

代码语言:javascript
复制
  <!--地址重定向用-->
        <dependency>
            <groupId>org.tuckey</groupId>
            <artifactId>urlrewritefilter</artifactId>
            <version>4.0.4</version>
        </dependency>

增加一个配制类

代码语言:javascript
复制
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);
        }
    }
}

增加需要重定向的配置文件

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<urlrewrite>

    <!-- 栏目首页 -->
    <rule>
        <from>/test/</from>
        <to>/</to>
    </rule>

    <!-- 栏目列表页,注意html后面没有加$,因为后面还有若干参数 -->
    <rule>
        <from>^/test/list/(\w+)/(\w+)\.html</from>
        <to>/test/list/$1/$2/</to>
    </rule>

    <!-- 文章详情页 -->
    <rule>
        <from>^/test11/(\w+)\.html$</from>
        <to>/realTime/$1/</to>
    </rule>

    <!-- 静态网页 -->
    <rule>
        <from>^/static/(\w+)\.html$</from>
        <to>/static/$1/</to>
    </rule>

</urlrewrite>

测试

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 参考地址
  • 代码实现部分
    • pom中引用jar
    • 增加一个配制类
    • 增加需要重定向的配置文件
  • 测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档