【问题标题】:How to programmatically setup a <security-constraint> in Servlets 3.x?如何在 Servlets 3.x 中以编程方式设置 <security-constraint>?
【发布时间】:2013-10-10 13:56:42
【问题描述】:

在我当前的 Web 应用程序中,我试图摆脱 web.xml,但我无法正确设置强制对应用程序的所有请求都使用 HTTPS 的安全约束。

<security-constraint>
  <web-resource-collection>
     <web-resource-name>all</web-resource-name>
     <url-pattern>/*</url-pattern>
  </web-resource-collection>
  <user-data-constraint>
     <transport-guarantee>CONFIDENTIAL</transport-guarantee>
  </user-data-constraint>
</security-constraint>

如何把上面的web.xml配置sn-p在servlet 3.x配置代码中做同样的事情?

更新

我希望约束应用于应用程序中的每个 servlet、过滤器和静态资源,到目前为止我在网上看到的示例显示将安全约束附加到 servlet,但我希望将安全约束附加到 Web 应用程序.在上面的 xml sn-p 中,您会看到它没有引用任何特定的 servlet

【问题讨论】:

  • 我们正在将基于 Spring XML 的配置迁移到基于类的配置。我还在为上述 xml 配置 (web.xml) 寻找等效的 java 配置。如果你已经有,请分享。

标签: java servlets servlet-3.0


【解决方案1】:

相信你正在寻找@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);

【讨论】:

  • 我希望对所有资源的约束而不是对特定资源的约束,因为我希望这个约束适用于 .css、.js、.jsp、.html,无论我只想要 https。
  • @ams 看着ServletContext的界面,没找到办法。
  • 似乎有时规范设计者会忘记规范中的内容。
【解决方案2】:

通过配置 glassfish 域安全性,我能够为一个项目做到这一点:

  1. 创建一个新的安全域,在本例中称之为:FooRealm
  2. 将带有(或不带)密码的用户添加到 FooRealm
  3. 将每个用户添加到“GroupFoo”

这涵盖了您的 glassfish 配置,这是您的 web.xml:

<security-constraint>
    <display-name>SecurityConstraint</display-name>
    <web-resource-collection>
        <web-resource-name>Everything</web-resource-name>
        <description>Everything</description>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description>UserAuthenticationConstraint</description>
        <role-name>GroupFoo</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>FooRealm</realm-name>
    <form-login-config>
        <form-login-page>/Login.jsp</form-login-page>
        <form-error-page>/LoginError.html</form-error-page>
    </form-login-config>
</login-config>

【讨论】:

    【解决方案3】:

    如果您在部署到 JBoss 或 WildFly(基于 Undertow 的服务器)之后有一个解决方案。

    将 ServletContainerInitializer 或 WebApplicationInitializer 添加到您的项目中。

    onStartup(Set&lt;Class&lt;?&gt;&gt; c, ServletContext ctx)onStartup(ServletContext ctx)

    io.undertow.servlet.spec.ServletContextImpl servletContextImpl = (ServletContextImpl) ctx;
    io.undertow.servlet.api.Deployment deployment = (DeploymentImpl) servletContextImpl.getDeployment();
    DeploymentInfo deploymentInfo = deployment.getDeploymentInfo();
    deploymentInfo.addSecurityConstraint(Servlets.securityConstraint()
                        .addRoleAllowed("*")
                        .addWebResourceCollections(Servlets.webResourceCollection().addUrlPattern("/*")));
    
    //auth-mode 
    deploymentInfo.setLoginConfig(Servlets.loginConfig("BASIC", null));
    //deploymentInfo.setLoginConfig(Servlets.loginConfig("SPNEGO", "SPNEGO"));
    
    deploymentInfo.addSecurityRole("*");
    deploymentInfo.setSecurityDisabled(false);
    
    ....
     //ur Servlets go here
     ServletRegistration.Dynamic servlet = ctx.addServlet("rwtServlet", "org.eclipse.rap.rwt.engine.RWTServlet");
    
     servlet.addMapping("/rap");
    
     ctx.addListener("org.eclipse.rap.rwt.engine.RWTServletContextListener");
    
    

    注意:确保添加undertow-servlet作为编译时依赖

    <dependency>
        <groupId>io.undertow</groupId>
        <artifactId>undertow-servlet</artifactId>
        <version>2.0.30.Final</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-26
      • 2023-03-10
      • 2020-08-07
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      相关资源
      最近更新 更多