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