Java Tomcat 7 中的 URLRewrite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25873406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:23:15  来源:igfitidea点击:

URLRewrite in Tomcat 7

javatomcat

提问by KK99

I am planning to develop an Intranet application (Java client, JSP, SQLite)

我正在计划开发一个内网应用程序(Java 客户端、JSP、SQLite)

Goal of this is that when a user clicks on the link, if the user has access (to a team which is handled in the business logic) a file should be provided for download

这样做的目标是,当用户单击链接时,如果用户有权访问(访问业务逻辑中处理的团队),则应提供文件以供下载

There is a table in the db which holds the info and below is the example row

数据库中有一个表,其中包含信息,下面是示例行

ID | file                               | team   | md5
1  | D:\test\output_20140915_100012.zip | Falcon | 5cceaf4cc400fd8f5c7fb4b2af754f4093369f59

where MD5 is the MD% checksum of the string "D:\test\output.zip" and not the file itself

其中 MD5 是字符串“D:\test\output.zip”的 MD% 校验和,而不是文件本身

I create the MD5 just for the sake of having random number. Reason I don't use RAND is to avoid collision (this part is trivial)

我创建 MD5 只是为了获得随机数。我不使用 RAND 的原因是为了避免碰撞(这部分是微不足道的)

I want my URL to look like this

我希望我的网址看起来像这样

http://mywebserver:8080/<appname>/5cceaf4cc400fd8f5c7fb4b2af754f4093369f59

Which should be taken to

应该采取哪些措施

http://mywebserver:8080/<appname>/download.jsp?id=5cceaf4cc400fd8f5c7fb4b2af754f4093369f59

I am using Tomcat and I am wondering if how we could have a URL rewrite for this scenario

我正在使用 Tomcat,我想知道我们是否可以为此场景重写 URL

回答by Tobías

I would recommend to do it with Apache HTTP Server or another server. But if you want to do it only with Tomcat 7 use Tuckey UrlRewriteFilter:

我建议使用 Apache HTTP Server 或其他服务器来完成。但是,如果您只想使用 Tomcat 7 执行此操作,请使用Tuckey UrlRewriteFilter

A Java Web Filter for any compliant web application servers (such as Tomcat, JBoss, Jetty or Resin), which allows you to rewrite URLs before they get to your code. It is a very powerful tool just like Apache's mod_rewrite.

适用于任何兼容 Web 应用程序服务器(例如 Tomcat、JBoss、Jetty 或 Resin)的 Java Web 过滤器,它允许您在 URL 到达您的代码之前对其进行重写。它是一个非常强大的工具,就像 Apache 的 mod_rewrite 一样。

This filter allows you to define rules like:

此过滤器允许您定义如下规则:

Clean a url

<rule>
    <from>/products/([0-9]+)</from>
    <to>/products/index.jsp?product_id=</to> 
</rule> 

eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing

清理一个网址

<rule>
    <from>/products/([0-9]+)</from>
    <to>/products/index.jsp?product_id=</to> 
</rule> 

例如, /products/1234 将被传递到 /products/index.jsp?product_id=1234 而不引起用户注意

from Tuckey urlrewrite example

来自Tuckey urlrewrite 示例

Since Tomcat 8 you can use the rewrite valve.

从 Tomcat 8 开始,您可以使用重写阀。

The rewrite valve implements URL rewrite functionality in a way that is very similar to mod_rewrite from Apache HTTP Server.

重写阀实现 URL 重写功能的方式与来自 Apache HTTP Server 的 mod_rewrite 非常相似。

Rewrite Valve docs

重写 Valve 文档

回答by sol4me

For Tomcat 7 you can also use tuckey. Tucket is a Java Web Filter for any compliant web application servers and allows mod_rewrite just like Apache

对于 Tomcat 7,您还可以使用tuckey。Tucket 是适用于任何兼容 Web 应用程序服务器的 Java Web 过滤器,并像 Apache 一样允许 mod_rewrite