MyWebMvcConfig.java 1.5 KB
Newer Older
Q
qinyingjie 已提交
1 2 3 4
package com.kwan.springbootkwan.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
Q
qinyingjie 已提交
5
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
Q
qinyingjie 已提交
6 7 8 9
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
Q
qinyingjie 已提交
10
 * 全局webconfig-跨域配置
Q
qinyingjie 已提交
11 12 13 14 15 16 17
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2022/12/19 16:11
 */
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
Q
qinyingjie 已提交
18 19 20 21 22
    /**
     * 配置静态资源
     *
     * @param registry
     */
Q
qinyingjie 已提交
23 24 25 26 27 28
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

Q
qinyingjie 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    /**
     * 配置拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/hello");
    }

    /**
     * 配置全局跨域
     *
     * @param registry
     */
Q
qinyingjie 已提交
46 47 48 49 50 51 52 53 54
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/book/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(1800)
                .allowedOrigins("http://localhost:8081");
    }
}