当前位置: 首页 > news >正文

【Spring security 解决跨域】

security 跨域

  • 概述
  • 方案
    • 方案一
    • 方案二
    • 方案三
    • 方案四

在这里插入图片描述

主页传送门:📀 传送

概述


  Spring Security是一个功能强大且高度可定制的,主要负责为Java程序提供声明式的身份验证和访问控制的安全框架。其前身是Acegi Security,后来被收纳为Spring的一个子项目,并更名为了Spring Security。Spring Security的底层主要是基于Spring AOP和Servlet过滤器来实现安全控制,它提供了全面的安全解决方案,同时授权粒度可以在Web请求级和方法调用级来处理身份确认和授权。

  跨域问题是由于浏览器的同源策略所引起的。当一个网页从一个域名(协议、域名和端口号相同)的页面向另一个域名的页面发送请求时,由于浏览器的同源策略限制,浏览器会阻止这种请求。
  Spring Security是一个安全框架,它可以防止跨站请求伪造(CSRF)攻击和其他安全漏洞。但是,它也会影响跨域请求。

方案


方案一


在Spring Security配置文件中添加CORS过滤器
1.创建一个CORSFilter类,继承WebMvcConfigurerAdapter类,并重写addCorsMappings方法

@Configuration
public class CorsConfig extends WebMvcConfigurerAdapter {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**") // 允许跨域访问的路径.allowedOrigins("*") // 允许跨域访问的源.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE") // 允许请求的方法.maxAge(168000) // 预检间隔时间.allowedHeaders("*") // 允许头部设置.allowCredentials(true); // 是否发送cookie}
}
  1. 在application.properties或application.yml文件中添加相关配置。

application.properties:

# application.properties
spring.mvc.cors.allowed-origins=*
spring.mvc.cors.allowed-methods=POST,GET,PUT,OPTIONS,DELETE
spring.mvc.cors.allow-credentials=true
spring.mvc.cors.max-age=168000

application.yml:

# application.yml
spring:mvc:cors:allowed-origins: *allowed-methods: POST,GET,PUT,OPTIONS,DELETEallow-credentials: truemax-age: 168000

方案二


使用@CrossOrigin注解
在Controller类或方法上添加@CrossOrigin注解

@RestController
@RequestMapping("/api")
public class ApiController {@CrossOrigin(origins = "http://localhost:8080", maxAge = 3600) // 允许跨域访问的源和预检间隔时间@GetMapping("/hello")public String hello() {return "Hello World!";}
}

  @CrossOrigin注解用于指定允许跨域访问的源和预检间隔时间。其中,origins属性表示允许跨域访问的源,maxAge属性表示预检间隔时间。

如果需要对特定的请求头进行跨域配置,则需要使用@CrossOrigin注解的headers属性

@CrossOrigin(origins = "http://localhost:8080", maxAge = 3600, headers = "Authorization") // 允许跨域访问的源、预检间隔时间和请求头
@GetMapping("/login")
public String login() {return "Login Page";
}

  @CrossOrigin注解的headers属性指定了只允许携带Authorization请求头的跨域请求。

方案三


使用RestTemplate
  在Java中,使用RestTemplate解决security跨域问题,可以通过配置CORS策略来实现

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.Arrays;@Configuration
public class RestTemplateConfig implements WebMvcConfigurer {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**") // 允许跨域访问的路径.allowedOrigins("*") // 允许跨域访问的源.allowedMethods(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)) // 允许请求的方法.allowedHeaders("*") // 允许头部设置.allowCredentials(true) // 是否发送cookie.maxAge(3600); // 预检间隔时间}public static void main(String[] args) throws Exception {RestTemplate restTemplate = new RestTemplate();String url = "https://api.example.com/users/1"; // 请求URLRequestCallback requestCallback = restTemplate::getForEntity; // 创建请求回调对象ResponseExtractor<ResponseEntity<String>> responseExtractor = restTemplate::getForEntity; // 创建响应提取器对象ResponseEntity<String> response = restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor); // 发送请求并获取响应实体if (response.getStatusCode().is2xxSuccessful()) { // 判断响应状态码是否为2xx系列String result = response.getBody(); // 获取响应体内容System.out.println(result); // 输出响应结果} else {System.out.println("Request failed with status code: " + response.getStatusCodeValue()); // 输出请求失败的状态码}}
}

  上述代码通过实现WebMvcConfigurer接口并重写addCorsMappings方法,可以配置CORS策略。在addCorsMappings方法中,使用RestTemplate的execute方法发送请求时,会自动应用CORS策略。需要注意的是,在使用RestTemplate时,需要引入相关的依赖包

方案四


Java中,使用WebMvcConfigurer接口可以方便地解决Spring Security中的跨域问题

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebMvcConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**") // 允许跨域访问的路径.allowedOrigins("*") // 允许跨域访问的源.allowedMethods("GET", "POST", "PUT", "DELETE") // 允许请求的方法.allowedHeaders("*") // 允许头部设置.allowCredentials(true) // 是否发送cookie.maxAge(3600); // 预检间隔时间}
}

  上述代码中,通过实现WebMvcConfigurer接口并重写addCorsMappings方法,可以配置CORS策略。在addCorsMappings方法中,使用CorsRegistry对象的addMapping方法指定允许跨域访问的路径、源、请求方法、头部设置、是否发送cookie和预检间隔时间等参数。这样,在使用Spring Security进行安全认证时,就可以自动应用CORS策略,从而解决跨域问题。需要注意的是,在使用WebMvcConfigurer时,需要引入相关的依赖包

在这里插入图片描述

  如果喜欢的话,欢迎 🤞关注 👍点赞 💬评论 🤝收藏  🙌一起讨论你的支持就是我✍️创作的动力!					  💞💞💞
http://www.lryc.cn/news/117930.html

相关文章:

  • 【C语言】经典题目(四)
  • Prometheus-监控 Postgresql
  • Android java.lang.UnsatisfiedLinkError: No implementation found
  • Leecode力扣704数组二分查找
  • Linux 的基本使用
  • vue3实现自定义select下拉框内容之城市区域篇
  • 性能测评:腾讯云轻量应用服务器_CPU内存带宽流量
  • python中的迭代器和生成器
  • Python-OpenCV中的图像处理-图像阀值
  • VB+SQL酒店客房管理设计与实现
  • 【Linux】从0到1实现一个进度条小程序
  • 江南大学轴承数据故障诊断(利用一维CNN进行故障诊断,代码和数据放在压缩包,无需修改任何东西,解压缩后直接运行,有详细注释)
  • 【网络基础实战之路】基于BGP协议连接三个AS区域的实战详解
  • 基于Python爬虫+词云图+情感分析对某东上完美日记的用户评论分析
  • Day 26 C++ list容器(链表)
  • 【深度学习注意力机制系列】—— SKNet注意力机制(附pytorch实现)
  • Markdown语法和表情
  • CSDN编纂目录索引跳转设置
  • cpu的架构
  • FastAPI和Flask:构建RESTful API的比较分析
  • 用康虎云报表打印二维码
  • 网盘直链下载助手
  • 【EI复现】售电市场环境下电力用户选择售电公司行为研究(Matlab代码实现)
  • 并发——何谓悲观锁与乐观锁
  • 【C++】模板
  • 【Echart地图】jQuery+html5基于echarts.js中国地图点击弹出下级城市地图(附完整源码下载)
  • Python AI 绘画
  • mongodb:环境搭建
  • Grafana技术文档--基本安装-docker安装并挂载数据卷-《十分钟搭建》
  • 【Github】Uptime Kuma:自托管监控工具的完美选择