Java Spring 如何在 webapplicationinitializer 中强制使用 https ssl?

标签 java spring https spring-java-config

为了在 web.xml 中强制使用 https,我使用了这个代码片段:

<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>

在 Spring Java Config 中有对应的吗?我已经知道我需要一个 ServletSecurityElement。但是我如何它连接到其余部分?

public class WebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        container.addListener(new ContextLoaderListener(context));
        context.register(PersistenceJPAConfig.class);

        FilterRegistration filter = container.addFilter("wicket.myproject", WicketFilter.class);
        filter.setInitParameter("applicationClassName", WicketApplication.class.getName());
        filter.setInitParameter(WicketFilter.FILTER_MAPPING_PARAM, "/*");
        filter.addMappingForUrlPatterns(null, false, "/*");

        HttpConstraintElement forceHttpsConstraint = new HttpConstraintElement(ServletSecurity.TransportGuarantee.CONFIDENTIAL, "");
        ServletSecurityElement securityElement = new ServletSecurityElement(forceHttpsConstraint);
    }
}

最佳答案

正如 John Thompson 指出的那样,您就在那里。您只需要将您定义的安全元素添加到 servlet。另一方面,我注意到您将 "" 作为 HttpConstraintElement 的 roleNames 参数。这实际上会导致所有没有 "" 角色的人都被拒绝。如果您希望它正常工作(强制 https),请不要提供任何角色。最后这对我有用:

public class ApplicationInitializer implements WebApplicationInitializer {

    private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
    private static final String DISPATCHER_SERVLET_MAPPING = "/";

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(ApplicationConfiguration.class);

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(dispatcherContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping(DISPATCHER_SERVLET_MAPPING);

        // Force HTTPS, and don't specify any roles for this constraint
        HttpConstraintElement forceHttpsConstraint = new HttpConstraintElement(ServletSecurity.TransportGuarantee.CONFIDENTIAL);
        ServletSecurityElement securityElement = new ServletSecurityElement(forceHttpsConstraint);

        // Add the security element to the servlet
        dispatcher.setServletSecurity(securityElement);
    }
}

关于Java Spring 如何在 webapplicationinitializer 中强制使用 https ssl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509275/

相关文章:

amazon-web-services - 将 HTTPS/SSL 添加到指向 AWS 实例的域

http - Angular + Web Api 上的 Windows 身份验证

tomcat - 将 HTTP 转换为 HTTPs

java - 如何从标准输入读取直到 EOF

java - 数据库拒绝删除项目

javascript - 如何与spring websocket通信

java - 使用 Spring MVC RequestMappingHandlerMapping 和 Spring Websocket 的 ServletWebSocketHandlerRegistry 处理相同的 URL

java - 使用循环进行编程以实现数学目的。 ( java )

java - 如何将 Joda 库的 DateTime 舍入到最接近的 X 分钟?

java - next() 与 Single() 哪个有效?