Provides a way to identify a user across more than one page
request.
The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user.
We can save user data in modal and modal can be set to session while login.
Example:AuthenticationResponse is a modal.
AuthenticationDAO.java
@Autowired
private HttpSession httpSession;
public void setHttpSession(HttpSession httpSession) {
this.httpSession = httpSession;
}
AuthenticationResponse authentications = new AuthenticationResponse();
authentications.setRole(role.getUserRole().getId());
authentications.setUserId(id.getId());
httpSession.setAttribute("isLogin", authentications);
Header is common in all pages and it is loaded first when page loads.
In Header we can check whether user session exist.
Header.html
$.getJSON("rest/authentication/isLogin", function(res){
if(res.status){
// user session exit
}else{
location.href="index.jsp"}
});
AuthenticationDAO.java
@RequestMapping(value="/isLogin",method=RequestMethod.GET)
public @ResponseBody Boolean isLoginUser(){
try {
AuthenticationResponse loginAuth = (AuthenticationResponse) httpSession.getAttribute("isLogin");
if(loginAuth!=null){
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user.
We can save user data in modal and modal can be set to session while login.
Example:AuthenticationResponse is a modal.
AuthenticationDAO.java
@Autowired
private HttpSession httpSession;
public void setHttpSession(HttpSession httpSession) {
this.httpSession = httpSession;
}
AuthenticationResponse authentications = new AuthenticationResponse();
authentications.setRole(role.getUserRole().getId());
authentications.setUserId(id.getId());
httpSession.setAttribute("isLogin", authentications);
Header is common in all pages and it is loaded first when page loads.
In Header we can check whether user session exist.
Header.html
$.getJSON("rest/authentication/isLogin", function(res){
if(res.status){
// user session exit
}else{
location.href="index.jsp"}
});
AuthenticationDAO.java
@RequestMapping(value="/isLogin",method=RequestMethod.GET)
public @ResponseBody Boolean isLoginUser(){
try {
AuthenticationResponse loginAuth = (AuthenticationResponse) httpSession.getAttribute("isLogin");
if(loginAuth!=null){
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
No comments:
Post a Comment