SpringBoot跨域和不能生效的問題


網上的方法很多,測試了幾個可行的方式(需要特別注意的是,網上很多代碼搞過來都不能用,測試發現跨域請求能否正常走下去,取決於你的跨域請求會不會被其它攔截器攔截,假設你的系統使用shiro權限,那么大概率會被首先攔截因為沒有經過跨域授權處理,直接告訴客戶端禁止跨域,因此針對網上的方法,稍作調整):
方式一:
import org.springframework.boot.web.servlet.FilterRegistrationBean;
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;
@Configuration
public class GlobalCorsConfig {
@Bean
public FilterRegistrationBean corsFilter() {
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
//1) 允許的域,不要寫*,否則cookie就無法使用了
// config.addAllowedOrigin("http://localhost:8081");
// config.addAllowedOrigin("http://192.168.59.168:8081");
config.addAllowedOrigin("*");
//2) 是否發送Cookie信息
config.setAllowCredentials(true);
//3) 允許的請求方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
config.setMaxAge(3600L);
// 4)允許的頭信息
config.addAllowedHeader("*");
 
//2.添加映射路徑,我們攔截一切請求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
 
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(configSource));
bean.setOrder(0);//利用FilterRegistrationBean,將攔截器注冊靠前,避免被其它攔截器首先執行
return bean;
}
}
方式二:
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)//控制過濾器的級別最高
public class CosFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest reqs = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", reqs.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type,X-Requested-With");
response.setHeader("Access-Control-Max-Age", "3600");
if ("OPTIONS".equalsIgnoreCase(((HttpServletRequest) req).getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
filterChain.doFilter(req, res);
}
}
}
方式三:
包括網上說的這種(需要注意這種方式,會被shiro之類的先攔截,瀏覽器只會出現“同源策略禁止讀取”,要想看到效果,需要shiro放開這個請求):
@Configuration
public class MyConfiguration extends WebMvcConfigurerAdapter {
@Override public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("*");
}
}
方式四:
還有一種方式,簡單粗暴,直接配置在類或者方法上面
@CrossOrigin(origins = {"http://localhost:8081", "http://xxxx:8081"})
 
需要注意:
以上所有方式都需要注意跨域的請求,如果有登錄攔截,是會出現“同源策略禁止讀取”。因為登錄攔截一般優先其它攔截器,會到不了后面我們需要處理的跨域授權攔截器,當然我這里上面的兩個攔截器例子處理好了,在springboot的shiro下正常通過,不過后續還是要么傳cookie自動登錄,要么放開。至少不會出現“同源策略禁止讀取”。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM