Learn shiro permission framework (Day 2), learn shiro
Shiro certification Entry Program shiro-first.ini
Use this configuration file to create a securityManager factory.
You need to modify the eclipse ini Editor:
Entry Program code
Java code
- // User login and exit
- @ Test
- Public void testLoginAndLogout (){
- // Create a securityManager factory and create a securityManager factory through the ini configuration file
- Factory <SecurityManager> factory = new IniSecurityManagerFactory (
- "Classpath: shiro-first.ini ");
- // Create a SecurityManager
- SecurityManager securityManager = factory. getInstance ();
- // Set securityManager to the current running environment
- SecurityUtils. setSecurityManager (securityManager );
- // Create a subject from SecurityUtils
- Subject subject = SecurityUtils. getSubject ();
- // Prepare the token (token) before the authentication is submitted)
- UsernamePasswordToken token = new UsernamePasswordToken ("zhangsan", "111111 ");
- Try {
- // Perform authentication submission
- Subject. login (token );
- } Catch (AuthenticationException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- // Whether the authentication is successful
- Boolean isAuthenticated = subject. isAuthenticated ();
- System. out. println ("authenticated:" + isAuthenticated );
- // Exit the operation
- Subject. logout ();
- // Whether the authentication is successful
- IsAuthenticated = subject. isAuthenticated ();
- System. out. println ("authenticated:" + isAuthenticated );
- }
// Log on to and exit @ Testpublic void testLoginAndLogout () {// create a securityManager Factory and create a securityManager factory using the ini configuration file <SecurityManager> Factory = new IniSecurityManagerFactory ("classpath: shiro-first.ini "); // create SecurityManagerSecurityManager securityManager = factory. getInstance (); // set securityManager to SecurityUtils in the current running environment. setSecurityManager (securityManager); // create a subjectSubject subject = SecurityUtils from SecurityUtils. getSubject (); // prepare the token (token) UsernamePasswordToken token = new UsernamePasswordToken ("zhangsan", "111111") before the authentication is submitted; try {// execute the authentication to submit the subject. login (token);} catch (AuthenticationException e) {// TODO Auto-generated catch blocke. printStackTrace ();} // whether to pass the boolean isAuthenticated = subject. isAuthenticated (); System. out. println ("authenticated:" + isAuthenticated); // exit subject. logout (); // whether to pass isAuthenticated = subject. isAuthenticated (); System. out. println ("authenticated:" + isAuthenticated );}
Execution Process
Java code
- 1. Create a securityManager using the ini configuration file
- 2. Call the subject. login method to submit the token for authentication.
- 3. securityManager authenticates, and securityManager authenticates by ModularRealmAuthenticator.
- 4. ModularRealmAuthenticator calls IniRealm (to pass a token to realm) to query user information in the ini configuration file.
- 5. IniRealm queries user information from the shiro-first.ini based on the input token (UsernamePasswordToken), according to the account query user information (account and password)
- If user information is queried, ModularRealmAuthenticator is returned with the user information (account and password)
- If the query fails, return null to ModularRealmAuthenticator.
- 6. ModularRealmAuthenticator receives IniRealm and returns Authentication information.
- If the returned authentication information is null, ModularRealmAuthenticator throws an exception (org. apache. shiro. authc. UnknownAccountException)
- If the returned authentication information is not null (inirealm finds the user), compare the password returned by IniRealm (which exists in the INIFILE) with the password in the token. If it is inconsistent, an exception is thrown (org. apache. shiro. authc. incorrectCredentialsException)
1. Create securityManager2 in the ini configuration file, call the subject. login method to submit the authentication, and submit token3 and securityManager for authentication. securityManager is finally authenticated by ModularRealmAuthenticator. 4. ModularRealmAuthenticator calls IniRealm (pass in token to realm) to query user information in the ini configuration file. 5. IniRealm queries user information from the shiro-first.ini according to the input token (UsernamePasswordToken, query user information (account and password) based on the account. If user information is found, return the user information (account and password) to ModularRealmAuthenticator. If no user information is found, modularRealmAuthenticator returns null6 and ModularRealmAuthenticator receives IniRealm and Authentication information. If the returned Authentication information is null, ModularRealmAuthenticator throws an exception (org. apache. shiro. authc. unknownAccountException) if the returned authentication information is not null (indicating that inirealm has found the user), compare the password returned by IniRealm (which exists in the INIFILE) with the password in the token, if they are inconsistent, an exception is thrown (org. apache. shiro. authc. incorrectCredentialsException)
Summary
Java code
- ModularRealmAuthenticator is used for authentication. You need to call realm to query user information (user information exists in the Database)
- ModularRealmAuthenticator compares passwords (authentication process ).
- Realm: the database needs to be queried based on the identity information in the token (the ini configuration file is used in the Getting Started Program). If the user returns the authentication information, if the query fails, null is returned.
ModularRealmAuthenticator for authentication. You need to call realm to query user information (user information exists in the Database) ModularRealmAuthenticator for password comparison (authentication process ). Realm: the database needs to be queried based on the identity information in the token (the ini configuration file is used in the Getting Started Program). If the user returns the authentication information, if the query fails, null is returned.
Java enterprise-level general permission security framework source code SpringMVC mybatis or hibernate + ehcache shiro druid bootstrap HTML5
[Download java framework source code]