SyA*_*yAu 2 security authentication tomcat
我的网络应用程序是“myweb”,在这个网络应用程序中,我的代码指的是“文件”文件夹下的“123.pdf”,如 http:// localhost
:8080/files/123.pdf
webapps
|
|--myweb
|
|--files
|
|--123.pdf
Run Code Online (Sandbox Code Playgroud)
我希望资源(123.pdf)仅对登录用户可用,当我尝试通过localhost
在浏览器地址栏中粘贴(http:// :8080/files/123.pdf)直接访问时,无需登录门户,我可以访问该文件。
基本上我想保护“webapps”下的“files”文件夹,以便只有门户中经过身份验证的用户才能访问“files”文件夹下的资源。我怎样才能做到这一点?
我找到了解决这个问题的方法。这是我想出来的
1) 将“文件”文件夹转换为 Web 应用程序,并使用基于 FORM 的身份验证来保护文件(例如 pdf)
2) 在通过“myweb”身份验证后 - 这里的身份验证不是基于 tomcat 容器,而是基于 spring & hibernate -
从“/myweb/customerhomepage.jsp”异步调用“files”Web应用程序中的servlet(PopulateServlet.java),并在“files”Web应用程序会话中设置tomcat角色用户名和密码
每当在“文件”网络应用程序下有对受保护 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"/>
Run Code Online (Sandbox Code Playgroud)
创建 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>
Run Code Online (Sandbox Code Playgroud)
在/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>
...
Run Code Online (Sandbox Code Playgroud)
填充Servlet.java
HttpSession session = request.getSession(true);
session.setAttribute("j_username","tomcat");
session.setAttribute("j_password","tomcat");
Run Code Online (Sandbox Code Playgroud)
“myweb”网络应用程序更改:customerhomepage.jsp
$.get('/files/Populate?ts='+new Date().getMilliseconds());
Run Code Online (Sandbox Code Playgroud)