jersey - Why the session attribute is coming as null -
a html5 ui connected backend (rest jersey business logic hibernate , db). need create , maintain session each user login until user logs out.
i clueless on how approach problem.
i followed approach
initially when user logs in , setting attribute under session shown below
httpsession session = request.getsession(true); session.setattribute("islogged", "islogged"); string value = (string)session.getattribute("islogged"); system.out.println("****************** user logge in value"+value);
later in different page checking if user logged in or not way
public string checkifuserloggedin() throws jsonexception,classnotfoundexception, sqlexception { httpsession session = request.getsession(); string value = (string)session.getattribute("islogged"); if(value==null) { // coming here } }
i agree francesco foresti, please not rely on http session without auth. unsafe, , quite dangerous app.
have been implementing specific session mecanism ? if not, jersey not store session data it. every call make give session id different yours. have make authentication & use auth token in order identify session.
use jax-rs please use auth mecanism defined : https://jersey.java.net/documentation/latest/security.html
@path("authentication") @singleton public static class myresource { // jersey inject proxy of security context @context securitycontext securitycontext; @get public string getuserprincipal() { return securitycontext.getuserprincipal().getname(); } }
or use framework : spring, shiro.... etc. prefer solution, since framework implement lot of stuff you. gain lot of time doing so.
please take official jersey doc: https://jersey.java.net/documentation/latest/index.html
Comments
Post a Comment