From 7e5ff3a0ec81073b1d6f31b639dd4f6cfa374d41 Mon Sep 17 00:00:00 2001 From: tschiman Date: Sun, 16 Apr 2017 11:58:58 -0600 Subject: [PATCH] BAEL-797 Adding login logic and a get principal endpoint to serve as our login method --- .../main/angular/ui/src/app/app.component.ts | 21 ++++++++++++++++--- .../src/main/angular/ui/src/app/app.module.ts | 5 +++-- .../ui/src/app/default-request-options.ts | 11 ++++++++++ .../gateway/AuthenticationController.java | 14 +++++++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/default-request-options.ts create mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/AuthenticationController.java diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.component.ts b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.component.ts index bbd2bab06b..d959ebdad6 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.component.ts +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.component.ts @@ -1,18 +1,33 @@ -import {Component} from "@angular/core"; +import {Component, OnInit} from "@angular/core"; import {NgForm} from "@angular/forms"; +import {RequestOptions, Http, Response, Headers} from "@angular/http"; +import "rxjs/Rx"; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) -export class AppComponent { +export class AppComponent implements OnInit{ credentials = { username: '', password: '' }; + constructor(private http: Http){} + + ngOnInit(): void { + + } + onLogin(form: NgForm) { - console.log(form); + let headers = new Headers({'Content-Type': 'application/json'}); + headers.append('Authorization','Basic ' + btoa(form.value.username + ':' + form.value.password)); + let options = new RequestOptions({headers: headers}); + this.http.get("/me", options) + .map((response: Response) => response.json()) + .subscribe((data: any) => { + console.log(data); + }); } } diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.module.ts b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.module.ts index b09c6b0a7f..1e702c3505 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.module.ts +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/app.module.ts @@ -1,9 +1,10 @@ import {BrowserModule} from "@angular/platform-browser"; import {NgModule} from "@angular/core"; import {FormsModule} from "@angular/forms"; -import {HttpModule} from "@angular/http"; +import {HttpModule, RequestOptions} from "@angular/http"; import {AppComponent} from "./app.component"; import {NgbModule} from "@ng-bootstrap/ng-bootstrap"; +import {DefaultRequestOptions} from "./default-request-options"; @NgModule({ declarations: [ @@ -15,7 +16,7 @@ import {NgbModule} from "@ng-bootstrap/ng-bootstrap"; HttpModule, NgbModule.forRoot() ], - providers: [], + providers: [{provide: RequestOptions, useClass: DefaultRequestOptions}], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/default-request-options.ts b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/default-request-options.ts new file mode 100644 index 0000000000..49124774ff --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/angular/ui/src/app/default-request-options.ts @@ -0,0 +1,11 @@ +import {Injectable} from "@angular/core"; +import {BaseRequestOptions, Headers} from "@angular/http"; +/** + * Created by tschi on 4/16/2017. + */ +@Injectable() +export class DefaultRequestOptions extends BaseRequestOptions { + headers = new Headers({ + 'X-Requested-With':'XMLHttpRequest', + }); +} diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/AuthenticationController.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/AuthenticationController.java new file mode 100644 index 0000000000..d82d809f57 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/AuthenticationController.java @@ -0,0 +1,14 @@ +package com.baeldung.spring.cloud.bootstrap.gateway; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.security.Principal; + +@RestController +public class AuthenticationController { + @GetMapping("/me") + public Principal getMyUser(Principal principal) { + return principal; + } +}