如何在Servlets 3.x中以编程方式设置?
在我当前的Web应用程序中,我试图摆脱web.xml,但我无法正确设置强制所有对应用程序的请求使用HTTPS的安全约束。
all /* CONFIDENTIAL
如何在servlet 3.x配置代码中执行相同操作的上述web.xml配置代码段?
UPDATE
我希望约束应用于应用程序中的每个servlet,filter和静态资源,到目前为止我在网上看到的示例显示将安全约束附加到servlet,但我希望将安全约束附加到Web应用程序。 在上面的xml片段中,您会看到它不引用任何特定的servlet
我相信你正在寻找@ServletSecurity
注释
@WebServlet(urlPatterns = "/*") @ServletSecurity(value = @HttpConstraint(transportGuarantee = TransportGuarantee.CONFIDENTIAL)) public class SomeServlet extends HttpServlet { ... }
或者在ServletContainerInitializer
使用ServletRegistration
(或者您可以访问ServletContext
任何地方)
ServletRegistration.Dynamic dynamic = context.addServlet("someServlet", SomeServlet.class); dynamic.addMapping("/*"); HttpConstraintElement httpConstraintElement = new HttpConstraintElement(TransportGuarantee.CONFIDENTIAL); ServletSecurityElement servletSecurityElement = new ServletSecurityElement(httpConstraintElement); dynamic.setServletSecurity(servletSecurityElement);
通过配置glassfish域安全性,我能够为项目执行此操作:
- 创建一个新的安全域,在本例中称之为:FooRealm
- 将用户w(或w / o)密码添加到FooRealm
- 将每个用户添加到“GroupFoo”
这涵盖了你的glassfish配置,这是你的web.xml:
SecurityConstraint Everything Everything /* UserAuthenticationConstraint GroupFoo FORM FooRealm /Login.jsp /LoginError.html