spring boot 使用spring security 配置cors时无效,解决办法

spring boot 跨域方式有多种,本文采用 实现WebMvcConfigurer接口,其他方式可自行百度

步骤

1 实现WebMvcConfigurer接口,重写addCorsMappings方法

/**

* web 配置类型

*/

@Configuration

publicclassWebConfigimplementsWebMvcConfigurer{

/**

    * Cors 跨域处理

    *

    * @param registry

    */

@Override

publicvoidaddCorsMappings(CorsRegistryregistry){

registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowedHeaders("*").exposedHeaders(HttpHeaders.SET_COOKIE).allowCredentials(true).maxAge(1800);

}

}

一般情况下 增加这个设置基本就已经是解决了 跨域问题。 但是在用了spring security 的后,发现不好使了。

步骤二 这一步非常关键:

publicclassSecurityConfigextendsWebSecurityConfigurerAdapter{

/**

    * 主配置方法

    *

    * @param http

    * @throws Exception

    */

@Override

protectedvoidconfigure(HttpSecurityhttp)throwsException{

// fixme: 生产环境应参照官方方式打开csrf防护

http.authorizeRequests()

.antMatchers("/rest/system/login","/swagger-ui.html/**","/webjars/**","/v2/api-docs","/swagger-resources/**").permitAll()

.antMatchers("/rest/**").hasAuthority("admin")

.anyRequest().authenticated()

.and().exceptionHandling().accessDeniedHandler(accessDeniedHandler()).authenticationEntryPoint(authenticationEntryPointHandler())

.and().cors() // 这句非常重要了

.and().formLogin().loginProcessingUrl("/rest/system/login").successHandler(authenticationSuccessHandler()).failureHandler(authenticationFailureHandler())

.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and().csrf().disable();

http.addFilterBefore(authenticationTokenFilter(),UsernamePasswordAuthenticationFilter.class);

}

@Override

protectedvoidconfigure(AuthenticationManagerBuilderauth)throwsException{

auth.userDetailsService(userDetailsServiceImpl())

.passwordEncoder(passwordEncoder());

}

搞定.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用Spring Security解决CORS跨域问题,可以按照以下步骤进行配置: 1. 添加CORS配置类:创建一个类,继承自`WebSecurityConfigurerAdapter`,并重写`configure(HttpSecurity http)`方法。 ```java @Configuration public class CorsConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors(); } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedOrigin("*"); // 允许所有来源 configuration.addAllowedMethod("*"); // 允许所有请求方法 configuration.addAllowedHeader("*"); // 允许所有请求头 UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } ``` 2. 启用CORS配置:在Spring Boot应用的入口类上添加`@EnableWebSecurity`注解。 ```java @EnableWebSecurity public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 通过以上配置Spring Security会自动处理跨域请求,并允许所有来源、所有请求方法和所有请求头。你可以根据需要调整配置,例如指定允许的来源、方法和头部信息。 请注意,如果你的应用程序中使用了其他Spring Security配置,你需要将CORS配置类的优先级调整到较低的位置,以确保CORS配置被正确应用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT从业者的职业生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值