【问题标题】:SpringBoot 2.2 still showing white label error when request "/*.jsp"Spring Boot 2.2 在请求“/*.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)

【问题讨论】:

    标签: spring-boot spring-mvc jsp


    【解决方案1】:

    删除

    @EnableWebMvc
    

    它显示了我的 404.jsp

    但我看到它仍然使用 ErrorMvcAutoConfiguration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 2017-12-26
      • 2018-06-01
      • 2022-01-22
      • 2022-01-10
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多