我已经创建了一个带有angular前端和spring boot后端的web应用程序。我目前在谷歌、Github和Facebook上使用OAuth2登录。目前我的登录页面是spring boot中的index.html。我不确定如何将其移出spring并使用angular登录组件。因此,当我点击angular应用程序中的登录按钮时,我打算只调用spring boot index.html。我怎么打电话把我的index.html从spring boot中拔出来?任何建议都将不胜感激。
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";
}
}发布于 2021-09-06 09:49:29
现在您正尝试使用Angular作为前端,Spring Boot作为后端。所以我认为如果你像UI一样对后端进行REST调用,这是很好的。
@PostMapping(path = "your/path", consumes = "application/json", produces =
"application/json")
public ResponseEntity<AuthDTO> login(@RequestBody LoginDTO loginRequest,
HttpServletRequest request) {
// Login logic
return new ResponseEntity<AuthDTO>(authResponse, HttpStatus.OK);
}https://stackoverflow.com/questions/69071502
复制相似问题