0

***I am trying to implement session management in spring boot application using HttpSession. when i call a web service through postman client,session attributes are accessible.But while accessing from application,it wont be available. This is my login page and i am setting my session attributes here.

@Autowired
public HttpSession httpSession;

 ClsCommon clsCommon=new ClsCommon();
private final Logger log = Logger.getLogger(this.getClass());
public static final String ACTIVE_RECORD_STATUS="A";
public static final String DEACTIVE_RECORD_STATUS="D";
@Override
public String userValidate(ClsLoginResourceDto clsLoginDto) {
    List serverList = new ArrayList();
    String result="";
    try{
        List<ClsLoginResourceBean> list=iLoginResourceRepository.userValidate(clsLoginDto.getUserName(), clsLoginDto.getPassword(), ACTIVE_RECORD_STATUS);


        if(list.size()>0){
            httpSession.setAttribute("COMPANY", list.get(0).getCompanyName());
            httpSession.setAttribute("COMPANYID", list.get(0).getCompanyId());
            httpSession.setAttribute("USERNAME", list.get(0).getUserName());
            httpSession.setAttribute("USERID", list.get(0).getUserId());
            httpSession.setAttribute("USERROLE", list.get(0).getUserRoleName());
            httpSession.setAttribute("USERROLEID", list.get(0).getUserRoleId());

            result=clsCommon.convertToJson(list);
        }
        else{
            httpSession.invalidate();
            result="failed";
        }
    }
    catch(Exception e){
        e.printStackTrace();
        result="exception";
    }
    finally{
    }
    return result;
}

}

This is my get session part..

@Autowired
private HttpSession  httpsession;
@RequestMapping(value = "/server/getData", method = RequestMethod.GET,consumes = "application/json", produces = "application/json")
public @ResponseBody String getData() {

    try{

System.out.println("==server==COMPANYID=="+httpession.getAttribute("COMPANYID"));
    System.out.println("==server==USERID=="+httpession.getAttribute("USERID"));
    }
    catch(Exception e){
        e.printStackTrace();
        log.error("Exception caught  :"+e);

    }
    return "success";
}

} Please let me know if i m doing any mistakes.I am new to java.please help me.***

  • 1
    Don't `@Autowire` the `HttpSession` inject it as a method argument in your controller. Also what is your `userValidate` you only show some partial code, show the real class (and also you shouldn't be injecting the `HttpSession` here). – M. Deinum Mar 31 '17 at 08:04
  • HI, i reffer https://dzone.com/articles/using-http-session-spring this link to use session management. – krishnanunni R Mar 31 '17 at 09:34
  • Hey, Did you get the answer for this Questions ? how did you solve it. – Delli Kilari May 07 '18 at 06:23

0 Answers0