当前位置:网站首页>No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
2022-08-10 02:56:00 【是九九不是酒酒】
目录
问题:前端调用后端登录接口时出现的跨域的问题
1.什么是跨域的问题
当使用异步请求从一个网址访问另一个网址时可能会出现跨域问题。
2.出现跨域的前提:
2.1. 必须为异步请求
2.2. 当端口号或协议或ip不同时则会出现跨域
我们可以看见有一个请求的方式为: OPTIONS 和login真实的请求方式
3.解决方法
在控制层接口上添加@CrossOrigin
origins: 允许哪些域可以跨域访问我这个接口
allowedHeaders:允许哪些请求头信息跨域
methods: 允许哪些请求方式跨域
上面再控制层接口处加上注解的方式解决跨,麻烦的地方就需要对每个控制类都加该注解。 设置一个全局跨域配置类。
4.设置全局跨域配置类。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* TODO
*
* @author lenovo
* @version 1.0
* @since 2022-08-08 15:44:54
*/
@Configuration
public class CorsConfig {
// 当前跨域请求最大有效时长。这里默认1天
private static final long MAX_AGE = 24 * 60 * 60;
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
corsConfiguration.setMaxAge(MAX_AGE);
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
return new CorsFilter(source);
}
}
5.如果上面的还有解决的话,应该是被shiro得拦截器给拦截器了
5.1.原因
第一个请求是OPTIONS,没有携带token
第二个因为前端和后端不是用得同一个session.默认shiro以sessionId为是否登录得标准
5.2.解决方法--在过滤器中添加isAccessAllowed方法
private RedisTemplate redisTemplate; //没有自动注入,原因该类没有交于spring管理LoginFilter必须交于spring容器来管理。
//构造方法
public LoginFilter(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
//当登录成功后执行得方法,如果该方法返回false,则执行onAccessDenied方法,true则不会执行onAccessDenied方法
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
System.out.println(redisTemplate);
HttpServletRequest req = (HttpServletRequest) request;
//1.请求方式是否为OPTIONS
String method = req.getMethod();
//如果请求方式不为空,并且请求方式是OPTIONS,则不执行onAccessDenied方法
if(method!=null && method.equals("OPTIONS")){
return true;
}
//2.判断请求头是否有token值
String token = req.getHeader("token");
if(token!=null && redisTemplate.hasKey(token)){
return true;
}
return false;
}
边栏推荐
- NFG电商系统在元宇宙趋势下做什么?
- leetcode-218.天际线问题
- Chapter 21 Source Code File REST API Reference (3)
- 【语义分割】2022-HRViT CVPR
- 湖仓一体电商项目(四):项目数据种类与采集
- 如何使用腾讯字体,已经在什么场合下可以使用该字体?TTTGB-Medium
- Evaluation and Construction of Enterprise Network Security Capability from the Sliding Ruler Model
- Instance 042: Variable scope
- Robust Real-time LiDAR-inertial Initialization (Real-time Robust LiDAR Inertial Initialization) Paper Learning
- 元宇宙+NFT是“宝”还是“炒”
猜你喜欢
随机推荐
网路编程_调试accept
使用注解实现限流
《天才基本法》:平行时空的第二次选择,小演员的表现意外出圈
MySQL: Introduction to Logging System | Error Log | Query Log | Binary Log: Bin-log Data Recovery Practice | Slow Log Query
Meteor accelerator Trojan analysis and disposal plan
applet wxs
fastjson autoType is not support
所谓软件测试工作能力强,其实就是这 5 点
金融财经翻译的行业前景如何
C - The Battle of Chibi (dp加树状数组前缀和优化)
Little rookie Hebei Unicom induction training essay
Day16 charles的基本使用
基于误差状态的卡尔曼滤波ESKF
uva1481
“双枪”木马病毒的进化史
[Semantic Segmentation] 2022-HRViT CVPR
How to write a high-quality test case?
cuda——nms
Camera partial update
The Evolutionary History of the "Double Gun" Trojan Horse Virus