springsecurity 跨域访问

这两天springsecurity跨域访问,怎么也不行,控制不了

后来发现是使用的类不对

,cors过滤记得要使用的是org.springframework.web.cors.CorsConfigurationSource,二不是org.springframework.web.cors.reactive.CorsConfigurationSource,使用了这个

跨域2种实现方式 注意使用的类路径,不要使用错了

1)在WebSecurityConfigurerAdapter中实现

@Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
//        configuration.setAllowedOrigins(Arrays.asList("http://192.168.1.188:3100"));
//        configuration.setAllowedOrigins(Arrays.asList("http://127.0.0.1:5500"));
        configuration.setAllowedOrigins(ignoredUrlsProperties.getCorsurls());
        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
protected void configure(HttpSecurity http) throws Exception {

//        http.cors(withDefaults());
        http.cors();}

第二种

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WZCorsFilter extends CorsFilter {

    public WZCorsFilter() {
        super(corsConfigurationSource());
    }

    public static CorsConfigurationSource corsConfigurationSource() {
        //这里似乎不能获取yml中的内容
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://192.168.1.188:3100"));
//        configuration.setAllowedOrigins(Arrays.asList("http://127.0.0.1:5500"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
        System.out.println("aaa");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

//    private static UrlBasedCorsConfigurationSource configurationSource() {
//        CorsConfiguration corsConfig = new CorsConfiguration();
//        List<String> allowedHeaders = Arrays.asList("x-auth-token", "content-type", "X-Requested-With", "XMLHttpRequest");
//        List<String> exposedHeaders = Arrays.asList("x-auth-token", "content-type", "X-Requested-With", "XMLHttpRequest");
//        List<String> allowedMethods = Arrays.asList("POST", "GET", "DELETE", "PUT", "OPTIONS");
//        List<String> allowedOrigins = Arrays.asList("http://127.0.0.1:5500");
        List<String> allowedOrigins = Arrays.asList("http://192.168.1.188:8082");
//        corsConfig.setAllowedHeaders(allowedHeaders);
//        corsConfig.setAllowedMethods(allowedMethods);
//        corsConfig.setAllowedOrigins(allowedOrigins);
//        corsConfig.setExposedHeaders(exposedHeaders);
//        corsConfig.setMaxAge(36000L);
//        corsConfig.setAllowCredentials(true);
//
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", corsConfig);
//        return source;
//    }

}

Spring Security Reference

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值