java - Struts2 interceptor session get null value when authenticate login page -
i beginner in struts2 , i'm trying scenario on own web project :
- when user access login page, server authenticate whether he/she has login "admin" / "user" accessing session using interceptor, if user has no privilege data (it's null) inside session, passed login page.
- if user has login "admin", user redirected "admin" page.
- if user has login "user", user redirected "user" page.
i trying these codes, if don't use interceptor, can access session, if use interceptor is, session still null , giving error 500 instead npe. don't know wrong it.
thanks of me.
struts.xml
<struts> <constant name="struts.action.extension" value=","/> <constant name="struts.custom.i18n.resources" value="global" /> <constant name="struts.devmode" value="true" /> <!-- configuration default package. --> <include file="strutsconf/struts-user.xml"/> </struts>
struts-pageauth.xml
<struts> <package name="struts-pageauth" namespace="/" extends="struts-default"> <interceptors> <interceptor name="loginpageauth" class="control.intercept.userauthenticationlogin"/> <interceptor-stack name="loginauth"> <interceptor-ref name="createsession"/> <interceptor-ref name="loginpageauth"/> <interceptor-ref name="defaultstack"/> </interceptor-stack> </interceptors> </package> </struts>
strusts-user.xml
<struts> <constant name="struts.custom.i18n.resources" value="prop-user" /> <include file="strutsconf/struts-pageauth.xml"/> <package name="struts-user" namespace="/" extends="struts-default, struts-pageauth"> <action name="login" method="login" class="control.action.login"> <interceptor-ref name="loginauth"/> <result name="admin">/main/admin/admin.jsp</result> <result name="user">/main/user/user.jsp</result> <result name="error">/login.jsp</result> </action> <action name="logout" method="logout" class="control.action.login"> <result name="success">/login.jsp</result> <result name="error">/login.jsp</result> </action> </package> </struts>
userauthenticationlogin.java
/* * change template, choose tools | templates * , open template in editor. */ package control.intercept; import com.opensymphony.xwork2.actioninvocation; import com.opensymphony.xwork2.actionsupport; import com.opensymphony.xwork2.interceptor.interceptor; import java.util.map; import org.apache.struts2.interceptor.sessionaware; public class userauthenticationlogin extends actionsupport implements sessionaware, interceptor { public void setsession(map<string, object> map) { this.sessionmap = map; } public void destroy() { system.out.println("userauthentication interceptor destroy() called"); } public void init() { system.out.println("userauthentication interceptor init() called"); } public string intercept(actioninvocation ai) throws exception { system.out.println("=========================debug========================"); system.out.println("userauthentication interceptor intercept() called"); system.out.println(gettext("auth.privilage")); // can access properties system.out.println(this.sessionmap); // gets null ?? // system.out.println(this.sessionmap.get(gettext("auth.privilage"))); if(this.sessionmap.get(gettext("auth.privilage"))==null) { return ai.invoke(); } else if(this.sessionmap.get(gettext("auth.privilage")).equals("admin")) { return "admin"; } else if(this.sessionmap.get(gettext("auth.privilage")).equals("user")) { return "user"; } else { return "login"; } } private string id; private string password; private string admin; private map<string, object> sessionmap; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string getadmin() { return admin; } public void setadmin(string admin) { this.admin = admin; } public map<string, object> getsessionmap() { return sessionmap; } public void setsessionmap(map<string, object> sessionmap) { this.sessionmap = sessionmap; } }
login.java
/* * change template, choose tools | templates * , open template in editor. */ package control.action; import com.opensymphony.xwork2.actionsupport; import java.util.map; import org.apache.struts2.interceptor.sessionaware; public class login extends actionsupport implements sessionaware { public void setsession(map<string, object> map) { this.sessionmap = map; } public string login() throws exception { if(id.equals("admin") && password.equals("admin")) { this.sessionmap.put("id", "admin"); this.sessionmap.put("priv", "admin"); return "admin"; } if(id.equals("user") && password.equals("user")) { this.sessionmap.put("id", "user"); this.sessionmap.put("priv", "user"); return "user"; } else { seterr_msg(super.gettext("error.login")); return super.error; } } public string logout() throws exception { this.sessionmap.remove("id"); this.sessionmap.remove("priv"); return super.success; } private string id; private string password; private string err_msg; private string admin; private map<string, object> sessionmap; public map<string, object> getsessionmap() { return sessionmap; } public void setsessionmap(map<string, object> sessionmap) { this.sessionmap = sessionmap; } public string getadmin() { return admin; } public void setadmin(string admin) { this.admin = admin; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } public string geterr_msg() { return err_msg; } public void seterr_msg(string err_msg) { this.err_msg = err_msg; } }
the sessionmap injected servletconfiginterceptor in actions implementing sessionaware interface, not interceptors.
the right way session map in interceptor is:
map<string, object> session = actioncontext.getcontext().getsession();
note: careful in messing actions , interceptors together: it's strange see interceptor implementing actionsupport... it's not problem code because using declarative xml configuration, convention plugin scan packages (luckily have package unmatching name) classes extending actionsupport , detect action, making threadlocal, not interceptor has meant be. have remember careful upgrading code in future avoid unexpected result.
Comments
Post a Comment