Simple Integration of apache shiro Based on spring framework

Source: Internet
Author: User

With regard to project security protection, I have always wanted to find a simple configuration method that can achieve the goal. Since I got in touch with shiro, this goal has been achieved. The following is my experience in using shiro, this feature can be easily integrated.

First, let's take a look at what shiro is.

Apache shiro is a powerful and easy-to-use Java security framework that provides developers with an intuitive and comprehensive solution for authentication, authorization, encryption, and session management.

In fact, according to my personal understanding, it is a filter that performs permission verification according to the configuration (or annotation) Rules.

My project is based on maven to manage dependent jar packages. First, introduce the jar required for apache shiro:

                            org.apache.shiro                shiro-web                1.2.1                                        org.apache.shiro                shiro-spring                1.2.1                                        org.apache.shiro                shiro-ehcache                1.2.1            

Shiro-web and shiro-spring must be used. If you want to cache permissions, shiro-ehcache will be introduced. The use of shiro-ehcache will be detailed later.

Let's take a look at how login. action enables user login and writing, get user form information, and query database verification without having to go through the key code:

= =

 

Since it is a filter, let's take a look at the method of this filter:

. Tech. nuo. common. security. MD5 ;. tech. nuo. core. domain. manager ;. tech. nuo. core. service. managerService; shiroFilter // administrator user service @ Resource init (FilterConfig filterConfig) === (! = // The user id is saved, that is, the Information System. out. println (principal. getName () in the top logon token; Manager m = (! = M & 1 =); // as an example, here I just put the user ID into the token. You can change it to other complex information Subject subject1 = (subjects! =

So far, we can say that the login and filter have been completed. Then implement the web. xml and spring files and permission verification.

1. Add the shiro filter configuration in web. xml:

shiroFilterorg.springframework.web.filter.DelegatingFilterProxy        shiroFilter        /*

This filter must be located before all filters.

2. Permission verification code implementation. Let's write a realm class that integrates shiro's

ShiroRealm (principals = "PrincipalCollection method argument cannot be null. "= (String) getAvailablePrincipal (principals); System. out. println ("-------------------" + = info. addRole ("ROLE_USER"/* You can obtain the role and permission information of the user from the database, and add the obtained information to info, the specific code for getting the database is omitted */= (userName! = &&! "" = <Object, AuthorizationInfo> cache = (cache! =

3. Configure applicationContext. xml (only shiro-related information is retained here)

                                                                                                                                /admin.html = authc,perms[shiro_admin:view]                //user.html=authc,perms[shiro_user:view]                                /manage/kindeditor/**=anon                /manage/**=authc,roles["ROLE_USER"]                /**=anon                                            .tech.nuo.action.shiro.realm.ShiroRealm"                                           "                                                

/Admin.html = authc, perms [shiro_admin: view]

/User.html = authc, perms [shiro_user: view]

 

 

Author: bixue Huangsha Source: yellow.

 

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.