admin-server中设置跨域问题

提问 未结 3 90
larry
larry VIP3 2020-06-22
悬赏:20积分
版本:renren-cloud 3.0.0 开发环境:
在页面上请求admin-server接口,存在跨域问题, 但是通过网关(gateway)地址 请求 就没事。 现在 只想单独启动,访问 admin-server 服务。 把 网关中的 CorsConfig 类 复制到 admin-server 中。 不起作用。 不管用。 网上多种方法都试过了。 都不行。 下面是:admin-server 中 CorsConfig类 求解答。 谢谢。 [pre] /** * Copyright (c) 2018 人人开源 All rights reserved. * * https://www.renren.io * * 版权所有,侵权必究! */ package com.renren.config; //import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Configuration; //import org.springframework.http.HttpHeaders; //import org.springframework.http.HttpMethod; //import org.springframework.http.HttpStatus; //import org.springframework.http.server.reactive.ServerHttpRequest; //import org.springframework.http.server.reactive.ServerHttpResponse; //import org.springframework.web.cors.CorsConfiguration; //import org.springframework.web.cors.UrlBasedCorsConfigurationSource; //import org.springframework.web.cors.reactive.CorsUtils; //import org.springframework.web.filter.CorsFilter; //import org.springframework.web.server.ServerWebExchange; //import org.springframework.web.server.WebFilter; //import org.springframework.web.server.WebFilterChain; //import org.springframework.web.servlet.config.annotation.CorsRegistry; //import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; //import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; //import reactor.core.publisher.Mono; /** * Cors跨域 */ //@Configuration //public class CorsConfig { // private static final String MAX_AGE = "18000L"; // // @Bean // public WebFilter corsFilter() { // System.out.println("进来了"); // return (ServerWebExchange ctx, WebFilterChain chain) -> { // ServerHttpRequest request = ctx.getRequest(); // if (!CorsUtils.isCorsRequest(request)) { // return chain.filter(ctx); // } // HttpHeaders requestHeaders = request.getHeaders(); // ServerHttpResponse response = ctx.getResponse(); // HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod(); // HttpHeaders headers = response.getHeaders(); // headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin()); // headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders()); // if (requestMethod != null) { // headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name()); // } // headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); // headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*"); // headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE); // if (request.getMethod() == HttpMethod.OPTIONS) { // response.setStatusCode(HttpStatus.OK); // return Mono.empty(); // } // return chain.filter(ctx); // }; // } // //} ///** // * 跨域配置 // */ //@Configuration //public class CorsConfig { // // @Bean // public CorsFilter corsFilter() { // CorsConfiguration config = new CorsConfiguration(); // config.addAllowedOrigin("*"); // config.setAllowCredentials(true); // config.addAllowedMethod("*"); // config.addAllowedHeader("*"); // UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource(); // configSource.registerCorsConfiguration("/**", config); // return new CorsFilter(configSource); // } // //} import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @author zhanghui * @date 2019/7/2 */ @Component public class CorsConfig implements Filter { private final Logger log = LoggerFactory.getLogger(CorsConfig.class); public CorsConfig() { log.info("SimpleCORSFilter init"); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (request.getHeader("Origin") != null) { response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); } response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me"); chain.doFilter(req, res); } @Override public void init(FilterConfig filterConfig) { } @Override public void destroy() { } } [/pre]
回帖
  • 如果不使用renren-gateway,权限这块会有问题,你这个报错,应该不是跨域问题,chrome浏览器一般没权限,也是报跨域问题,这个坑比较大,其实是其他原因 可以使用360浏览器试试,会有正确的报错提示
    0 回复
  • larry
    larry VIP3 (楼主)
    2020-06-23
    不应该吧,我没有在浏览器里调用。 是 直接 页面通过 ajax 调用的。
    1 回复
  • wuhan_renren
    2020-06-24
    @larry 大佬,能跟你学习一下吗?
    0 回复