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

挣钱做任务的网站时事热点新闻

挣钱做任务的网站,时事热点新闻,网站怎么做微信送红包活动,仿站多少钱一套security 跨域 概述方案方案一方案二方案三方案四 主页传送门:📀 传送 概述 Spring Security是一个功能强大且高度可定制的,主要负责为Java程序提供声明式的身份验证和访问控制的安全框架。其前身是Acegi Security,后来被收纳为Spring的一个…

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.hrbkazy.com/news/35196.html

相关文章:

  • 大连网站建设领超最好周口搜索引擎优化
  • 手机网站如何建立传智播客培训机构官网
  • 发布网站建设平面设计安徽疫情最新情况
  • 网站建设费用价格香港疫情最新情况
  • 北京pk10网站建设优化疫情政策
  • dw建网站怎么做商业网站
  • 呼伦贝尔做网站公司短视频推广平台
  • 做电影网站为什么查封不了怎么推广产品最有效
  • 闵行建管委网站品牌型网站设计推荐
  • 知乎网站怎么做推广百度广告费用
  • wordpress登录后台空白性价比高seo排名优化的
  • wordpress优秀站点seo主要是指优化
  • 78创业商机网seo外包费用
  • 素材解析网站搭建最近的新闻有哪些
  • 怎样给网站做流量专业的网站建设公司
  • 无锡本地模板网站建设产品重庆百度seo
  • 织梦dede做网站的优点山东一级造价师
  • 乐清做网站哪家好谷歌搜索引擎免费入口2022
  • 网站推广员需要做什么百度认证平台
  • 莱芜最新莱芜话题东莞seo优化团队
  • icp网站备案信息表2022十大网络营销案例
  • 珠海企业网站制作费用sem优化公司
  • 合肥序曲网站建设公司怎么样深圳市昊客网络科技有限公司
  • 手机在线做ppt的网站有哪些问题搜索引擎论文3000字
  • 昆山住房与城乡建设局网站东莞网络营销渠道
  • 网站新闻列表怎么做自媒体论坛交流推荐
  • 做设计必知网站网络销售每天做什么
  • 哈尔滨做网站网站设计用什么软件
  • 网站建设 启象科技企业培训
  • 网站联系我们的地图怎么做的seo推广哪家公司好