security - Tomcat 7 - 保护 webapps 文件夹下的文件夹

标签 security authentication tomcat

我的网络应用程序是“myweb”,在此网络应用程序中,我的代码引用“files”文件夹下的“123.pdf”,例如 http://localhost:8080/files/123.pdf

   webapps  
   |  
   |--myweb  
   |  
   |--files  
       |  
       |--123.pdf  

当我尝试通过粘贴 (http://localhost:8080/files/123.pdf) 直接访问时,我希望资源 (123.pdf) 仅可供登录用户使用通过浏览器地址栏,无需登录门户,我就可以访问该文件。

基本上,我想保护“webapps”下的“files”文件夹,以便只有门户中经过身份验证的用户才能访问“files”文件夹下的资源。我怎样才能实现这个目标?

最佳答案

我找到了解决这个问题的方法。这是我想出来的,

1) 将“files”文件夹转换为 Web 应用程序,并使用 tomcat 保护文件(例如 pdf) 基于 FORM 的身份验证

2) 获得“myweb”身份验证后 - 这里的身份验证不是基于 tomcat 容器,而是基于 Spring 和休眠 -

从“/myweb/customerhomepage.jsp”异步调用“files”Web 应用程序中的 servlet (PopulateServlet.java),并在“files”Web 应用程序 session 中设置 tomcat 角色用户名和密码

每当在"file"Web 应用程序下请求 protected pdf 时,都会调用 login.jsp - 在此 jsp 中填充隐藏的 j_username 和 j_password 字段 来自已由 PopulateServlet 填充的 session 对象。使用jquery ajax,将html表单提交到tomcat 用于资源认证。

"file"网络应用更改:

创建新角色以及用户名和密码
/conf/tomcat-users.xml

    <role rolename="tomcat"/>
    <user username="tomcat" password="tomcat" roles="tomcat"/>

创建 WEB-INF/web.xml

    <servlet>
    <servlet-name>Populate</servlet-name>
    <servlet-class>PopulateServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Populate</servlet-name>
    <url-pattern>/Populate</url-pattern>
    </servlet-mapping>

    <servlet>
    <servlet-name>Logout</servlet-name>
    <servlet-class>LogOutServlet</servlet-class> <!-- in this servlet, call session.invalidate() -->
    </servlet>
    <servlet-mapping>
    <servlet-name>Logout</servlet-name>
    <url-pattern>/Logout</url-pattern>
    </servlet-mapping>

<security-constraint>
  <display-name>Security Constraint</display-name>
  <web-resource-collection>
     <web-resource-name>Protected Area</web-resource-name>
     <url-pattern>/jsp/security/protected/*</url-pattern>
     <url-pattern>*.pdf</url-pattern>

     <http-method>DELETE</http-method>
     <http-method>GET</http-method>
     <http-method>POST</http-method>
     <http-method>PUT</http-method>
  </web-resource-collection>
  <auth-constraint>
     <role-name>tomcat</role-name>
  </auth-constraint>
</security-constraint>

<login-config>
  <auth-method>FORM</auth-method>
  <realm-name>Form-Based Authentication Area</realm-name>
  <form-login-config>
    <form-login-page>/jsp/security/protected/login.jsp</form-login-page>
    <form-error-page>/jsp/security/protected/error.jsp</form-error-page>
  </form-login-config>
</login-config>

<!-- Security roles referenced by this web application -->
<security-role>
  <role-name>tomcat</role-name>
</security-role>

在/files/jsp/security/protected/下创建login.jsp和error.jsp

登录.jsp

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#secure").submit();
});
</script>
...
<form method="POST" action='<%= response.encodeURL("j_security_check") %>' name="secure" id="secure">
<input type="hidden" name="j_username" value='<%=session.getAttribute("j_username")%>' />
<input type="hidden" name="j_password" value='<%=session.getAttribute("j_password")%>' />
</form>
...

PopulateServlet.java

HttpSession session = request.getSession(true);
session.setAttribute("j_username","tomcat");
session.setAttribute("j_password","tomcat");

“myweb”网络应用程序更改: customerhomepage.jsp

$.get('/files/Populate?ts='+new Date().getMilliseconds());

关于security - Tomcat 7 - 保护 webapps 文件夹下的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18142353/

相关文章:

php - 如何将 get 字符串转换为 slug?

security - InfoPath -如何禁用 Microsoft Office InfoPath 安全声明 - "Microsoft has identified a potential security concern"

Postgresql - 针对 Active Directory (AD) 的 LDAP 身份验证 - 来自 linux 服务器的问题,而来自 windows 服务器的正常

Java & MySQL - 创建登录认证

tomcat - Servlet 容器优于 Tomcat

security - Groovy/Grails:有没有办法使.evaluate()完全安全?

spring - 配置 Spring Boot 以防止 Logjam 攻击

java - 如何从具有多个模块的项目中获取正确的 list ? [JAVA]

authentication - 2 路 SSL 的替代方案

Tomcat内存设置