title: Session HTTP - Spring and Angular 2
body: <p>I have a problem adding an authentication feature to my spring boot application.
  I have implemented the session pattern as below.</p>
  
  <p>AdminSession.java</p>
  
  <pre><code>@Component
  @Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)
  public class AdminSession {
  private final String id = UUID.randomUUID().toString();
  private Admin admin;
  
  public String getId() {
      return id;
  }
  
  public Admin getAdmin() {
      return admin;
  }
  
  public void setAdmin(Admin admin) {
      this.admin = admin;
  }
  }
  </code></pre>
  
  <p>AdminController.java</p>
  
  <pre><code>@RequestMapping("/admin")
  @CrossOrigin
  @RestController
  public class AdminController extends RestAbstractController&lt;Admin, AdminService&gt;{
  @Autowired
  private AdminService adminService;
  @Autowired
  private AdminSession adminSession;
  
  
  @RequestMapping(value="/auth",method=RequestMethod.POST)
  public ResponseEntity&lt;Admin&gt; auth(
  @RequestParam("pseudo") String pseudo, 
  @RequestParam("password") String password,
  ){
  Admin a = this.adminService.auth(pseudo, password);
  if(a instanceof Admin) {
  this.adminSession.setAdmin(a);
  System.out.println("[DEBUG] Login "+this.adminSession.getAdmin());
  this.displaySessionInfo();
  }else System.err.println("[ERROR] a is not Admin instance");
  return new ResponseEntity&lt;Admin&gt;(a,HttpStatus.ACCEPTED);
  }
  
  public Admin add(@RequestBody Admin admin){
  if(!this.adminService.exist(admin.getPseudo())){
  return super.add(admin);
  }else return new Admin();
  
  }
  
  @RequestMapping("/isAuth")
  public Admin isAuth(){
  
  this.displaySessionInfo();
  return this.adminSession.getAdmin();
  
  }
  
  private void displaySessionInfo(){
  System.out.println("[DEBUG] Session info : "+this.adminSession.getId()+" "+this.adminSession.getAdmin()+" "+this.adminSession.toString());
  }
  
  
  }
  </code></pre>
  
  <p>The problem is that when I try to connect to the Angular side, Spring saves the user correctly in the session. But when I try to access the URL '/ admin / isAuth' after logging in, the session is not the same.</p>
  
  <p>console LOG</p>
  
  <pre><code>2017-05-03 19:08:52.258  INFO 756 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 124 ms
  # FIRST LOGIN
  [DEBUG] Client connexion : 127.0.0.1 on /admin/auth
  [DEBUG] Login root 
  [DEBUG] Session info : 31e7a837-7b0e-4bcc-83a2-b5297a76d2e0 root fr.-----.session.AdminSession@6039cb34
  # LOGIN SUCCESSFUL
  # CHECK IS LOGIN
  [DEBUG] Client connexion : 127.0.0.1 on /admin/isAuth
  [DEBUG] Session info : f83ba190-0faa-480b-be1d-4b2745d4a168 null fr.-----.session.AdminSession@6052863f
  </code></pre>
  
  <p>Side Angular 2</p>
  
  <p>Admin.service.ts</p>
  
  <pre><code>auth(pseudo:string,password:string):Observable&lt;Admin&gt;{
  let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded;' });
  let options = new RequestOptions({ headers: headers });
  return this.http.post(this.provider.getURL()+"/admin/auth","pseudo="+pseudo+"&amp;password="+password,options)
  .map(this.extractData);
  }
  </code></pre>
  
  <p>Login.component.ts</p>
  
  <pre><code>submit(){
      this.process=true;
      this.adminService.auth(this.pseudo,this.password).subscribe(t=&gt;{
        this.adminService.isAuth();
        this.process=false;
        this.adminService.isAuthentified = true;
        this.provider.tokenSession = t;
        this.adminService.isAuth().subscribe(t=&gt;console.log("Test login"));
        this.router.navigate(['/']);
      },err=&gt;console.error(err));
    }
  </code></pre>
  
  <p>Why I lost my http session ?</p>
  
  <p>Thank for your help.</p>