栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

爆破专栏丨Spring系列教程解决Spring Security环境中的跨域问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爆破专栏丨Spring系列教程解决Spring Security环境中的跨域问题

上一章节中,一一哥 给各位讲解了同源策略和跨域问题,以及跨域问题的解决方案,在本篇文章中,我会带大家进行代码实现,看看在Spring Security环境中如何解决跨域问题。

一. 启用Spring Security 的CORS支持

1. 创建web接口

我先在SpringBoot环境中,创建一个端口号为8080的web项目,注意这个web项目没有引入Spring Security的依赖包。然后在其中创建一个IndexController,定义两个测试接口以便被ajax进行跨域访问。8080项目的代码结构:

@RestController

public class IndexController {

@GetMapping("/hello")

public String hello() {

return "get hello";

}

@PostMapping("/hello")

public String hello2() {

return "post hello";

}

}

请参考如下代码结构进行项目创建。

2. 执行ajax请求

我们接下来再创建另一个端口号为8082的web项目,注意这个web项目也没有引入Spring Security的依赖包。接着在这里定义一个index.html页面,利用ajax跨域访问8080项目中的web接口。

8082项目的代码结构:

Index

请参考如下代码结构进行项目创建。

3. 发起跨域请求

我们访问8082项目中的index.html页面,然后分别执行get与post请求,这时候就可以在浏览器的控制台上看到产生了CORS跨域问题,出现了CORS error状态,在请求头中出现了Referer Policy: strict-origin-when-cross-origin。

4. 解决跨域问题

既然现在产生了跨域问题,那么该怎么解决呢?其实我们可以采用如下两种方式之一来解决跨域问题。

方式1:在接口方法上利用@CrossOrigin注解解决跨域问题

@RestController

public class IndexController {

@CrossOrigin(value = "http://localhost:8082")

@GetMapping("/hello")

public String hello() {

return "get hello";

}

@CrossOrigin(value = "http://localhost:8082")

@PostMapping("/hello")

public String hello2() {

return "post hello";

}

}

方式2:通过实现WebMvcConfigurer接口来解决跨域问题

@Configuration

public class WebMvcConfig implements WebMvcConfigurer {

@Override

public void addCorsMappings(CorsRegistry registry) {

registry.addMapping("

@Configuration

public class GlobalCorsConfiguration {

@Bean

public CorsFilter corsFilter() {

CorsConfiguration corsConfiguration = new CorsConfiguration();

corsConfiguration.setAllowCredentials(true);

corsConfiguration.addAllowedOrigin("*");

corsConfiguration.addAllowedHeader("*");

corsConfiguration.addAllowedMethod("*");

UrlbasedCorsConfigurationSource urlbasedCorsConfigurationSource = new UrlbasedCorsConfigurationSource();

urlbasedCorsConfigurationSource.registerCorsConfiguration("

@Configuration

public class GlobalCorsConfiguration {

@Bean

public CorsFilter corsFilter() {

CorsConfiguration corsConfiguration = new CorsConfiguration();

corsConfiguration.setAllowCredentials(true);

corsConfiguration.addAllowedOrigin("*");

corsConfiguration.addAllowedHeader("*");

corsConfiguration.addAllowedMethod("*");

UrlbasedCorsConfigurationSource urlbasedCorsConfigurationSource = new UrlbasedCorsConfigurationSource();

urlbasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

return new CorsFilter(urlbasedCorsConfigurationSource);

}

}

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

//跨域方式3:

http.requestMatchers()

.antMatchers(HttpMethod.OPTIONS, "/oauth/**")

.and()

.csrf()

.disable()

.formLogin()

.and()

.cors();

}

}

该方式也可以实现Spring Security中的跨域访问。

4. 代码结构

以下是本案例的代码结构,可以参考下图进行项目创建:

至此,我就带各位解决了Spring Security环境中的跨域问题,你学会了吗?

Security环境中的跨域问题,你学会了吗?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356640.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号