diff --git a/src/main/java/com/kwan/springbootkwan/controller/VueChatController.java b/src/main/java/com/kwan/springbootkwan/controller/VueChatController.java index 731795e20b7331f7cbc6a1d21a0c211f2d82f6a4..f75b4d28fef5cbb8e180a91c20da9be27f5cd7e7 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/VueChatController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/VueChatController.java @@ -6,7 +6,6 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.kwan.springbootkwan.entity.Result; import com.kwan.springbootkwan.entity.VueChat; import com.kwan.springbootkwan.service.VueChatService; -import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -29,7 +28,6 @@ import java.util.List; */ @RestController @RequestMapping("vueChat") -@CrossOrigin(origins = "http://localhost:8080") public class VueChatController { /** * 服务对象 diff --git a/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java b/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..1f95cb80340523fd0be240cdf0c7076fd6327a98 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java @@ -0,0 +1,31 @@ +package com.kwan.springbootkwan.filter; + +import org.springframework.stereotype.Component; + +import javax.servlet.*; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + + +/** + * 跨域配置 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/7/9 22:54 + */ +@Component +public class CorsFilter implements Filter { + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + HttpServletResponse httpResponse = (HttpServletResponse) response; + httpResponse.setHeader("Access-Control-Allow-Origin", "*"); + httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); + httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + httpResponse.setHeader("Access-Control-Allow-Credentials", "true"); + + chain.doFilter(request, response); + } +}