Problems in angular4 / java: lose server sessions

0

I have two http requests, in one the REST service saves the request

import { Injectable } from '@angular/core';
import { HttpHeaders,HttpResponse } from '@angular/common/http'
import { Observable } from 'rxjs';
import 'rxjs/Rx';
import { Router } from '@angular/router';
import { Usuario } from '../modelo/usuario';
import { MyGuard } from './my-guard.service';
import { APIServiceGetLugares } from '../interfaces/api.service.getlugares';
import { Http, Headers, RequestOptions } from '@angular/http';
import { HttpClient } from '../request/HttpClient'
@Injectable()
export class AutorizacionService {
  private error:string=null;
    private loggedUser:Usuario=Usuario.ANONIMO;
  private loggedIn:boolean=false;
  private app;
    constructor (private http: Http, private router:Router, private myguard:MyGuard) {
        this.loggedUser=Usuario.ANONIMO;
    this.loggedIn=false;
    this.error=null;

    }

public login(login, password, parent) {
        const headers = new Headers({"Content-Type":"application/json"});
        var serviceUrl = 'http://127.0.0.1:8080/ProgramaHospitalServicios/autenticacion';
        var body:any = {
            "service":"getLogin",
            "params": {
                "login":login,
                "password":password
            }
        };
    this.http.post( serviceUrl,  body,{headers:headers} )
           .subscribe((response) => {
            debugger; 
            if(response.status==200){
              this.loggedIn = true
              debugger;
              this.loggedUser = this.getUser();
            } else {
              this.loggedIn = false;
              this.loggedUser = Usuario.ANONIMO;
            }

            }, (error)=>{
          alert (error._body);
          this.loggedIn = false;
          this.loggedUser = Usuario.ANONIMO;
          parent.error = "ERROR! " + error._body;
        });
    }

On the server ...

HttpSession session = req.getSession(true);
session.setAttribute(Constants.AUTHENTICATION, auth);

and in another petition I recover it

  public getUser(parent){


    const headers = new Headers({"Content-Type":"application/json"});
      var serviceUrl = 'http://127.0.0.1:8080/ProgramaHospitalServicios/autenticacion';
      var body:any = {
          "service":"getAutentication",
          "params": {}
      };

      return this.http.post(serviceUrl, body,{headers:headers})
      .subscribe((response)=>{
          if(response.status==200){
            debugger;
            var aux = response.json();
              this.loggedUser.id=aux.id;
              this.loggedUser.nombre=aux.nombre;
              this.loggedUser.apellidos=aux.apellidos;
              this.loggedUser.rolId=aux.rolId;
              this.loggedUser.rol=aux.rol;
              this.myguard.setloggedUser(this.loggedUser)
              this.app.loggedUser = this.loggedUser;
         } else if(response.status==204){
             alert("Se perdio la sesion");
debugger;
         }
      }, error =>{
        console.log(error);
      });



    }

-in the server ...

AuthenticationSession auth = (AuthenticationSession) 
session.getAttribute(Constants.AUTHENTICATION);

Apart from the client I have

import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';

@Injectable()
export class HttpClient {

  constructor(private http: Http) {}

  createAuthorizationHeader(headers: Headers) {
    headers.append('Authorization', 'Basic ' +
      btoa('username:password')); 
  }

  get(url) {
    let headers = new Headers();
    this.createAuthorizationHeader(headers);
    return this.http.get(url, {
      headers: headers
    });
  }

  post(url, data) {
    let headers = new Headers();
    this.createAuthorizationHeader(headers);
    return this.http.post(url, data, {
      headers: headers
    });
  }
}

in the course has been lost

Does Angular 4 not support server sessions?

I have noticed that the client header does not reach the server at times, when it is an OPTIONS, with POST If

    
asked by Carlos Guerra Cubillo 19.11.2017 в 18:39
source

1 answer

1

You should use JWT to handle token instead of sessions, angular is stateless, so it does not save any session - use the one that stores the loc-angular using Gusardians in the routes to verify if the token is still active.

    
answered by 09.12.2017 в 01:23