java - Spring Web MVC Java 配置-默认 Servlet 名称

标签 java spring spring-mvc spring-security spring-java-config

我写了一个小应用来学习spring的java配置,因为我被同行唠叨了一段时间,现在升级我们的应用程序;-),一个简单的待办事项列表应用程序,它具有安全性和web mvc配置,JPA用于持久化,全部通过java配置。我在尝试运行应用程序时遇到问题。安全配置和 JPA 等工作正常,但在成功拦截 protected URL 后我得到一个空 View

主要的 Web 应用初始化程序类扩展 AbstractAnnotationConfigDispatcherServletInitializer

public class WiggleWebApplicationInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { WiggleApplicationConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {

        return new Class<?>[] { WiggleWebAppConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected void registerDispatcherServlet(ServletContext servletContext) {
        super.registerDispatcherServlet(servletContext);

        servletContext.addListener(new HttpSessionEventPublisher());

    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);

        return new Filter[] { characterEncodingFilter };
    }
}

WiggleApplicationConfig 导入安全、JPA 和社交

@Configuration
@ComponentScan(basePackages = { "wiggle.app.services.*" })
@Import({ WigglePersistenceConfig.class, WiggleSecurityConfig.class,
        WiggleSocialConfig.class })
public class WiggleApplicationConfig {

    @Bean
    public DateFormat dateFormat() {
        return new SimpleDateFormat("dd-MM-yyyy");
    }

}

Web 配置然后添加默认处理程序等

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "wiggle.app.controllers.*" })
public class WiggleWebAppConfig extends WebMvcConfigurerAdapter {

    private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/jsp/";
    private static final String VIEW_RESOLVER_SUFFIX = ".jsp";

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(
                "/static/");
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public SimpleMappingExceptionResolver exceptionResolver() {
        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();

        Properties exceptionMappings = new Properties();

        exceptionMappings.put("java.lang.Exception", "error/error");
        exceptionMappings.put("java.lang.RuntimeException", "error/error");

        exceptionResolver.setExceptionMappings(exceptionMappings);

        Properties statusCodes = new Properties();

        statusCodes.put("error/404", "404");
        statusCodes.put("error/error", "500");

        exceptionResolver.setStatusCodes(statusCodes);

        return exceptionResolver;
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix(VIEW_RESOLVER_PREFIX);
        viewResolver.setSuffix(VIEW_RESOLVER_SUFFIX);

        return viewResolver;
    }

}

所有这些都驻留在包 wiggle.app.config 中,根据我的配置 /** 是 protected ,应该重定向到/login,它对所有人开放,安全过滤器链工作正常,我看到访问被拒绝,之后重定向到/wiggle/login 当我访问主页时,我怎么会得到 404 以及以下日志条目,即 http://localhost:8080/Swing/

Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@6faeba70: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@fffbcba8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 8A7C29831E56336A6FDF1A0E19200E70; Granted Authorities: ROLE_ANONYMOUS 
Voter: org.springframework.security.web.access.expression.WebExpressionVoter@c01ac1b, returned: 1 
Authorization successful 
RunAsManager did not change Authentication object 
/login reached end of additional filter chain; proceeding with original chain 
DispatcherServlet with name 'dispatcher' processing GET request for [/wiggle/login] 
Looking up handler method for path /login 
Did not find handler method for [/login] 
Matching patterns for request [/login] are [/**] 
URI Template variables for request [/login] are {} 
Mapping [/login] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@688a42b5] and 1 interceptor 
Last-Modified value for [/wiggle/login] is: -1 
SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession. 
Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling 
Successfully completed request 
Chain processed normally 
SecurityContextHolder now cleared, as request processing completed 

我通常会将以下内容放在 XML 中以处理映射

<beans:bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- Enables annotated POJO @Controllers -->
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

 <!-- Scans within the base package of the application for @Components to configure as beans -->
 <context:component-scan base-package="com.code.controller" />

我无法找出我缺少什么来启用 Java 配置的类似行为。

最佳答案

原来我错过了一份重要的文档 w.r.t.此配置,来自 Spring Framework Docs16.16.8 mvc:default-servlet-handler 部分

The caveat to overriding the "/" Servlet mapping is that the RequestDispatcher for the default Servlet must be retrieved by name rather than by path. The DefaultServletHttpRequestHandler will attempt to auto-detect the default Servlet for the container at startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere). If the default Servlet has been custom configured with a different name, or if a different Servlet container is being used where the default Servlet name is unknown, then the default Servlet’s name must be explicitly provided as in the following example:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable("myCustomDefaultServlet");
   }

所以我改成了这个

@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    configurer.enable("wiggleServlet");
}

还有一个错误配置

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "wiggle.app.controllers.*" })
public class WiggleWebAppConfig extends WebMvcConfigurerAdapter {

应该是

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "wiggle.app.controllers" })
public class WiggleWebAppConfig extends WebMvcConfigurerAdapter {

关于java - Spring Web MVC Java 配置-默认 Servlet 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25164885/

相关文章:

hibernate - 找不到 org.springframework.security.authentication.UsernamePasswordAuthenticationToken 的 AuthenticationProvider

java - 限制类的直接实例数

java - 按多个字段对对象列表进行排序

java - 我的问题是关于 javax.xml.ws.WebServiceException : Undefined port type while trying to get port?

java - 无法将字段设置为 com.sun.proxy.$Proxy

spring - Spring Data REST 中的身份验证和授权

java - 将 OpenCV 代码从 C++ 转换为 Java

java - Android 中的 Post 请求 Spring Boot Rest 服务出现 I/O 错误

java - Hibernate @OneToOne 无法添加或更新子行 : a foreign key constraint

java - Spring REST Controller 在具有空格或逗号等特殊字符时理解字符串数组