this is my first time I ask a question here.Although I’ve already cover CORS configuration in the server, I encounter a CORS error when trying to redirecting from server after login.
Here is my controller
package com.phatdo.authorizationserver.controllers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Slf4j
@Controller
@RequestMapping("/api/authorization")
public class AuthorizationController {
@Value("${oauth2-client.client-id}")
private String clientId;
@Value("${oauth2-client.redirect-uri}")
private String redirectUri;
@GetMapping
public String authorize(Authentication authentication) {
log.info("Authentication : {}", authentication.getName());
String authorizationUri = String.format("?client_id=%s&redirect_uri=%s&scope=%s&response_type=%s",
clientId, redirectUri, "openid", "code");
return "redirect:/oauth2/authorize" + authorizationUri;
}
}
Angular post login data
postData(form : LoginForm) : Observable<any> {
const headers = new HttpHeaders({
'Content-Type' : 'application/x-www-form-urlencoded'
});
const body = new URLSearchParams();
body.set('username', form.username);
body.set('password', form.password);
return this.http.post<any>(this.apiUrl, body.toString(), {
headers: headers,
withCredentials: true,
observe : 'response'
})
}
*The error I encountered *
CORS error
I try to authenticate user using authorization code grant type and I want to eventually get the authorization code to exchange the access token.
I think Angular HttpClient block Spring server redirecting but I cannot solve it.
Thank you for reading.
Phát C1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.