Java 阻止web应用程序中文件的直接url访问
我有一个JavaWeb应用程序,其中图像存储在一个文件夹中 现在的问题是,用户可以通过下面的URL访问图像Java 阻止web应用程序中文件的直接url访问,java,Java,我有一个JavaWeb应用程序,其中图像存储在一个文件夹中 现在的问题是,用户可以通过下面的URL访问图像 http://localhost/Webapplication/images/image.jpg 我想阻止对web应用程序图像文件夹中的图像文件的直接URL访问。但是这些图像应该通过htlm页面显示 我在JBoss应用服务器上运行,已经在谷歌上搜索过了,但最终得到了.htaccess解决方案,这对我的java应用程序没有帮助。 任何帮助都将不胜感激。 谢谢 将此配置放在web.xml中
http://localhost/Webapplication/images/image.jpg
我想阻止对web应用程序图像文件夹中的图像文件的直接URL访问。但是这些图像应该通过htlm页面显示
我在JBoss应用服务器上运行,已经在谷歌上搜索过了,但最终得到了.htaccess解决方案,这对我的java应用程序没有帮助。
任何帮助都将不胜感激。
谢谢
将此配置放在web.xml中
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/Secured/*</url-pattern>
</servlet-mapping>
Facesservlet
/安全的/*
在HTML页面上显示的这些图像可能是重复的吗?你能考虑重定向作为选项吗?是的。这些图像显示在html页面中。如果我通过web.xml中的security constraints标记重新限制访问,它也会禁止通过html页面显示图像@nurzhano我想重定向不会解决我的问题@harshavmb
import java.io.IOException;
import javax.faces.application.ResourceHandler;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Md. Amran Hossain
*/
@WebFilter("/Secured/*")
public class AuthenticationFilter implements Filter {
private FilterConfig config;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (((HttpServletRequest) request).getSession().getAttribute(LoginController.AUTH_KEY) == null
&& !((HttpServletRequest) request).getRequestURI().endsWith("/Secured/login.xhtml")
&& !((HttpServletRequest) request).getRequestURI().contains("/Secured/temp/")
&& !((HttpServletRequest) request).getRequestURI().startsWith(((HttpServletRequest) request).getContextPath() + "/Secured" + ResourceHandler.RESOURCE_IDENTIFIER)) {
((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/Secured/login.xhtml");
} else {
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
this.config = null;
}
}
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/Secured/*</url-pattern>
</servlet-mapping>