(go) fine-grained permission control via Springmvc+annotation implementation method, Button level

Source: Internet
Author: User

Original address: http://blog.csdn.net/ycyk_168/article/details/18456631

With the deepening of enterprise informatization, a variety of information systems become the necessary tools to improve the efficiency of enterprise operation and management, more and more enterprises core secrets such as sales opportunities, customer information, design programs, such as the storage of information systems, record, circulation, these core information once leaked, will inevitably cause great loss to the enterprise. In the era of science and technology, information is the lifeblood of enterprise survival, and the security of information must become the problem that enterprises attach great importance to. Nowadays, with the implementation of various information security measures, information leaks have been transferred from external leaks to internal personnel leaks. The external hacker, the virus wants to obtain the valuable information, must penetrate the multi-channel firewall, avoids the multiple anti-virus tool's pursue, then carries on the filtering to the information to be possible, but the internal personnel knows what information is valuable, if does not carry on the necessary security protection to the information, Within the enterprise, some people who have the heart will be very easy to get the information they need.

A recent survey shows that almost half of all industry professionals admit that when they switch jobs, they take information, including documents, sales agreements, and contract lists, and tell them to the next boss. The survey also found that 80% of employees can easily download "competitive" information and information, and then take it to their next job.

Information security is a long way off. To ensure the security of the information system, there are many aspects such as firewall, encrypted transmission, anti-SQL injection, etc., but many security programs are from how to guard the door, such as identity authentication, digital certificate, whether it is the traditional user name plus password method or based on biometric identification fingerprint, retinal scanning technology, Even all kinds of e-government fields commonly used in the Usbkey are in the door to enter the system, once the identification of the completion of the door, but let go, there are few treatment options. The focus of this article is not how to authenticate, and after the completion of the identity authentication is also entered the system door, how to ensure that users only in their own scope of access to operate, rather than can be any function of the operation of the system is the internal fine-grained rights control solution.

The commonly used rights system design pattern is role-centric, where a role is a collection of people with the same permissions:

1. A role can have multiple operators, an operator can also belong to multiple roles

2. A role can have multiple functions of operation permissions, a function can also be owned by multiple roles.

By querying the user's role when logging in, you can get all the feature sets for a user, such as:

Most Business System page function menu design is a three-level standard, that is, the first level function menu, level Two function menu, level Three function menu, usually one or two level function menu is only used for functional classification, is not functional access address, three menu is the function of the real entrance, The general permission system is to control the rights by controlling the display and hiding of each person's corresponding function menu. To achieve fine-grained permission control, you can add the fourth layer when you design the menu: page elements, which are subordinate to the third-level feature menus, which identify each function button in a feature page, such as adding, modifying, deleting, querying, which can be considered page elements, and when assigning permissions to a role The fourth tier also incorporates unified rights management, which is displayed on the page if there is permission on the page element, which is not displayed if there is no feature permission for the page element.

For non-privileged access to the function or page in addition to the foreground of the hidden, but also in the background access to the authorization of the authentication, or the operator around the page directly through the input URL access function will create a privilege vulnerability, through the springmvc+annotation way can be easily implemented, The code is as follows:

First step: Create a SPRINGMVC interceptor to intercept all feature requests that require permission validation

[HTML] view plain copy

    1. <!--opening annotations--
    2. <mvc:annotation-driven/>
    3. <!--static resource access--
    4. <mvc:resources location= "/static/" mapping= "/static/**"/>
    5. <!--interceptors--
    6. <mvc:interceptors>
    7. <!--multiple interceptors, sequential execution--
    8. <mvc:interceptor>
    9. <!--if not configured or/**, all controllers---
    10. <mvc:mapping path= "/**"/>
    11. <!--do some general processing before the Freemarker interface is displayed--
    12. <bean class= "Xx.xxxx.core.web.FreeMarkerViewInterceptor" ></bean>
    13. </mvc:interceptor>
    14. </mvc:interceptors>

Step two: Create a annotation class that acts on the method level for passing in the feature ID

[Java] view plain copy

    1. @Retention (Retentionpolicy.runtime)
    2. @Target (Elementtype.method)
    3. Public @interface Permission {
    4. /**
    5. * Function ID, the function ID, the function ID in the corresponding database
    6. * @return
    7. * @version V1.0.0
    8. * @date Jan, 4:59:35 PM
    9. */
    10. String value ();
    11. }

Step three: Establish a one-to-one relationship between function IDs and execution methods in a database by static constants

[Java] view plain copy

    1. public class Funcconstants {
    2. /**
    3. * System Management-Role management-add roles
    4. */
    5. Public final static String Xtgl_jsgl_addjs = "4399D98BB0D84114ACB5693081E83BC9";
    6. /**
    7. * System Management-Department management-Department list
    8. */
    9. Public final static String xtgl_bmgl_bmlist = "dbc4bf80f8b6418788b79de204d37932";
    10. }

Fourth step: Verify permissions in the SPRINGMVC interceptor

[Java] view plain copy

  1. /**
  2. * Freemarker view blocker, page display before doing some general processing
  3. * @version V1.0.0
  4. * @date Dec, 4:20:04 PM
  5. */
  6. public class Freemarkerviewinterceptor extends Handlerinterceptoradapter {
  7. public void Aftercompletion (HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Except Ion {
  8. }
  9. public void Posthandle (HttpServletRequest request, httpservletresponse response, Object arg2, Modelandview view) throws E xception {
  10. String ContextPath = Request.getcontextpath ();
  11. if (view! = null) {
  12. Request.setattribute ("base", ContextPath);
  13. }
  14. }
  15. public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) throws Exception {
  16. Handle permission Annotation, implement method level permission control
  17. Handlermethod method = (Handlermethod) handler;
  18. Permission Permission = method.getmethodannotation (Permission.class);
  19. If NULL indicates that the method does not require permission validation
  20. if (permission = = null) {
  21. return true;
  22. }
  23. Verify that you have permissions
  24. if (! Webutil.haspower (Request, Permission.value ())) {
  25. Response.sendredirect (Request.getcontextpath () + "/business/nopermission.html");
  26. return false;
  27. }
  28. return true;
  29. Note that you must return true here, or the request will stop
  30. return true;
  31. }
  32. }

At this point, based on button, method validation of fine-grained permission system to complete!

(go) fine-grained permission control via Springmvc+annotation implementation method, Button level

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.