Backend-URL=http://localhost:8080
Frontend-URL=http://localhost:8081
在main.js文件中,我为Axios添加了以下默认值:
axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
axios.defaults.headers.common['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept';
axios.defaults.withCredentials = true;
axios.defaults.crossDomain = true;
在后端站点上,我添加了以下条目:
@CrossOrigin(origins = "http://localhost:8081")
@RestController
public class ControllerName {
...
package de.my.server;
import java.util.Arrays;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class DevelopmentWebSecurityConfigurerAdapter extends WebMvcConfigurerAdapter {
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://locahost:8081"));
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
protected void configure(HttpSecurity http) throws Exception {
//http.csrf().disable();
http
.cors()
.and()
.csrf().disable()
.anonymous().disable()
.authorizeRequests()
.antMatchers("/api").permitAll()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll();
}
}
package de.my.server;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
public class CorsFilter implements Filter {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpServletRequest request= (HttpServletRequest) servletRequest;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT,OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "*");
response.setHeader("Access-Control-Allow-Credentials", "false");
response.setIntHeader("Access-Control-Max-Age", 3600);
filterChain.doFilter(servletRequest, servletResponse);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
在您的VueJS根目录中创建一个vue.config.js文件(如果它不存在),并添加
// vue.config.js
module.exports = {
// proxy all webpack dev-server requests starting with /api
// to our Spring Boot backend (localhost:8088) using http-proxy-middleware
// see https://cli.vuejs.org/config/#devserver-proxy
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8088',
ws: true,
changeOrigin: true
},
'/fileUpload': {
target: 'http://localhost:8088',
ws: true,
changeOrigin: true
}
}
},
// Change build paths to make them Maven compatible
// see https://cli.vuejs.org/config/
outputDir: 'target/dist',
assetsDir: 'static'
}
在我的例子中,对“/api”和“/fileupload”的所有请求都被转发到我的Spring Java后端
如果还没有解决,我希望这能对你有所帮助
我的SimpleCorsFilter如下所示: 机智的邮递员它工作正常: 邮递员得到请求 restlet.com/blog/2016/09/27/how-to-fix-cors-problems/ 并调查了StackOverflow上发布的几个类似问题。
我的网页(wp1)有一个iframe。iframe的来源是另一个网页(wp2)。我在wp1上有一些javascript函数,试图操纵wp2的内容。然而,浏览器会让“阻止原点为空的帧”访问跨原点帧我怎么才能绕过这个?
我的web应用程序有一个小问题:一个连接到Spring BootAPI的angular2应用程序。 如何解决它,这样我就可以使用PUT方法而不会得到这个错误?
其实思想可以按照从尾开始比较两个链表,如果相交,则从尾开始必然一致,只要从尾开始比较,直至不一致的地方即为交叉点,如图所示 # 使用a,b两个list来模拟链表,可以看出交叉点是 7这个节点 a = [1,2,3,7,9,1,5] b = [4,5,7,9,1,5] for i in range(1,min(len(a),len(b))): if i==1 and (a[-1] != b[-
问题内容: 假设我有一个数据库,其中包含人员,杂货店和您可以在商店中购买的商品,如下所示: 我还有一个表格可以跟踪哪些商店销售什么: 我还有一张桌子上有购物清单 我的问题是,给定一个人或他们的ID,找出他们可以去的商店的最佳方法是什么,以便他们将所有物品都列入清单。MySQL中是否有针对这些类型的计算的模式? 我的尝试(非常丑陋和凌乱)是这样的: 谢谢你的时间! 使用数据编辑SQL 问题答案: 如
调用JSON时发生此错误。我不知道为什么会发生这个错误。 错误: