问题
I'm trying to get data to angular application from spring boot server via angular http call. Also I have added security dependency for spring boot application.
When I'm trying with browser this will come.
Once I enter them with the password in console of Intellij it will show the response.
When I'm trying with angular http call it fails and show error on topic.
My angular component has following code.
constructor(private http: HttpClient) {
http.get('http://localhost:8080/resource').subscribe(data => this.greeting = data);
}
My spring boot application has below code
@RequestMapping("/resource")
public Map<String,Object> home() {
Map<String,Object> model = new HashMap<String,Object>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "Hello World");
return model;
}
Also i have read about Cross-Origin Resource Sharing (CORS) but it is unclear how to apply those to angular.
My error is:
回答1:
What I have done in angular application was correct and had to correct my spring boot application only.
The only change I made is creating configuration file called WebSecurityConfig.
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new
UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
回答2:
In scala + spring boot you can add cors mapping
class WebAppConfig extends WebMvcAutoConfigurationAdapter {
@Value("${cors.origin}")
private val corsOrigin: String = "all"
override def addCorsMappings(registry: CorsRegistry): Unit = {
if(corsOrigin=="all"){
registry.addMapping("/**")
}else{
registry.addMapping("/**").allowedOrigins(corsOrigin.split(","):_*)
}
}
}
回答3:
Every method in controller needs to be allowed the Cross-Origin individually. Simply add @CrossOrigin(origins = "*") at top of the method, hope this will do the trick.
@CrossOrigin(origins = "*")
@RequestMapping("/resource")
public Map<String,Object> home() {
Map<String,Object> model = new HashMap<String,Object>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "Hello World");
return model;
}
回答4:
Check whether the server also accepts http method 'OPTIONS'. because if the domain is different browser by default sends OPTIONS request and the response should be a success 200. if its a spring back-end
public class CorsFilter implements Filter {
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
final HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
if ("OPTIONS".equalsIgnoreCase(((HttpServletRequest) req).getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(req, res);
}
}
来源:https://stackoverflow.com/questions/48919460/no-access-control-allow-origin-in-angular-with-spring-boot-http-call-angular