Commit 2a4ff80a authored by suntao's avatar suntao

跨域

parent 3740182f
...@@ -3,6 +3,7 @@ package cn.quantgroup.cashloanflowboss; ...@@ -3,6 +3,7 @@ package cn.quantgroup.cashloanflowboss;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
...@@ -14,6 +15,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; ...@@ -14,6 +15,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication(scanBasePackages = {"cn.quantgroup.cashloanflowboss"}) @SpringBootApplication(scanBasePackages = {"cn.quantgroup.cashloanflowboss"})
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@ServletComponentScan
@EnableFeignClients @EnableFeignClients
@EnableAsync(proxyTargetClass = true) @EnableAsync(proxyTargetClass = true)
public class Bootstrap { public class Bootstrap {
......
package cn.quantgroup.cashloanflowboss.core.configuration; package cn.quantgroup.cashloanflowboss.core.configuration.web;
import cn.quantgroup.cashloanflowboss.core.Application; import cn.quantgroup.cashloanflowboss.core.Application;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -20,12 +20,13 @@ import java.util.List; ...@@ -20,12 +20,13 @@ import java.util.List;
* *
* @author: suntao * @author: suntao
*/ */
@Configuration //@Configuration
public class BossCorsConfiguration extends WebMvcConfigurerAdapter { public class BossCorsConfiguration extends WebMvcConfigurerAdapter {
@Override @Override
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
HttpServletRequest request = Application.getHttpServletRequest(); HttpServletRequest request = Application.getHttpServletRequest();
String origin = request.getHeader("Origin"); String origin = request.getHeader("Origin");
......
package cn.quantgroup.cashloanflowboss.core.configuration.web;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @description: 跨域
* @author:tao
* @create: 2020-01-06 18:50
*/
@WebFilter(urlPatterns = "/*", filterName = "CORSFilter")
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 req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
String origin = req.getHeader("Origin");
if(origin == null) {
origin = req.getHeader("Referer");
}
resp.setHeader("Access-Control-Allow-Origin", origin);//这里不能写*,*代表接受所有域名访问,如写*则下面一行代码无效。谨记
resp.setHeader("Access-Control-Allow-Credentials", "true");//true代表允许携带cookie
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment