Struts custom Interceptor-logon permission Control

Source: Internet
Author: User

Struts custom Interceptor-logon permission Control

Note: The Custom interceptor allows you to control the logon permissions.

Login. jsp ---> LoginAction -- redirection --> MainAction ---> main. jsp

I. 1. Overall steps:

(1). Define the interceptor class. LoginInterceptor (complete logon interception)
Method 1: implement the com. opensymphony. xwork2.interceptor. Interceptor interface and override the method.
Method 2: Inherit the com. opensymphony. xwork2.interceptor. AbstractInterceptor class and overwrite the intercept method.

(2) register the interceptor in struts. xml

(3) tell you to use the interceptor




Main. action


However, after Step 3 is completed, the request parameters cannot be obtained in the Action.
Configure in struts-default.xml
All have default interceptors. Once other interceptors are referenced, the default interceptor is canceled.


Solution: first reference defastack stack and then reference loginInterceptor.






Main. action



The two interceptors referenced above are defined in one element. Multiple interceptors must also be referenced.
Copy the two lines of code.

Solution: We also define the interceptor stack.




Ii. Code practice:

1. LoginInterceptor. java:

Package cn. wwh. www. web. interceptor; import java. util. arrays; import java. util. list; import com. opensymphony. xwork2.Action; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionInvocation; import com. opensymphony. xwork2.interceptor. abstractInterceptor;/*** class: defines the interceptor class, inherits AbstractInterceptor, *** @ author @ * @ version 1.0 * @ Creation Time: 05:10:23 */public class LoginInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L; private List
  
   
ActionNames; // set the value of public void setActionNames (String actionNames) {System. out. println ("--->" + actionNames); // remove the parameters in the configuration file using the Arrays tool. actionNames = Arrays. asList (actionNames. split (",");} // Interception Method @ Overridepublic String intercept (ActionInvocation invocation) throws Exception {// if the Session contains an object whose key is USER_IN_SESSION, actionContext ctx = invocation should be released. getInvocationContext (); Objec T user = ctx. getSession (). get ("USER_IN_SESSION"); // get from the Session // if the current Action name is login, the String actionName = invocation should be allowed. getProxy (). getActionName (); // get the name of the current Action System. out. println (actionName); System. out. println ("LoginInterceptor:" + user); // the current action is login, or the program is allowed when a correct user logs on. However, the main action is not included in actionNames, // It cannot be passed, so the if judgment condition should be | instead of & if (user! = Null | this. actionNames. contains (actionName) {// log on to System. out. println ("passed, and enter the main interface of the program! "); Return invocation. invoke (); // allow} System. out. println (" You have not logged on yet. Please log on! "); // Otherwise return Action. LOGIN; //" login "}}
  


2. logon interface: login. jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
 Insert title here

3. action class:

Package cn. wwh. www. web. interceptor; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; import com. opensymphony. xwork2.ModelDriven;/*** function: *** @ author yiye banzhou * @ version 1.0 * @ Creation Time: 05:17:12 */public class LoginAction extends ActionSupport implements ModelDriven
 
  
{Private static final long serialVersionUID = 1L; private User user = new User (); public String execute () throws Exception {System. out. println (user); // you can store data in the session and put the User object in the Session // ServletActionContext. getRequest (). getSession (). setAttribute (name, value) ActionContext. getContext (). getSession (). put ("USER_IN_SESSION", user); return SUCCESS;} public User getModel () {return user ;}}
 

4. entity class: User. java

Package cn. wwh. www. web. interceptor; import java. io. serializable;/*** class function: *** @ author a boat * @ version 1.0 * @ Creation Time: 05:03:09 */public class User implements Serializable {private static final long serialVersionUID = 1L; private String username; private String password; public String getUsername () {return username ;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} @ Overridepublic String toString () {return "User ---> \ n username =" + username + "\ n password =" + password ;}}


5. Page after successful user verification: main. jsp

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" %>
 Insert title hereYou have successfully logged on to the main interface of the program!

6. main action class:

Package cn. wwh. www. web. interceptor; import com. opensymphony. the role of the xwork2.ActionSupport;/*** class: *** @ author yiye banzhou * @ version 1.0 * @ Creation Time: 05:18:16 */public class MainAction extends ActionSupport {private static final long serialVersionUID = 1L; public String execute () throws Exception {return SUCCESS ;}}

7. interceptor. xml configuration file (this should be included in the struts. xml file with include)

 
 
  
   
    
    
     
     Login, userLogin, xxAction, ooAction
    
    
    
     
     
     
     
    
   
   
   
   
   
    
     
/Views/interceptor/login. jsp
    
   
   
    
    Main. action
   
   
    
/Views/interceptor/main. jsp
   
  
 


Note: Only one default interceptor can be specified for each package. In addition, once an interceptor is explicitly specified for an action in the package,

The default interceptor does not work.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.