网站首页 > java教程 正文
在XXXApplication 类中增加ServletComponentScan
@ServletComponentScan
package com.xxx.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import java.util.concurrent.Executor;
@ServletComponentScan
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//@ComponentScan({"com.hc.resume.service"})
public class HelloWordApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWordApplication.class, args);
}
}
创建CORSFilter
package com.xxx.xxx;
import cn.hutool.core.util.StrUtil;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "CorsFilter", urlPatterns = {"/*"},asyncSupported = true)
@Order(1)
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN,request.getHeader(HttpHeaders.ORIGIN));
response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS,"true");
response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, StrUtil.format("x-token,{}",HttpHeaders.CONTENT_TYPE));
response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
response.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, StrUtil.format("{},{},{}", HttpMethod.GET.name(), HttpMethod.POST.name(),HttpMethod.OPTIONS));
if(request.getMethod().equals(HttpMethod.OPTIONS.name())){
response.setStatus(200);
return;
}
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
猜你喜欢
- 2025-03-23 别再问Cookie了,再问就崩溃了!(别再问我disco是什么 歌词)
- 2025-03-23 Vue整合Node.js,直连Mysql数据库进行CURD操作
- 2025-03-23 深度解析 Spring Boot 3 与 GraphQL 的整合:解锁高效后端开发新姿势
- 2025-03-23 熬夜彻底搞懂Cookie Session Token JWT
- 2025-03-23 工作中,如何解决跨域问题?(跨域问题解决方法)
- 2025-03-23 springboot(二十八)stomp在spring5.3以上报跨域问题的处理
- 2025-03-23 JQuery ajax jsonp 跨域理解(jquery怎么解决跨域)
- 2025-03-23 Spring Boot跨域问题终极解决方案:3种方案彻底告别CORS错误
- 2025-03-23 前后端分离session问题(前后端分离后前端怎么请求)
- 2025-03-23 解决跨域问题的8种方法,含网关、Nginx和SpringBoot~
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)