Spring Boot 2.2 在请求“/*.jsp”时仍然显示白标签错误

Posted

技术标签:

【中文标题】Spring Boot 2.2 在请求“/*.jsp”时仍然显示白标签错误【英文标题】:SpringBoot 2.2 still showing white label error when request "/*.jsp" 【发布时间】:2020-07-20 22:31:21 【问题描述】:

我有一个带有 jsp 的 Spring boot 2.2 web 项目

@Configuration
@EnableWebMvc
@Slf4j
public class WebConfig implements WebMvcConfigurer 


    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() 
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("language");
        return localeChangeInterceptor;
    

    @Override
    public void addInterceptors(InterceptorRegistry registry) 
        registry.addInterceptor(localeChangeInterceptor());
    

    @Bean
    public MessageSource messageSource() 
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:i18n/messages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    

    @Bean
    public LocalValidatorFactoryBean getValidator() 
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(messageSource());
        return bean;
    

    @Bean
    public ViewResolver jspViewResolver() 
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    


application.properties

spring.mvc.throw-exception-if-no-handler-found = true

GlobalExceptionHandler.java

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler 

    public static final String DEFAULT_ERROR_VIEW = "error/error";
    public static final String PAGE_NOT_FOUND_ERROR_VIEW = "error/404";


    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView pageNotFoundErrorHandler(HttpServletRequest req, Exception e) 
        if (log.isDebugEnabled()) 
            log.debug("The page [] does not exist : ", req.getRequestURI(), e.getMessage());
        
        return getMAV(req, e, HttpStatus.NOT_FOUND, PAGE_NOT_FOUND_ERROR_VIEW);
    


    private ModelAndView getMAV(HttpServletRequest req, Exception e, HttpStatus status, String viewName) 
        ModelAndView mav = new ModelAndView();
        mav.addObject("error", status.getReasonPhrase());
        mav.addObject("exception", e.getMessage());
        mav.addObject("url", req.getRequestURL());
        mav.setStatus(status);
        mav.setViewName(viewName);
        return mav;
    

当我去 localhost:8080/notMapped 它会显示我的错误页面, 但如果我去,我去 localhost:8080/notMapped.jsp 它显示白标错误 我试过删除

@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)

不起作用(显示 tomcat 404)...

试着放

@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurity extends WebSecurityConfigurerAdapter 

    @Bean
    public AccessDeniedHandler accessDeniedHandler()
        return new ApplicationAccessDeniedHandler();
    

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        log.info("Overriding default configure to bypass default login");
        **http
                .authorizeRequests()
                .antMatchers("/*.jsp")
                .denyAll();**

        http
                .authorizeRequests()
                .anyRequest()
                .permitAll();

        http
                .headers()
                .frameOptions()
                .sameOrigin();
    


只有白标中的错误代码改变了 (404 -> 403)

【问题讨论】:

【参考方案1】:

删除

@EnableWebMvc

它显示了我的 404.jsp

但我看到它仍然使用 ErrorMvcAutoConfiguration

【讨论】:

以上是关于Spring Boot 2.2 在请求“/*.jsp”时仍然显示白标签错误的主要内容,如果未能解决你的问题,请参考以下文章

android+spring boot 选择,上传,下载文件

在 Spring Boot 中将未知请求重定向到 index.html

使用 axios 从 react js 调用 Spring Boot get 方法时,浏览器显示“请求已被 CORS 策略阻止”

Ajax 请求未调用 Spring Boot 控制器

spring boot 2.2中默认的rest api超时是啥

关于spring-boot-actuator的httptrace端点不生效问题解决办法