【发布时间】:2021-11-03 08:44:28
【问题描述】:
我创建了一个带有 Angular 前端和 Spring Boot 后端的 Web 应用程序。我目前通过 Google、Github 和 Facebook 使用 OAuth2 登录。目前我的登录页面是spring boot中的index.html。我不确定如何将其移出春天并使用角度登录组件。所以我打算在我的 Angular 应用程序中单击登录按钮时尝试调用 spring boot index.html。如何拨打电话以从 Spring Boot 中提取我的 index.html?任何建议将不胜感激。
login.component.html
<div>
<button onClick="getLoginPage()" class="btn btn-primary">Login</button>
</div>
<div>
<button onClick="logout()" class="btn btn-primary">Logout</button>
</div>
login.component.ts
import { Component, OnInit } from '@angular/core';
import {HttpErrorResponse} from "@angular/common/http";
import {LoginService} from "../../service/login.service";
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
loginPage = '';
constructor(private loginService: LoginService) {
}
ngOnInit() {
this.getLoginPage();
}
public getLoginPage(): void {
this.loginService.getLoginPage().subscribe(
(response: any) => {
this.loginPage = response;
},
(error: HttpErrorResponse) => {
alert(error.message);
}
)
}
}
login.service.ts
import { Injectable } from '@angular/core';
import {environment} from "../../environments/environment";
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
@Injectable({
providedIn: 'root'
})
export class LoginService {
private apiServerUrl = environment.apiBaseUrl;
constructor(private http: HttpClient) { }
public getLoginPage(): Observable<any> {
return this.http.get<any>( this.apiServerUrl);
}
}
Controller.java
@RestController
public class Controller {
@GetMapping("/")
public String index() {
return "/resources/static/index.html";
}
}
【问题讨论】:
标签: angular spring spring-boot oauth-2.0