All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cattleframework.webmvc.WebMvcAutoConfiguration Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cattleframework.webmvc;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.cattleframework.exception.web.ExceptionProcessCustomizer;
import org.cattleframework.exception.web.ExceptionProcessResponse;
import org.cattleframework.web.WebProperties;
import org.cattleframework.webmvc.exception.ExceptionAdvice;
import org.cattleframework.webmvc.exception.ExceptionController;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.Nullable;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler;
import org.springframework.security.web.firewall.RequestRejectedHandler;
import org.springframework.security.web.firewall.StrictHttpFirewall;
import org.springframework.web.ErrorResponse;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web Mvc自动配置
 * 
 * @author orange
 *
 */
@AutoConfiguration(before = ErrorMvcAutoConfiguration.class)
@EnableWebMvc
@EnableWebSecurity
public class WebMvcAutoConfiguration {

    @Value("${server.error.path:${error.path:/error}}")
    private String errorPath;

    @Bean
    public WebMvcConfigurer webMvcConfigurer(WebProperties webProperties) {
	return new CattleWebMvcConfigurer(webProperties);
    }

    @Bean
    public WebSecurityCustomizer webMvcIgnoringCustomizer(WebProperties webProperties) {
	Set securityIgnorePaths = new HashSet(Arrays.asList(errorPath, "/favicon.ico"));
	if (CollectionUtils.isNotEmpty(webProperties.getSecurityIgnorePaths())) {
	    securityIgnorePaths.addAll(webProperties.getSecurityIgnorePaths());
	}
	return web -> web.ignoring().requestMatchers(securityIgnorePaths.toArray(new String[0]));
    }

    @Bean
    @ConditionalOnMissingBean
    public HttpFirewall httpFirewall(WebProperties webProperties) {
	StrictHttpFirewall firewall = new StrictHttpFirewall();
	if (CollectionUtils.isNotEmpty(webProperties.getAllowedHttpMethods())) {
	    firewall.setAllowedHttpMethods(webProperties.getAllowedHttpMethods());
	}
	return firewall;
    }

    @Bean
    @ConditionalOnMissingBean
    public RequestRejectedHandler requestRejectedHandler() {
	return new HttpStatusRequestRejectedHandler();
    }

    @Bean
    public ExceptionProcessCustomizer errorResponseProcessCustomizer() {
	return new ExceptionProcessCustomizer() {

	    @Override
	    public Class getType() {
		return ErrorResponse.class;
	    }

	    @Override
	    public ExceptionProcessResponse customize(Throwable exception) {
		return new ExceptionProcessResponse(((ErrorResponse) exception).getStatusCode().value(),
			exception.getMessage());
	    }
	};
    }

    @Bean
    @ConditionalOnMissingBean
    public ExceptionAdvice exceptionAdvice(ErrorAttributes errorAttributes, WebProperties webProperties,
	    @Nullable ExceptionProcessCustomizer[] exceptionProcessCustomizers) {
	return new ExceptionAdvice(errorAttributes, webProperties, exceptionProcessCustomizers);
    }

    @Bean
    @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
    public ExceptionController exceptionController(ErrorAttributes errorAttributes, WebProperties webProperties,
	    @Nullable ExceptionProcessCustomizer[] exceptionProcessCustomizers) {
	return new ExceptionController(errorAttributes, webProperties, exceptionProcessCustomizers);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy