我是 angular 和 spring-security 的新手。尝试使用基本身份验证从 angular 登录表单页面登录到其余端点时,我遇到了 CORS 问题。我的 Angular 代码在http://localhost:4200 上运行,其余端点在http://localhost:8181 上。我的角度登录表单尝试向我在登录控制器中指定的http://localhost:8181/token发出请求。即使我在服务器端添加了 cors 配置,我还是收到了这个错误:-
无法加载http://localhost:8181/token:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。来源 ' http://localhost:4200因此,不允许访问 '。响应具有 HTTP 状态代码 403。
(角度)login.service.ts:-
@Injectable()
export class LoginService {
constructor(private http: Http) {}
sendCredential(username: string, password: string) {
const url = 'http://localhost:8181/token';
const encodedCredential = username + ':' + password;
const basicHeader = 'Basic ' + btoa(encodedCredential);
const headers = new Headers();
headers.append('Content-Type', 'application/x-wwww-form-urlencoded');
headers.append('Authorization' , basicHeader);
const opts = new RequestOptions({headers: …Run Code Online (Sandbox Code Playgroud)