Authentication spring Security in angular 5

angular 7 + spring security login example
angular 6 spring security login example
angularjs spring security login example
angular spring boot jwt authentication example
spring security angular
angular 7 + spring boot login example
angular 6 spring boot login example
angular 8 + spring boot login example

i'm using an existing code in angular 4 and i'm trying to change it to angular 5 : in this part i have an auth.service.ts where i have the error :

    import { Injectable } from '@angular/core';
    import {User} from '../model/model.user';
    import 'rxjs/add/operator/map';
    import {HttpClient, HttpHeaders} from '@angular/common/http';
 @Injectable()
 export class AuthService {
   constructor(public http: HttpClient) { }

  public logIn(user: User) {

const headers = new HttpHeaders();
headers.append('Accept', 'application/json')
const base64Credential: string = btoa( user.username + ':' + user.password);
headers.append('Authorization', 'Basic ' + base64Credential);

this.http.get('http://localhost:8081/' + '/account/login', {
  headers: headers}).map(resp => {
    // login successful if there's a jwt token in the response
// this line where i have a problem : 
    user = resp.json().principal; // the returned user object is a principal object
    if (user) {
      // store user details  in local storage to keep user logged in between page refreshes
      localStorage.setItem('currentUser', JSON.stringify(user));
    }
  });
    }

      logOut() {
// remove user from local storage to log user out
return this.http.post('http://localhost:8081' + 'logout', {})
  .map(resp => {
    localStorage.removeItem('currentUser');
  });

  }
   }

if there are any changes that i have to make please tell me the acctual error is :

  error :src/services/auth.service.ts(19,21): error TS2339: Property 'json' does not exist on type 'Object'.

You can add type any to the resp

this.http.get('http://localhost:8081/' + '/account/login', { headers: headers}).map(resp: any => 
    {
        user = resp.json().principal; 
        if (user) {
            localStorage.setItem('currentUser', JSON.stringify(user));
        }
    });

Spring Security Login Page with Angular, The tutorial is about creating a full stack app using angular5 JWT authentication with spring boot security in the server as token provider and  Welcome to angular 5 jwt authentication with spring security.In this tutorial, we will be creating a full stack app using jwt authentication in an angular5 single page application having backened server supported by spring boot with integration of spring security.Having said that we will have a sample angular5 example application with HttpInterceptor integrated with it to intercept all the HTTP request to add jwt authorization token in the header and in the server we will have some REST

You can do like this

this.http.get('http://localhost:8081/' + '/account/login', {
  headers: headers}).subscribe(resp => {
    user = resp['principal'];
    if (user) {
        localStorage.setItem('currentUser', JSON.stringify(user));
    }
});

Hope this answer is solve your issue

Angular 5 JWT Authentication(Full Stack), Spring Security. Different ways to implement username/password authentication in Spring Security. Implementing authentication for Angular client. Send HTTP Status code 401 instead of 403. Observable pattern. Save currently logged in user. Add cookie to each request. Tracking session expiration. In this tutorial, we'll be creating a login page using Spring Security with: Angular 2, 4, 5, and 6. The example application which we're going to discuss here consists of a client application that communicates with the REST service, secured with basic HTTP authentication. 2. Spring Security Configuration. First of all, let's set up the REST API

Remove the resp.json() in the map operator because HttpClient will handle it for us. we don't need to parse into JSON again.

public logIn(user: User) {

const headers = new HttpHeaders();
headers.append('Accept', 'application/json')
const base64Credential: string = btoa( user.username + ':' + user.password);
headers.append('Authorization', 'Basic ' + base64Credential);

this.http.get('http://localhost:8081/' + '/account/login', {headers)
  .map(resp => {
    // this response is already parsed into a JSON by HttpClient. We don't need to do that explicitly
    // do something
    return resp;
  })
  ...
 }

Basic Authentication with Angular 7 and Spring Security, Follow. Apr 5, 2019 · 6 min read All the REST calls made from Angular to Spring Boot will Duration: 20:14 Posted: Mar 27, 2019 It should be accessible to beginners with Spring and Angular, but there also is plenty of detail that will be of use to experts in either. This is actually the first in a series of sections on Spring Security and Angular, with new features exposed in each one successively.

Angular 7 + Spring Boot Basic Authentication Example, Spring Security Angular 6 Basic Authentication Example. Introduction. Technologies used. Build Java back-end using Spring Boot. Build front-end application using Angular. Secure the REST endpoint. Update Angular application for accessing the secure REST endpoint. Cross-origin resource sharing (CORS) How to disable browser basic authentication popup - Angular 5 + Spring Security. Ask Question Asked 1 year, 7 months ago. Here is my spring security configuration:

Spring Security Angular 6 Basic Authentication Example, Check out this tutorial to learn more about implementing JSON Web Tokens (JWT​) with Spring Duration: 11:35 Posted: Jun 14, 2019 LDAP Server For the Spring Security LDAP authentication demo, we are using UnboundID LDAP SDK as embedded server in our application. For UnboundID LDAP SDK, we have resolved unboundid-ldapsdk dependency in our pom.xml file.

Angular 7 + Spring Boot JWT Authentication Example, This is actually the first in a series of sections on Spring Security and Angular, with new Basic login authentication using Spring security to secure REST service that created <artifactId>spring-boot-starter-parent</artifactId> <version>​2.0.5. All the REST calls made from Angular to Spring Boot will be authenticated using Basic Authentication. Spring boot made the easiest way to secure REST services by adding a very simple dependency - spring boot starter security. It is the spring boot starter for implementing security in web applications as well as RESTful services.

Comments
  • try map(resp: any => { once
  • it worked thanks
  • Sboui, I added an answer, Please check
  • how will it be? (if i remove this resp.json)