了解CORS: 跨域资源共享 CORS 详解 - 阮一峰
前端地址: http://localhost:9528 后端地址: http://localhost:8889
localhost:8888/user/login
出现了跨域情况
在filter中添加白名单,完整filter代码⤵️
package com.futao.springmvcdemo.foundation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
/**
* @author futao
* Created on 2018/9/19-15:47.
*/
@WebFilter(filterName = "AppFilter", urlPatterns = "/*")
public class AppFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
ArrayList<String> allowOrigins = (ArrayList<String>) req.getServletContext().getAttribute("allowOrigins");
String origin = request.getHeader("Origin");
if (allowOrigins.contains(origin)) {
response.setHeader("Access-Control-Allow-Origin", origin);
}
// Access-Control-Max-Age
response.setHeader("Access-Control-Max-Age", "3600");
// Access-Control-Allow-Credentials
response.setHeader("Access-Control-Allow-Credentials", "true");
// Access-Control-Allow-Methods
response.setHeader("Access-Control-Allow-Methods", "PUT,POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
chain.doFilter(req, resp);
}
@Override
public void init(FilterConfig config) throws ServletException {
//白名单
ArrayList<String> allowOrigins = new ArrayList<>();
allowOrigins.add("http://localhost:63343");
allowOrigins.add("http://localhost:9528");
config.getServletContext().setAttribute("allowOrigins", allowOrigins);
}
}
OK了
需要额外添加如下配置
package com.futao.springmvcdemo.foundation;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @author futao
* Created on 2018/11/6.
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers(HttpMethod.OPTIONS, "/**");
}
}
如果同时进行了filter和CorsConfiguration的配置,OPTIONS请求会返回403,并且控制台提示 Itdoesnothave HTTP ok status.
非常恶心。 网上没有找到相应的解释。
疑问:OPTIONS请求到达服务器后是谁做出的响应
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有