Shaaban Ebrahim
Shaaban Ebrahim

Reputation: 10422

How can provide username and password for consuming rest controller with oAuth security in Angular 4?

I made rest controller method that returns list of person in spring boot, and provided oAuth service to secure my rest methods and this is service in angular 2 for consuming this rest service.

import { Observable } from 'rxjs';
import {Http,Response} from '@angular/http';
import 'rxjs/add/operator/map';



@Injectable()
export class XService {

constructor(private http:Http) { }

getAll(): Observable<any>{


return this.http.get('http://localhost:8087/rest2/persons').map((response:Response)=>response.json());


}

How can I provide user name and password for making authentication with web service.

@RequestMapping(path = "/rest2/", produces = "application/json")
@CrossOrigin
public class SubscriberController {
@RequestMapping(method = RequestMethod.GET,     path="/persons",produces="application/json")
public List<Person> getPersons(){
    List<Person> p=new ArrayList<>();
    p.add(new Person(1,"dsd","dsdsd"));
    p.add(new Person(2,"dsd","dsdsd"));
    p.add(new Person(4,"dsd","dsdsd"));
    p.add(new Person(14,"dsd","dsdsd"));
    p.add(new Person(3,"dsd","dsdsd"));
    return p;

}


 }

Upvotes: 1

Views: 126

Answers (1)

linhao
linhao

Reputation: 154

if your angular project and spring-boot project in the same project, you can ask 'http://localhost:8087/rest2/persons' directly.

else you should set the proxy for your api.

make a config json(xxx.config.json) file in the root { "/your api": { "target": "http://localhost:8087", "secure": "false" } }

and modify the ng serve
ng serve --proxy xxx.config.json

then when you ask /your api you will get data from "http://localhost:8087"

hope help you

Upvotes: 1

Related Questions