Struts.xml configuration:<!--Custom user Login Interceptor--><!--for other struts profiles that require login interception, if you inherit "default", you do not need to configure it, you can implement the interceptor directly through inheritance, if you do not inherit "Default", you need to configure the login blocker, configure the same way as here, if an action does not need to intercept, then add the default interceptor under the action (<intercentor-ref name= "Defaultstack"/> --><!--Description: Logininterceptor: Can be arbitrarily taken, and <intercentor-ref name= "Logininterceptor"/> In the same line mydefault: free to take, and rewrite the default interceptor is the same as the name can be defaultstack: The default interceptor, you must write--><intercentors><intercentor class= "Interceptor Java file path" Name= " Logininterceptor "/><intercentor-stack name=" Mydefault "><intercentor-ref name=" DefaultStack "/>< Intercentor-ref name= "Logininterceptor"/></intercentor-stack></intercentors><!--overriding the default interceptor-- <default-interceptor-ref name= "Mydefault"/><!--define global variables, jump to login page opslogin.jsp for login page--><global-results ><resutl name= "Login" type= "redirect" >/opsLogin.jsp</result></global-results>
//Interceptor Java code import java.util.map;import Com.opensymphony.xwork2.action;import Com.opensymphony.xwork2.actioncontext;import Com.opensymphony.xwork2.actioninvocation;import com.opensymphony.xwork2.interceptor.abstractinterceptor;/** * User Login Blocker */public class Logininterceptor extends Abstractinterceptor{public string Intercept (Actioninvocation invaction) throws exception{// Gets the user in session Idactioncontext CTX = Invocation.getincovationcontext (); map<string,object> session = Ctx.getsession (); Object object = Session.get ("id");//If it is not empty, struts will continue to execute if (Object!). = null) {return Invocation.invoke ();} else{//if empty, jump directly to the login page return action.login;}}}
User Login Action Code public String login () {///user information based on username and password user = Opsusermanager.findbynameandpwd (name,password); If the queried user information is not empty, the user ID is stored in the session and jumps to the main page if (user! = null) {HttpServletRequest request = Servletactioncontext.getrequest (); map<string,object> map = Actioncontext.getcontext (). getsession (); Map.put ("id", User.getid ()); return SUCCESS;} else{//if the query is not reached, then jump to the login page return ERROR;}
SSH2 Landing Interception configuration (if logged in, then jump to the user needs to visit the page, if not logged in, go directly to the login page)