Permissions Framework-Shiro Simple Getting Started instance

Source: Internet
Author: User

The previous post simply introduced the basic permissions control, it can be said that any background management system is required permission

Let's talk about Shiro today.

First introduce the basic jar package

1 <!--Shiro -2         <Dependency>3             <groupId>Org.apache.shiro</groupId>4             <Artifactid>Shiro-core</Artifactid>5         </Dependency>6         <Dependency>7             <groupId>Org.apache.shiro</groupId>8             <Artifactid>Shiro-web</Artifactid>9         </Dependency>Ten         <Dependency> One             <groupId>Org.apache.shiro</groupId> A             <Artifactid>Shiro-spring</Artifactid> -         </Dependency> -         <Dependency> the             <groupId>Org.apache.shiro</groupId> -             <Artifactid>Shiro-ehcache</Artifactid> -         </Dependency> -         <Dependency> +             <groupId>Org.apache.shiro</groupId> -             <Artifactid>Shiro-quartz</Artifactid> +         </Dependency>

Project Engineering Structure:

Create Shiro-demo.ini:

# User Infomation Configer: [Name=pwd]
[Users]
lee=123456
nee=654321

Build a JUnit test for the most basic login logout

1 @Test2      Public voidtestloginandlogout () {4         //Create a SecurityManager factory, create a SecurityManager factory with an INI configuration file5Factory<securitymanager> Factory =NewInisecuritymanagerfactory ("Classpath:shiro/shiro-demo.ini");7         //Create SecurityManager8SecurityManager SecurityManager =factory.getinstance ();Ten         //set SecurityManager to run environment, keep Singleton mode One Securityutils.setsecuritymanager (SecurityManager); -         //create a subject from inside the Securityutils -Subject Subject =Securityutils.getsubject (); -         //Prepare tokens (tokens) before certification is submitted -         //the account number and password will be entered by the user in the future. -Usernamepasswordtoken token =NewUsernamepasswordtoken ("Lee", "123456"); -         Try { +             //Perform certification submissions A Subject.login (token); at}Catch(authenticationexception e) { - e.printstacktrace (); -         } -         //Whether certification is passed -         BooleanIsAuthenticated =subject.isauthenticated (); -SYSTEM.OUT.PRINTLN ("Whether certified by:" +isauthenticated); +         //Exit Operation - subject.logout (); *         //Whether certification is passed $IsAuthenticated =subject.isauthenticated (); -SYSTEM.OUT.PRINTLN ("Whether certified by:" +isauthenticated); the}

After running, you can test whether the user name password is correct or not to pass

The user name is incorrect, the password is incorrect, or the user name does not exist and will be thrown with the exception information

Permissions Framework-Shiro Simple Getting Started instance

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.