***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.***