项目结构
添加WebCorsConfig
跨域类,实现WebMvcConfigurer
接口,同时加上@Configuration
注解
完整代码
package com.mzx.loginlist.config;
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 Mzx
* @create 2019-09-15 11:14
*/
@Configuration
public class WebCorsConfig implements WebMvcConfigurer {
/**
* 跨域问题解决
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")// 1 允许任何域名使用
.allowedHeaders("*")// 2 允许任何头
.allowedMethods("*");// 3 允许任何方法(post、get等)
}
/**
* 配置自定义类 LoginInterceptor 实现拦截登陆
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
// .addPathPatterns(new String[]{"/test"});
.addPathPatterns(new String[]{"/page/allUsers.html"});
// .addPathPatterns(new String[]{"/findAllUser"});
}
}
原文地址:https://www.cnblogs.com/xmg520/p/12275356.html
时间: 2024-10-07 15:27:52