package com.seecoder.BlueWhale.Configure; import org.springframework.beans.factory.annotation.Autowired; 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.WebMvcConfigurer; /** * @Author: DingXiaoyu * @Date: 0:17 2023/11/26 * * 这个类实现了WebMvcConfigurer接口, * 表示会被SpringBoot接受, * 这个类有两个作用:配置拦截器和跨域。 * addInterceptors方法配置了拦截器, * 添加了loginInterceptor作为拦截器, * 并且设置除了register和login的所有接口都需要通过该拦截器。 * addCorsMappings方法配置了跨域, * 表示任何格式的请求都能通过, * 包括了请求地址、请求端域名、请求方法(GET、POST等)、请求头。 */ @Configuration public class MyWebMvcConfig implements WebMvcConfigurer { @Autowired LoginInterceptor loginInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(loginInterceptor) .addPathPatterns("/**") .excludePathPatterns("/api/user/register") .excludePathPatterns("/api/user/login") .order(1); } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("*") .allowedHeaders("*"); } }