Technologies : spring boot angular 2 google oauth
spring boot code tries to connect to google auth for login. angular code makes a http.get to fetch user info. I am getting the above 403 error. Earlier, I got "CORS preflight channel did not succeed" in firefox. Moving to chrome gives me 403.
I searched SO and google. Tried few fixes, but didnot solve my problem. Any help appreciated.
sring boot controller
@CrossOrigin(origins = {"http://localhost:4200"})
@RestController
public class UserRestController {
@RequestMapping("/")
public RedirectView index()
{
RedirectView redirectView = new RedirectView();
redirectView.setUrl("http://localhost:4200/");
return redirectView;
// return "index";
}
@CrossOrigin(origins = {"http://localhost:4200"})
@RequestMapping("/user")
public Principal sayHello(Principal principal) {
return principal;
}
}
angular 2 code
import { Component, OnInit } from '@angular/core';
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import {Headers, RequestOptions} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
@Injectable()
export class AppComponent implements OnInit {
title = 'app works!';
user : any;
private headers:Headers = new Headers({'Content-Type': 'application/json'});
private auth64 = btoa("my-trusted-client:secret");
private tokenHeaders = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic '+this.auth64
});
constructor(private http : Http){
}
// method for getting user details
getUser() {
// this.http.get('http://localhost:8080/user').success(function(user) {
// this.user = user;
// console.log('Logged User : ', user);
// }).error(function(error) {
// // $scope.resource = error;
// });
let options = new RequestOptions({ headers: this.tokenHeaders});
// let options = { headers : this.tokenHeaders};
this.http.get('http://localhost:8080/user', options)
.subscribe ((data :Response)=> {
this.user = data;
console.log('Logged User : ', data);
});
}
// method for logout
logout() {
// this.http.post('http://localhost:8080/logout', null);
this.user = null;
}
ngOnInit() {
this.getUser();
}
}