Use annotation and interceptor to set struts2 Permissions

Source: Internet
Author: User

Tag: struts2

Requirement Analysis:

List all permissions, and then grant related permissions to each department. If you have the permission, you can perform the operation. If no permission information is displayed.


In order to realize reusability, the annotation form is selected here.

Idea: Since struts2 is used, implementing a custom interceptor can easily intercept and allow methods in actions. First, create a permission table and list all the functions of the system, 1.

Create a table to store all function items of the system sys_popedom, set the field popodommodule (Department Group of the module), popedomprivilege (specific function of the module's specific Department), sort (SORT), title, popedomname (name)

Create Table Storage permission sys_popedom_privilege create field RID (this is the department ID), popedommodule, popedomprivilege

Create limit Interface

Packagecom. jrrjw. annotation; importjava. lang. annotation. retention; importjava. lang. annotation. retentionpolicy; @ retention (retentionpolicy. runtime) Public @ interfacelimit {stringmodule (); // Module name stringprivilege (); // operation name}


Create interceptor

Packagecom. jrrjw. inteceptor; importjava. lang. reflect. method; importjava. util. collection; importjava. util. list; importjavax. servlet. HTTP. httpservletrequest; importorg. apache. struts2.servletactioncontext; importcom. jrrjw. CRM. annotation. limit; importcom. jrrjw. CRM. container. serviceprovider; importcom. jrrjw. CRM. domain. syspopedomprivilege; importcom. jrrjw. CRM. domain. sysuser; importcom. jrrjw. CRM. service. syspopedomp Rivilegeservice; importcom. jrrjw. CRM. utils. sessionutils; importcom. opensymphony. xwork2.actioninvocation; importcom. opensymphony. xwork2.interceptor. methodfilterinterceptor; Public classlimitinterceptor extends methodfilterinterceptor {protectedstring dointercept (actioninvocation Invocation) throws exception {objectaction = invocation. getaction (); // obtain the requested actionstring methodstring = invocation. getproxy (). Getmethod (); // obtain the request method name if (methodstring = NULL) {return "error "; // Return Error Page} // obtain the encapsulation class of the method in Action (the method in action has no parameter) methodmethod = action. getclass (). getmethod (methodstring, null); httpservletrequestrequest = servletactioncontext. getrequest (); // obtain requestboolean flag = checklimit (request, method); // check the relationship between annotation and request methods and the permissions of the current user. The main code is to implement if (! Flag) {return "error";} else {stringresultname = invocation. invoke (); returnresultname ;}} privateboolean checklimit (httpservletrequest request, method) {sysuseruser = sessionutils. getsessionuser (request); // obtain the currently logged-in user integer roleid = user. getsysrole (). getrid (); // obtain the group of the current user Boolean Anno = method. isannotationpresent (limit. class); // determines whether the method has any annotation if (! Anno) {returnfalse;} limitlimit = method. getannotation (limit. class); // obtain the annotation stringmodule = Limit added to the method name. module (); // Module name stringprivilege = limit. privilege (); // operation name // query the database to check whether there are three fields, role = (syspopedomprivilegeservice) serviceprovider. getservice (syspopedomprivilegeservice. service_bean); List <syspopedomprivilege> privileges = (list <syspopedomprivilege>) popedomprivilegeservice. getallprivilege (); For (INT I = 0; I <privileges. size (); I ++) {// In order to use the second-level cache in the future, all permission data is found here. syspopedomprivileges = privileges. get (I); If (roleid. equals (S. GETID (). getroleid () & module. equals (S. GETID (). getpopedommodule () & privilege. equals (S. GETID (). getpopedomprivilege () {returntrue ;}} returnfalse ;}}


In this case, you need to configure the struts2 configuration file to modify the default interceptor stack.

<Packagename = "Main" namespace = "/" extends = "struts-Default"> <interceptors> <interceptorname = "limitinteceptor" class = "com. jrrjw. CRM. inteceptor. limitinterceptor "> </interceptor> <Interceptor-stackname =" Limit "> <Interceptor-refname =" defaultstack "/> <Interceptor-refname =" limitinteceptor "> <! -- Configure which methods are not blocked --> <paramname = "excludemethods"> login, top, left </param> </Interceptor-ref> </Interceptor-stack> </interceptors> <default-interceptor-refname = "Limit"> </default-interceptor-ref> <global-Results> <resultname = "error">/WEB-INF/JSP/error. JSP </result> </Global-Results> </package>


Add @ limit (module = "group", privilege = "save") before the method name in the action to implement interception.


Use annotation and interceptor to set struts2 Permissions

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.