The first simple case is to simulate a login by reading the. ini file,
1. Using the getinstance () method of the factory factory to obtain an instance of the SecurityManager, instantiating the factory requires the path of an INI file as a parameter;
2. Set the instance obtained above into the class Securityutils, the Securityutils class has a Get method to get the subject class
3.Subject This class is used to log in and exit, but requires a Authenticationtoken instance as a parameter
The above is the step, the following is a step by step introduction, the end will be at the bottom of the code
The second step I will not say, the method is static, the direct class name call is good;
Now to the third step, it is time to get the Subjiect instance, need to login, but need a token instance
Now token is also available, and then we log in;
Here is a basic example of an. ini file that fills the path of the file with the parameters in the first instance of the chemical plant.
Finally, the code for a simple case
Packagecom;Importorg.apache.shiro.SecurityUtils;ImportOrg.apache.shiro.authc.AuthenticationToken;ImportOrg.apache.shiro.authc.UsernamePasswordToken;Importorg.apache.shiro.config.IniSecurityManagerFactory;ImportOrg.apache.shiro.mgt.SecurityManager;ImportOrg.apache.shiro.subject.Subject;Importorg.apache.shiro.util.Factory;/*** Test the first case of Shiro **/ Public classApp { Public Static voidMain (string[] args) {{/*Example Chemical Factory*/Factory<SecurityManager> factory =NewInisecuritymanagerfactory ("Classpath:shiro.ini"); /*Get Instance*/SecurityManager Instance=factory.getinstance (); /*Setting Parameters*/Securityutils.setsecuritymanager (instance); /*Get Login Instance*/Subject Subject=Securityutils.getsubject (); /*instantiate token, incoming login user and password*/Authenticationtoken Token=NewUsernamepasswordtoken ("Admin1", "admin"); Try { /*to sign in*/Subject.login (token); /*Sign Out*/subject.logout (); }Catch(Exception e) {System.out.println ("Login Error!! "); } } }}
Java Security Framework Shiro (i)