package com.huixinpn.dionysus.controller;

import com.huixinpn.dionysus.domain.User;
import com.huixinpn.dionysus.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/v1")
public class LoginController {

  private static Logger logger = LoggerFactory.getLogger(LoginController.class);
  private UserService userService;

  @Autowired
  public LoginController(UserService service) {
    this.userService = service;
  }

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public
  @ResponseBody
  User login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {

    User loginedUser = userService.sign(user.getUsername(), user.getPassword());
    request.getSession();
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(loginedUser, user.getPassword(), user.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
    return loginedUser;
  }

  @RequestMapping(value = "/logout", method = RequestMethod.GET)
  public String logout(HttpSession session) {
    if (session != null)
      session.invalidate();
    return "success";
  }

  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public User register(@RequestBody User user) {
    return userService.register(user);
  }

  @RequestMapping(value = "/consultant", method = RequestMethod.POST)
  public User consultant(@RequestBody User user) {
    return userService.notifyuser(user, "Consultant Validation & Register");
  }
}

@@ -404,64 +404,550 @@
 text
-Holder;%0Aimport org.springframework.web.bind.annotation.*
+;%0Aimport org.springframework.security.core.context.SecurityContextHolder;%0Aimport org.springframework.security.web.context.HttpRequestResponseHolder;%0Aimport org.springframework.security.web.context.HttpSessionSecurityContextRepository;%0Aimport org.springframework.web.bind.annotation.RequestBody;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.ResponseBody;%0Aimport org.springframework.web.bind.annotation.RestController
 ;%0A%0Ai
@@ -1270,16 +1270,143 @@
 rvice;%0A%0A
+  private HttpSessionSecurityContextRepository sessionSecurityContextRepository = new HttpSessionSecurityContextRepository();%0A%0A
   @Autow
@@ -1768,34 +1768,8 @@
 ));%0A
-    request.getSession();%0A
     
@@ -1851,27 +1851,34 @@
 onToken(
-loginedUser
+user.getUsername()
 , user.g
@@ -1887,25 +1887,32 @@
 Password(), 
-u
+loginedU
 ser.getAutho
@@ -1925,16 +1925,42 @@
 ());%0A   
+ SecurityContext context =
  Securit
@@ -1986,16 +1986,29 @@
 ontext()
+;%0A    context
 .setAuth
@@ -2025,16 +2025,325 @@
 (auth);%0A
+    HttpRequestResponseHolder requestResponseHolder = new HttpRequestResponseHolder(request, response);%0A    sessionSecurityContextRepository.loadContext(requestResponseHolder);%0A    sessionSecurityContextRepository.saveContext(context, requestResponseHolder.getRequest(), requestResponseHolder.getResponse());%0A
     retu
@@ -2358,16 +2358,17 @@
 edUser;%0A
+%0A
   %7D%0A%0A  @

use spring HttpSessionSecurityContext to store securitycontext after login successfully