简体   繁体   English

使用 xAuthToken 、spring redis 和 security 登录应用程序

[英]Login application using xAuthToken , spring redis and security

I am new in angular and trying to develop a login application using spring boot, redis, spring security and Angular.我是 angular 新手,正在尝试使用 spring boot、redis、spring security 和 Angular 开发登录应用程序。 I am following an Angular 2 tutorial while using Angular 5 and tried to convert all the code into Angular 5. Even though I converted most of the code, I couldn't convert localStorage.setItem("xAuthToken", res.json().token);我在使用 Angular 5 时正在关注 Angular 2 教程,并尝试将所有代码转换为 Angular 5。即使我转换了大部分代码,我也无法转换localStorage.setItem("xAuthToken", res.json().token); line since this code was related to Http of Angular 2. My question is what is the equivalent of it?行,因为此代码与 Angular 2 的 Http 相关。我的问题是它的等价物是什么? Below are what I did till now.以下是我到目前为止所做的。

loging.service.ts日志服务.ts

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class LoginService {

  constructor(private http: HttpClient) { }

  sendCredential(username: string, password: string) {

    //console.log("User -- > "+username,+"Pass -- > "+password);
    let url = "http://localhost:8181/token";
    let encodedCredentials = btoa(username + ":" + password);
    let basicHeader = "Basic " + encodedCredentials;
    let headers = new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': basicHeader
    });

    return this.http.get(url, { headers: headers });

  }

 checkSession() {
    let url = "http://localhost:8181/checkSession";

    let headers = new HttpHeaders({
      'x-auth-token': localStorage.getItem('xAuthToken')
    });

    return this.http.get(url, { headers: headers });
  }
}

login.component.ts登录.component.ts

import { Component, OnInit } from '@angular/core';
import {LoginService} from '../../services/login.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  private credential = {'username':'', 'password' : ''};
  private loggedIn = false;

  constructor(private loginService: LoginService) { }

  onSubmit() {
    this.loginService.sendCredential(this.credential.username, this.credential.password).subscribe(
        res => {
            console.log(res);
        localStorage.setItem("xAuthToken", res.json().token);
            this.loggedIn = true;
            location.reload();
        },
        error => {
            console.log(error);
        }
    );
  }

  ngOnInit() {
    this.loginService.checkSession().subscribe(
        res => {
            this.loggedIn=true;
        },
        error => {
            this.loggedIn=false;
        }
    );
  }

}

I am getting error:我收到错误:

Property 'json' does not exist on type 'Object' with existing code.具有现有代码的“对象”类型不存在属性“json”。

只需添加这个,它就会被解决:

localStorage.setItem("xAuthToken", res.token);

localStorage.setItem("xAuthToken", JSON.stringify(res)); localStorage.setItem("xAuthToken", JSON.stringify(res)); "Appaji " This is pakka answer “Appaji”这是pakka答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM