日期:2014-05-16 浏览次数:20910 次
String referer = request.getHeader("referer");
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = request = (HttpServletRequest) req; HttpServletResponse response = response = (HttpServletResponse) resp; String referer = request.getHeader("referer"); if(referer == null || isLegalUrl(referer)){ chain.doFilter(req, resp); //合法referer可继续执行 }else{ System.out.println("非法请求来源,转到首页"); //转到登录页面或首页 } }
<filter> <filter-name>ResourceAccessFilter</filter-name> <filter-class>com.redgateonline.daren.web.filter.ResourceAccessFilter</filter-class> </filter> <filter-mapping> <filter-name>ResourceAccessFilter</filter-name> <url-pattern>*.mp3</url-pattern> </filter-mapping>
<a href="http://localhost:8080/webapp2/sss.mp3">下载</a>
非法请求来源,转到首页
<a href="http://localhost:8080/webapp2/sss.mp3">下载</a>你这个8080端口是apache还是tomcat的?如果是apache的,当然filter不起作用,如果是tomcat的,应该会起作用吧
#LoadModule rewrite_module modules/mod_rewrite.so
<VirtualHost *:80> DocumentRoot C:/eclipseworkspace/daren/web ServerName www.webapp2.com //以下是配置url重写 RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://www.webapp2.com/demo/.*$ [NC] //NC是忽略大小写 RewriteRule \.(mp3)$ http://www.sohu.com [R=301,L] //R是redirect L是link </VirtualHost>
RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://www.webapp2.com/demo/.*$ [NC]
RewriteRule \.(mp3)$ http://www.sohu.com [R=301,L]