首页>代码>基于SpringBoot+Vue实现的在线考试系统>/OnlineExamSystem/OnlineExamSystemApi/src/main/java/com/rabbiter/oes/config/MvcConfig.java
package com.rabbiter.oes.config;

import com.rabbiter.oes.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    /**
     * 配置拦截器
     * @param registry 相当于拦截器的注册中心
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns()
                .excludePathPatterns("/login");
    }


    @Bean
    public CorsFilter corsFilter() {
        // 1.创建 CORS 配置对象
        CorsConfiguration config = new CorsConfiguration();
        // 支持域
        config.addAllowedOrigin("*");
        // 是否发送 Cookie
        config.setAllowCredentials(true);
        // 支持请求方式
        config.addAllowedMethod("*");
        // 允许的原始请求头部信息
        config.addAllowedHeader("*");
        // 2.添加地址映射
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", config);
        // 3.返回 CorsFilter 对象
        return new CorsFilter(corsConfigurationSource);
    }
}
最近下载更多
coder_f  LV2 3月5日
ma406805131  LV19 2024年12月27日
wen435301369  LV17 2024年12月7日
Daima000  LV4 2024年12月3日
citybird  LV4 2024年11月18日
wuying8208  LV15 2024年10月23日
withyouatdusk  LV2 2024年10月20日
泓鼎168  LV20 2024年5月24日
xuweisong2010  LV28 2024年5月15日
最近浏览更多
luhong  LV2 3月31日
coder_f  LV2 3月5日
暂无贡献等级
暂无贡献等级
docnnxxy688 3月1日
暂无贡献等级
哪里的完整版  LV8 2月27日
usb001  LV8 2月26日
z96141176 2月23日
暂无贡献等级
陈小灏  LV18 2月21日
kkkxyh  LV13 2月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友