2017-09-29 29 views
0

我有一個角度4的客戶服務,調用一個休息方法在一個Java服務器上用Postmapping註釋聲明。通過角度4 http post連接到b a java rest api不允許在服務器端

當我從角度調用它時,它不被服務器接受。但在郵遞員上試用它時,它僅適用於添加頭Content-Type application/json。

添加了相同的頭文件,甚至還有一些頭文件仍然無法使用角度。

這裏對角服務代碼:

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

const requestParam = { 
    username: username, 
    password: password, 
    email: '[email protected]' 
}; 

const options = this.generateOptions(); 

const body = JSON.stringify(requestParam); 
return this.http 
    .post(AuthService.SIGNIN_URL, body, options) 
    .map(this.extractData) 
    .catch(this.handleError); 
} 

的頭我都不得不時刻:

const headers = new Headers({ 
'Access-Control-Allow-Origin' : '*', 
'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, 
DELETE, OPTIONS', 
'Access-Control-Allow-Headers': 'Origin, Content-Type, X- 
Auth-Token', 
'Content-Type': 'application/json' 
}); 

而且在瀏覽器上給定的錯誤:

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 

服務器方正在使用彈簧靴:

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import 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; 
... 
} 

謝謝!

+1

服務器端是否使用彈簧?你能爲我們分享你的服務器端嗎? –

+0

爲服務器端添加了詳細信息。 –

回答

1

你應該在服務器端建立這樣的:

@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"); 
    ... 
} 
+0

它稍作修改,因爲WebMvcConfigurerAdapter在Spring 5.0.0上已棄用,我以我的回答的方式進行了修改。 –

+0

@ b.lopes,謝謝你的補充。其實我不知道你的項目中使用了什麼版本的spring =) –

0
@Configuration 
public class WebMvcConfig implements WebMvcConfigurer { 
@Override 
public void addCorsMappings(CorsRegistry registry) { 
    registry.addMapping("/**"); 
} 
} 

而不是使用WebMvcConfigurerAdapter,但它應該工作羅馬丹尼洛夫帖子

相關問題