前言:
现在兄弟们对“java 跨域”大体比较关心,同学们都想要知道一些“java 跨域”的相关知识。那么小编在网上汇集了一些对于“java 跨域””的相关文章,希望我们能喜欢,你们一起来了解一下吧!在线测试跨域请求打开浏览器,打开一个网页(比如掘金), 按F12调出开发者工具,google 浏览器开发者工具界面如下:
2在 Console 输入下面的代码:
var xhr = new XMLHttpRequest();xhr.open('GET', ';);xhr.send(null);xhr.onload = function(e) { var xhr = e.target; console.log(xhr.responseText);}
3。回车运行代码,如果出现跨域问题,会提示下面的信息
JAVA 处理跨域基于WebMvcConfigurerAdapter配置加入Cors的跨域
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class CorsConfig extends WebMvcConfigurationSupport { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }复制代码集成 HandlerInterceptorAdapter 拦截器
@Componentpublic class CorsInterceptor extends HandlerInterceptorAdapter { private final Logger logger = LoggerFactory.getLogger(CorsInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { response.setHeader("Access-Control-Allow-Origin",request.getHeader("origin")); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS"); response.setHeader("Access-Control-Max-Age", "86400"); response.setHeader("Access-Control-Allow-Headers", "*"); // 如果是OPTIONS则结束请求 if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) { response.setStatus(HttpStatus.NO_CONTENT.value()); return false; } return true; }}复制代码
@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport{ @Recourse private CorsInterceptor corsInterceptor; /** * 添加拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(corsInterceptor); }}复制代码创建一个filter解决跨域
@Componentpublic class SimpleCORSFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With"); chain.doFilter(req, res); } public void init(FilterConfig filterConfig) {} public void destroy() {}}复制代码springBoot 配置 CorsFilter(推荐)
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;@Configurationpublic class CorsConfig { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); // 允许任何域名 corsConfiguration.addAllowedOrigin("*"); // 允许任何头 corsConfiguration.addAllowedHeader("*"); // 允许任何方法 corsConfiguration.addAllowedMethod("*"); return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); // 注册 source.registerCorsConfiguration("/**", buildConfig()); return new CorsFilter(source); }}
问题配置多个跨域
2‘https网页不能调试http
Mixed Content: The page at '; was loaded over HTTP
3不安全的证书
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java 跨域