如何将web.xml的某些部分更改为Java配置

问题描述

我尝试用java config替换web.xml(我使用Spring,但没有Spring Boot)。我移动了web.xml的某些部分,例如添加了过滤器,servlet和侦听器,如下面的代码所示:

public class Servletinitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws servletexception {
      AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
      context.register(MyApplication.class);
      servletContext.addListener(new ContextLoaderListener(context));

      addFilters(servletContext);
      addServlets(servletContext,context);
    }

    private void addServlets(ServletContext servletContext,AnnotationConfigWebApplicationContext context) {
      ServletRegistration.Dynamic dispatcher = servletContext
            .addServlet("dispatcherServlet",new dispatcherServlet(context));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");

      ServletRegistration.Dynamic mvcServlet = servletContext
            .addServlet("MvcServlet",new dispatcherServlet(context));
      mvcServlet.setLoadOnStartup(1);
      mvcServlet.addMapping("/mvc/*");
    }

    private void addFilters(ServletContext servletContext) {
      servletContext.addFilter("characterEncoding",new CharacterEncodingFilter("UTF-8",true))
            .addMappingForUrlPatterns(EnumSet.of(dispatcherType.REQUEST,dispatcherType.FORWARD),false,"/*");

      servletContext.addFilter("springSecurityFilterChain",DelegatingFilterProxy.class)
            .addMappingForUrlPatterns(null,"/*");

      servletContext.addFilter("i18nOutputFilter",DelegatingFilterProxy.class)
            .addMappingForUrlPatterns(
                  EnumSet.of(dispatcherType.REQUEST,"/*");
    }
}

不幸的是,我不知道该如何处理这些元素:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Unsecured website</web-resource-name>
        <url-pattern>/mvc/extNavigation/*</url-pattern>
        <http-method>GET</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>authenticated</role-name>
    </auth-constraint>
</security-constraint>

<security-role>
    <description>Access to use the Secured web site - authenticated</description>
    <role-name>authenticated</role-name>
</security-role>

<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/loginSSO.jsp</form-login-page>
        <form-error-page>/erroRSSO.jsp</form-error-page>
    </form-login-config>
</login-config>

您知道解决方案吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)