【问题标题】:Blocking direct url access of an file in web application阻止对 Web 应用程序中文件的直接 URL 访问
【发布时间】:2017-06-01 04:40:40
【问题描述】:

我有一个 java web 应用程序,其中图像存储在一个文件夹中。

现在的问题是,用户可以通过以下 URL 访问图像。

http://localhost/Webapplication/images/image.jpg 

我想阻止驻留在 Web 应用程序的图像文件夹中的图像文件的直接 URL 访问。但是这些图像应该通过 htlm 页面显示。

我在 JBoss 应用程序服务器上运行,已经在 Google 上搜索过,但最终得到了 .htaccess 解决方案,这对我的 java 应用程序没有帮助。 任何帮助将不胜感激。 谢谢

【问题讨论】:

  • 这些图片是否显示在 html 页面上?
  • 你可以考虑重定向吗?
  • 是的。这些图像显示在 html 页面中。如果我通过 web.xml 中的 security-constrains 标记限制访问,它还禁止通过 html 页面显示图像@Nurzhan
  • 不,我猜重定向不会解决我的问题@harshavmb

标签: java


【解决方案1】:
- 您刚刚在您的应用程序中使用了 AuthenticationFilter。 - AUTH_KEY 定义 LoginController 以获取 userId - @Secured 定义了 web.xml 文件以过滤此路径。 - @/Secured/temp/ 是为我的项目目录中的图像定义的。 - @/Secured/login.xhtml 在 servlet 配置初始后定义,它将 login.xhtml 重定向。

如下代码:

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;
}
}

把这个配置放到web.xml中

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/Secured/*</url-pattern>
</servlet-mapping>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多