WebMvcConfigurer类的用法

/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 ([email protected])
 */
package org.springblade.common.config;


import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import org.springblade.common.filter.PreviewFilter;
import org.springblade.common.interceptor.SessionInterceptor;
import org.springblade.core.secure.registry.SecureRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Blade配置
 *
 * @author Chill
 */
@Configuration
public class BladeConfiguration implements WebMvcConfigurer {
    @Resource
    SessionInterceptor sessionInterceptor;
    @Value("${file.path}")
    private String filePath;

    @Bean
    public SecureRegistry secureRegistry() {
        SecureRegistry secureRegistry = new SecureRegistry();
        secureRegistry.setEnabled(true);
        secureRegistry.excludePathPatterns("/blade-auth/**");
        secureRegistry.excludePathPatterns("/blade-log/**");
        secureRegistry.excludePathPatterns("/blade-system/menu/routes");
        secureRegistry.excludePathPatterns("/blade-system/menu/auth-routes");
        secureRegistry.excludePathPatterns("/blade-system/menu/top-menu");
        secureRegistry.excludePathPatterns("/blade-system/tenant/info");
        secureRegistry.excludePathPatterns("/blade-flow/process/resource-view");
        secureRegistry.excludePathPatterns("/blade-flow/process/diagram-view");
        secureRegistry.excludePathPatterns("/blade-flow/manager/check-upload");
        secureRegistry.excludePathPatterns("/doc.html");
        secureRegistry.excludePathPatterns("/js/**");
        secureRegistry.excludePathPatterns("/webjars/**");
        secureRegistry.excludePathPatterns("/swagger-resources/**");
        secureRegistry.excludePathPatterns("/druid/**");
        return secureRegistry;
    }

    @Bean
    @ConditionalOnProperty(value = "blade.preview.enabled", havingValue = "true")
    public PreviewFilter previewFilter() { //这个是注册过滤器
        return new PreviewFilter();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {//解决跨域的问题
        registry.addMapping("/cors/**")
            .allowedOrigins("*")
            .allowedHeaders("*")
            .allowedMethods("*")
            .maxAge(3600)
            .allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {//将访问路径映射到磁盘路径

//        registry.addResourceHandler("/upload/**").addResourceLocations("file:d:/gangtong/upload/drugferment/resource/");
        registry.addResourceHandler("/file/**").addResourceLocations(filePath);
        //registry.addResourceHandler("/showImg/**").addResourceLocations("file:d:/gangtong/upload/drugferment/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {//添加拦截器

        registry.addInterceptor(sessionInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/blade-auth/oauth/token","/blade-auth/oauth/captcha","/blade-auth/oauth/logout");

    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer builderCustomizer() { // jackson的全局配置 日期和long变成string(解决mybatis-plus 生成主健到前台失去精度的问题)
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateTimeSerializeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateTimeDeserializeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        return builder -> {
            // 所有Long类型转换成String到前台
            builder.serializerByType(Long.class, ToStringSerializer.instance);
            builder.serializerByType(LocalDateTime.class, new LocalDateSerializer(dateTimeSerializeFormatter));
        };
    }

}

猜你喜欢

转载自www.cnblogs.com/dkws/p/13208427.html