繁体   English   中英

当应用程序没有web.xml时如何强制重定向到HTTPS

[英]How to force redirect to HTTPS when application has no web.xml

我正在使用Servlet 3.x开发Web应用程序,但是没有web.xml,而是使用配置注释。 我想将所有流量限制为在Openshift中部署为HTTPS的应用程序。 我在Tomcat(Jboss EWS)中找到了有关如何执行此操作的文章。

https://help.openshift.com/hc/zh-CN/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>

当前,无法使用@ServletSecurity注释映射<url-pattern>/*</url-pattern> 它仅适用于@WebServlet定义的模式。

您可以定义如下注释:

@ServletSecurity(@HttpConstraint(transportGuarantee = 
TransportGuarantee.CONFIDENTIAL))
@WebServlet(”/SSLServlet”)
public class SSLProtected extends HttpServlet {
}

但是,它仅对访问that servlet (/ SSLServlet模式)而不是整个应用程序提出SSL要求。 您可以为所有servlet定义此类注释,但是比起使用web.xml ,它更容易出错。 因此,仍建议使用web.xml而不是注释来定义安全约束。

只需使用您提供的片段将web.xml添加到应用程序中的WEB-INF文件夹中,您就可以了。 为什么不能使用web.xml任何原因?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM