通过angular 4 http post连接到b服务器端不允许使用java rest api

通过angular 4 http post连接到b服务器端不允许使用java rest api,java,json,angular,rest,http,Java,Json,Angular,Rest,Http,我有一个angular 4客户端服务,它调用java服务器上声明的rest方法,并带有Postmapping注释 当我从angular调用它时,服务器不接受它。但是在postman上尝试它,当我只添加标题内容类型application/json时,它就起作用了 添加了相同的标题,或者甚至添加了其他一些标题,但从角度看仍然不起作用 以下是有关角度服务的代码: public login(username: string, password: string): Observable<{}>

我有一个angular 4客户端服务,它调用java服务器上声明的rest方法,并带有Postmapping注释

当我从angular调用它时,服务器不接受它。但是在postman上尝试它,当我只添加标题内容类型application/json时,它就起作用了

添加了相同的标题,或者甚至添加了其他一些标题,但从角度看仍然不起作用

以下是有关角度服务的代码:

public login(username: string, password: string): Observable<{}> {

const requestParam = {
  username: username,
  password: password,
  email: 'email@at.pt'
};

const options = this.generateOptions();

const body = JSON.stringify(requestParam);
return this.http
    .post(AuthService.SIGNIN_URL, body, options)
    .map(this.extractData)
    .catch(this.handleError);
}
以及浏览器上的给定错误:

2zone.js:2744 OPTIONS 
http://localhost:8080/api/auth/login net::ERR_ABORTED 
invokeTask @ zone.js:1427 globalZoneAwareCallback @ 
zone.js:1445 login?returnUrl=%2F:1 Failed to load 
http://localhost:8080/api/auth/login: Response to 
preflight request doesn't pass access control check: No 
'Access-Control-Allow-Origin' header is present on the 
 requested resource. Origin 'http://localhost:4200' is 
therefore not allowed access. The response had HTTP 
status code 403. auth.service.ts:205 Server error
服务器端正在使用spring引导:

导入org.springframework.web.bind.annotation.PostMapping

导入org.springframework.web.bind.annotation.RequestBody

导入org.springframework.web.bind.annotation.RestController

/**
 * Returns authentication token for given user
 * 
 * @param authenticationRequest
 *            with username and password
 * @return generated JWT
 * @throws AuthenticationException
 */
@PostMapping(LOGIN_URL)
public ResponseEntity getAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
        throws AuthenticationException {
    System.out.println("on login server side");
   ...
}   

 public class  JwtAuthenticationRequest implements Serializable {

private String username;
private String email;
private String password;
...
}

谢谢

您应该在服务器端创建:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}
在您的方法上方,您还应添加
@CrossOrigin
,如下所示:

@CrossOrigin 
@PostMapping(LOGIN_URL)
public ResponseEntity getAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
        throws AuthenticationException {
    System.out.println("on login server side");
   ...
}

不使用WebMVCConfigureAdapter,但它也应该像Roman Danilov post一样工作

服务器端是否使用spring?您能为我们分享一下您的服务器端吗?添加了服务器端的详细信息。由于WebMVCConfigureAdapter在spring 5.0.0上被弃用,因此它进行了轻微的修改。@b.lopes,谢谢您的添加。事实上,我不知道您的项目中使用了哪个版本的spring=)我添加了annotation和WebConfig类,但是我仍然无法捕获来自angular的POST请求。。。即使那些和邮递员一起寄出的人被抓住了:哦,有什么线索吗?
@CrossOrigin 
@PostMapping(LOGIN_URL)
public ResponseEntity getAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
        throws AuthenticationException {
    System.out.println("on login server side");
   ...
}
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**");
}
}