Spring boot 跨域问题

分布式环境下跨域问题,包括websocket 请求 资源请求等。

1. 首先配一个 CorsConfigure ,添加 请求头信息,如下:

/*
 * web 跨域问题
 * author 
 */
@SuppressWarnings("deprecation")
@Configuration
public class CorsConfigure extends WebMvcConfigurerAdapter {


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("**").allowedOrigins("*").allowedMethods("POST, GET, HEAD, OPTIONS").allowCredentials(true)
                .allowedHeaders(
                        "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers")
                .exposedHeaders("Access-Control-Allow-Origin,Access-Control-Allow-Credentials").maxAge(10);
    }

2. 其次,然后 配置资源映射 继承 ResourceServerConfigurerAdapter

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().anonymous().and().authorizeRequests().antMatchers(HttpMethod.OPTIONS, "**").permitAll()
                .antMatchers("/authenticated/**").authenticated();
    }

OK ,完美解决!

猜你喜欢

转载自blog.csdn.net/m0_37598953/article/details/81777760