有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java请求的资源上不存在“Accesscontrolalloworigin”头。起源http://localhost:4200因此不允许访问

我正试图通过电子邮件发送请求。JS4到JavaRESTAPI。我收到以下错误:请求的资源上不存在“Access control allow origin”头。因此,不允许访问源http://localhost:4200 我的代码如下所示:

    signupUser() {
    // if (this.signupForm.dirty && this.signupForm.valid) {
    // let headers = new Headers({ 'Content-Type': 'application/json' });
    let data = {
      "email": this.signupForm.value.email,
      "password": this.signupForm.value.password,
      'phone': this.signupForm.value.mobile,

      // 'first_name': 'dd1',
      // 'email': 'xyz@gmail.com',
      // 'password': 'manju',
      // 'phone': '123456',
      'signup_type': 'Custom',
      // social_media: 
      // "email": this.loginForm.value.name,
      // "password": this.loginForm.value.password
    };
    console.log(data)

    let options = {
      type: "GET",
      url: "http://localhost:8085/dashboard/signUp",
      body: JSON.stringify(data)
    };

    this.http.post(options.url, options.body, {
      headers: this.headers
    }).subscribe((data) => {
      console.log(data);
      this.otpScreen = true;
    }, (err) => {
      console.log(err);
    });
    // alert(`Name: ${this.userForm.value.name} Email:         ${this.userForm.value.password}`);
    // }
}

@CrossOrigin(origins="http://localhost:4200") 在java代码中添加这一行必须解决这个问题,但仍然会遇到相同的错误。有人能告诉我如何解决这个问题吗?我的控制器看起来像:

@CrossOrigin
@RequestMapping("/signUp")
public void getOTP(HttpServletRequest request, HttpServletResponse response){
    System.out.println("In Signup");
}

共 (2) 个答案

  1. # 1 楼答案

    只需在chrome浏览器中添加一个扩展名Allow Control Allow Origin:*1.0.3。 然后,地址栏旁边会出现一个CORS按钮,只需启用它并在chrome上运行您的项目,它就会正常工作。 Link for Allow-Control-Allow-Origin extension

  2. # 2 楼答案

    或者向spring应用程序添加一个全局配置,允许localhost:4200

    例如

    package nl.ivonet.config;
    
    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;
    
    /**
     * Adds a CORS filter on http://localhost:4200 allowing it cross origin access.
     * It is the url where ng serve works when developing.
     * <p>
     * you could also add @CrossOrigin(origins = "*") to the rest method but this is global.
     *
     * @author Ivo Woltring
     */
    @Configuration
    public class CorsConfig {
        @Bean
        public FilterRegistrationBean corsFilter() {
            final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            final CorsConfiguration config = new CorsConfiguration();
            config.setAllowCredentials(true);
            config.addAllowedOrigin("http://localhost:4200");
            config.addAllowedHeader("*");
            config.addAllowedMethod("*");
            source.registerCorsConfiguration("/**", config);
            final FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
            bean.setOrder(0);
            return bean;
        }
    }