The main research in this paper is that the Struts interceptor implements the interceptor of users that have not been logged in. The specific implementation is as follows.
First create a tool class:
checkPrivilegeInterceptor: This class inherits interceptor, which is an interface, and requires three methods to implement. If you think there are more, you can inherit its implementation class AbstractInterceptor. After inheriting this class, you only need to rewrite one method, that is, control whether to log in, what permissions will be after logging in, etc. The code is as follows;
package com.cjdx.utils;import javax.annotation.Resource;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.cjdx.domain.User;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;//Check whether you have permission, here only check whether you know whether you log in to public class CheckPrivilegeInterceptor extends AbstractInterceptor {@Override public String intercept(ActionInvocation invocation) throws Exception {User user = (User) ActionContext.getContext().getSession().get("user");String nameSpace = invocation.getProxy().getNamespace();String actionName = invocation.getProxy().getActionName();String privilegeUrl = nameSpace + actionName;if (user == null) {// If the user is not logged in if (privilegeUrl.startsWith("/user_login")) {//If the user is ready to log in, return invocation.invoke();}{return "loginUI";//If the user is not logged in and is not logged in, go to the login page}} else {return invocation.invoke();//If the user is already logged in, execute the corresponding method}}}Then configure struts2.xml:
<!-- Declaration of login interceptor--> <interceptors> <!-- Declare the interceptor you have written--> <interceptor name="checkPrivilege" ></interceptor> <!-- Add the declared interceptor to the default interceptor--> <!-- The name of this interceptor is called defaultStack, which directly overwrites the original defaultStack --> <!-- If you don't write this, you can also add it like comments --> <interceptor-stack name="defaultStack"> <interceptor-ref name="checkPrivilege"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> <!-- <interceptor-stack name="myStack"> <interceptor-ref name="checkPrivilege"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> <interceptor-stack name="defaultStack"> <interceptor-ref name="myStack"></interceptor-ref> </interceptor-stack> --> </interceptors>
The above is all the content of this article about the analysis of Struts interceptor intercepting the instance of unlogged user. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!