我找到了解决这个问题的方法。这是我想出的,
1) 将“文件”文件夹转换为 Web 应用程序并使用基于 tomcat 的身份验证来保护文件(比如 pdf)
2)在通过“myweb”身份验证后-这里的身份验证不是基于tomcat容器的,它基于spring和hibernate-
从“/myweb/customerhomepage.jsp”异步调用“文件”网络应用程序中的 servlet (PopulateServlet.java),并在“文件”网络应用程序会话中设置 tomcat 角色用户名和密码
每当在“文件”Web 应用程序下请求受保护的 pdf 时,将调用 login.jsp - 在此 jsp 中,从已由 PopulateServlet 填充的会话对象中填充隐藏的 j_username 和 j_password 字段。使用jquery ajax,将html表单提交给tomcat进行资源认证。
“文件”网络应用程序更改:
创建新角色和用户名和密码
/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());