1

I have a angular 4 cliente service that calls a rest method declared on a java server with a Postmapping annotation.

When I call it from angular it is not accepted by the server. But trying it on postman it works when I add only the header Content-Type application/json.

With the same header added or even some others it still doesn't work from the angular.

Here the code on angular service:

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);
}

The headers I have at the moment:

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'
 });

And the given error on the browser:

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

The server side is using spring-boot:

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

Thanks!

2
  • 1
    Does server side use spring? Could you share for us your server side? Commented Sep 29, 2017 at 8:28
  • added detail for the server side. Commented Sep 29, 2017 at 9:07

2 Answers 2

1

You should create this on server side:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

And above your method you should also add @CrossOrigin like this:

@CrossOrigin 
@PostMapping(LOGIN_URL)
public ResponseEntity getAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest)
        throws AuthenticationException {
    System.out.println("on login server side");
   ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

It worked with a slight modification since WebMvcConfigurerAdapter is deprecated on the spring 5.0.0 I did in the way as my answer.
@b.lopes, thank you for your addition. Actually I didn't know what version of spring is used in your project =)
I added both annotation and WebConfig class, but I still can't catch the POST requests from angular ... even if those sent with postman are catched :o Any clue please ?
0
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**");
}
}

instead of using WebMvcConfigurerAdapter, but it should work too as Roman Danilov post

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.