【问题标题】:Angular calling Spring Boot index.htmlAngular 调用 Spring Boot index.html
【发布时间】: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


    【解决方案1】:

    现在您正尝试使用 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);
    
     }
    

    【讨论】:

    • @Ryan,这行得通吗?
    猜你喜欢
    • 2021-11-07
    • 2016-11-21
    • 1970-01-01
    • 2018-03-09
    • 2016-04-16
    • 1970-01-01
    • 2021-01-17
    • 2017-01-31
    • 2018-04-08
    相关资源
    最近更新 更多