我正在使用 Servlet 3.x 开发 Web 应用程序,但我没有 web.xml,而是使用配置注释。我想将部署在 Openshift 中的应用程序的所有流量限制为 HTTPS。我找到了这篇关于如何在 Tomcat(Jboss EWS) 中执行此操作的文章。
https://help.openshift.com/hc/en-us/articles/202398810-How-to-redirect-traffic-to-HTTPS-
但是最后一步涉及我更改我没有的 web.xml。当我试图寻找替代方案时,我可以找到有关 Servlet 安全性的文章。有人可以帮我翻译下面这段代码吗?
A sample security-constraint directive in repo/src/main/webapp/WEB-INF/web.xml looks like:
<security-constraint>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
最佳答案
目前无法映射 <url-pattern>/*</url-pattern>
使用 @ServletSecurity
注解。它仅适用于 @WebServlet
中定义的模式.
你可以这样定义注解:
@ServletSecurity(@HttpConstraint(transportGuarantee =
TransportGuarantee.CONFIDENTIAL))
@WebServlet(”/SSLServlet”)
public class SSLProtected extends HttpServlet {
}
然而,它只会对访问 that servlet
提出 SSL 要求(/SSLServlet 模式),而不是整个应用程序。您可以为所有 servlet 定义此类注释,但它比使用 web.xml
更容易出错。 .
这就是为什么仍然建议使用 web.xml
的原因。而不是注释来定义安全约束。
只需添加 web.xml
使用您提供的片段到应用程序中的 WEB-INF 文件夹,您会很好。您不能使用 web.xml
的任何原因?
关于java - 当应用程序没有 web.xml 时如何强制重定向到 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26167174/