Questions tagged «servlet-filters»

在Servlet API中,当您要控制,预处理和/或后处理特定请求时,通常可以使用Servlet。但是,当您要根据特定条件过滤/修改常见的请求和/或响应时,则“过滤器”更为合适。


3
如何在Java中使用Servlet过滤器来更改传入的Servlet请求网址?
如何使用Servlet过滤器来更改来自的传入Servlet请求网址 http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123 至 http://nm-java.appspot.com/Check_License?Contact_Id=My_Obj_123 ? 更新:根据BalusC的以下步骤,我想到了以下代码: public class UrlRewriteFilter implements Filter { @Override public void init(FilterConfig config) throws ServletException { // } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; String requestURI = request.getRequestURI(); if (requestURI.startsWith("/Check_License/Dir_My_App/")) { String toReplace = …


7
我可以从<filter-mapping>内部的<url-pattern>中排除一些具体的url吗?
我希望对所有网址应用一种具体的过滤器,除了一种具体的(即/*除外/specialpath)。 有可能这样做吗? 样例代码: &lt;filter&gt; &lt;filter-name&gt;SomeFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.somproject.AFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;SomeFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;!-- the question is: how to modify this line? --&gt; &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt; &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt; &lt;/filter-mapping&gt;

8
使用Servlet过滤器修改请求参数
现有的Web应用程序正在Tomcat 4.1上运行。页面存在XSS问题,但是我无法修改源代码。我决定编写一个servlet过滤器以在页面看到参数之前对其进行清理。 我想这样编写一个Filter类: import java.io.*; import javax.servlet.*; public final class XssFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { String badValue = request.getParameter("dangerousParamName"); String goodValue = sanitize(badValue); request.setParameter("dangerousParamName", goodValue); chain.doFilter(request, response); } public void destroy() { } public void init(FilterConfig filterConfig) { } …

12
一次读取后,Http Servlet请求会丢失POST正文中的参数
我正在尝试访问Java Servlet过滤器中的两个http请求参数,这里没有新内容,但是很惊讶地发现这些参数已经被消耗了!因此,它在过滤器链中不再可用。 似乎只有在参数进入POST请求正文(例如表单提交)时才会发生这种情况。 有没有办法读取参数而不消耗它们? 到目前为止,我只找到了以下参考:使用request.getParameter的Servlet过滤器会丢失Form数据。 谢谢!

3
如何从Java过滤器获取请求URL?
我正在尝试编写一个可以检索请求URL的过滤器,但是我不确定该怎么做。 这是我到目前为止的内容: import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; public class MyFilter implements Filter { public void init(FilterConfig config) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { chain.doFilter(request, response); String url = ((HttpServletRequest) request).getPathTranslated(); System.out.println("Url: " + url); } public void destroy() { …

2
为Servlet过滤器提供多个URL模式
我在JSF应用程序中使用了Servlet过滤器。我的应用程序中有三组Web页面,我想在Servlet过滤器中检查这些页面的Authentication: 我的资料夹 /Admin/ *.xhtml /Supervisor/*.xhtml /Employee/*.xhtml 我写得web.xml像 &lt;filter&gt; &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt; &lt;filter-class&gt;com.ems.admin.servlet.LoginFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/Employee/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/Admin/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/Supervisor/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; 但要求像 http://localhost:8080/EMS2/faces/Html/Admin/Upload.xhtml 没有进入过滤器。 我必须为这3个文件夹提供安全性。 如何解决这个问题呢 ?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.