如何将 Angular 2 与 Spring 启动应用程序连接?
How to connect Angular 2 with Spring boot application?
我使用 HttpModule 发送 post 请求以将登录凭据发送到 Spring 框架应用程序,但请求被 spring security.Angular 应用程序过滤掉了发送 post 请求但发送一些“选项”请求。
下面的代码是做什么的?
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
if (!(request.getMethod().equalsIgnoreCase("OPTIONS"))) {
try {
chain.doFilter(req, res);
} catch(Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Pre-flight");
response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "authorization, content-type," +
"access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with");
response.setStatus(HttpServletResponse.SC_OK);
}
}
您的 CORS 配置仅允许来自
的流量
response.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
确保您的 Angular 客户端在该特定主机和端口上 运行。
浏览器最初发出 HTTP OPTIONS (CORS pre-flight) 请求以验证客户端 host/port 是否与服务器的响应匹配(即 Access-Control-Allow-Origin 响应的值header你设置的)
我使用 HttpModule 发送 post 请求以将登录凭据发送到 Spring 框架应用程序,但请求被 spring security.Angular 应用程序过滤掉了发送 post 请求但发送一些“选项”请求。
下面的代码是做什么的?
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
response.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Credentials", "true");
if (!(request.getMethod().equalsIgnoreCase("OPTIONS"))) {
try {
chain.doFilter(req, res);
} catch(Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Pre-flight");
response.setHeader("Access-Control-Allow-Methods", "POST,GET,DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "authorization, content-type," +
"access-control-request-headers,access-control-request-method,accept,origin,authorization,x-requested-with");
response.setStatus(HttpServletResponse.SC_OK);
}
}
您的 CORS 配置仅允许来自
的流量 response.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
确保您的 Angular 客户端在该特定主机和端口上 运行。
浏览器最初发出 HTTP OPTIONS (CORS pre-flight) 请求以验证客户端 host/port 是否与服务器的响应匹配(即 Access-Control-Allow-Origin 响应的值header你设置的)