BAEL-797 Adding login logic and a get principal endpoint to serve as our login method

This commit is contained in:
tschiman 2017-04-16 11:58:58 -06:00
parent b4ddc23ebf
commit 7e5ff3a0ec
4 changed files with 46 additions and 5 deletions

View File

@ -1,18 +1,33 @@
import {Component} from "@angular/core"; import {Component, OnInit} from "@angular/core";
import {NgForm} from "@angular/forms"; import {NgForm} from "@angular/forms";
import {RequestOptions, Http, Response, Headers} from "@angular/http";
import "rxjs/Rx";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
templateUrl: './app.component.html', templateUrl: './app.component.html',
styleUrls: ['./app.component.css'] styleUrls: ['./app.component.css']
}) })
export class AppComponent { export class AppComponent implements OnInit{
credentials = { credentials = {
username: '', username: '',
password: '' password: ''
}; };
constructor(private http: Http){}
ngOnInit(): void {
}
onLogin(form: NgForm) { 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);
});
} }
} }

View File

@ -1,9 +1,10 @@
import {BrowserModule} from "@angular/platform-browser"; import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from "@angular/core"; import {NgModule} from "@angular/core";
import {FormsModule} from "@angular/forms"; import {FormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http"; import {HttpModule, RequestOptions} from "@angular/http";
import {AppComponent} from "./app.component"; import {AppComponent} from "./app.component";
import {NgbModule} from "@ng-bootstrap/ng-bootstrap"; import {NgbModule} from "@ng-bootstrap/ng-bootstrap";
import {DefaultRequestOptions} from "./default-request-options";
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -15,7 +16,7 @@ import {NgbModule} from "@ng-bootstrap/ng-bootstrap";
HttpModule, HttpModule,
NgbModule.forRoot() NgbModule.forRoot()
], ],
providers: [], providers: [{provide: RequestOptions, useClass: DefaultRequestOptions}],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule { }

View File

@ -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',
});
}

View File

@ -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;
}
}