Learn shiro permission framework (Day 2), learn shiro

Source: Internet
Author: User

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
  1. // User login and exit
  2. @ Test
  3. Public void testLoginAndLogout (){
  4. // Create a securityManager factory and create a securityManager factory through the ini configuration file
  5. Factory <SecurityManager> factory = new IniSecurityManagerFactory (
  6. "Classpath: shiro-first.ini ");
  7. // Create a SecurityManager
  8. SecurityManager securityManager = factory. getInstance ();
  9. // Set securityManager to the current running environment
  10. SecurityUtils. setSecurityManager (securityManager );
  11. // Create a subject from SecurityUtils
  12. Subject subject = SecurityUtils. getSubject ();
  13. // Prepare the token (token) before the authentication is submitted)
  14. UsernamePasswordToken token = new UsernamePasswordToken ("zhangsan", "111111 ");
  15. Try {
  16. // Perform authentication submission
  17. Subject. login (token );
  18. } Catch (AuthenticationException e ){
  19. // TODO Auto-generated catch block
  20. E. printStackTrace ();
  21. }
  22. // Whether the authentication is successful
  23. Boolean isAuthenticated = subject. isAuthenticated ();
  24. System. out. println ("authenticated:" + isAuthenticated );
  25. // Exit the operation
  26. Subject. logout ();
  27. // Whether the authentication is successful
  28. IsAuthenticated = subject. isAuthenticated ();
  29. System. out. println ("authenticated:" + isAuthenticated );
  30. }
// 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. 1. Create a securityManager using the ini configuration file
  2. 2. Call the subject. login method to submit the token for authentication.
  3. 3. securityManager authenticates, and securityManager authenticates by ModularRealmAuthenticator.
  4. 4. ModularRealmAuthenticator calls IniRealm (to pass a token to realm) to query user information in the ini configuration file.
  5. 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)
  6. If user information is queried, ModularRealmAuthenticator is returned with the user information (account and password)
  7. If the query fails, return null to ModularRealmAuthenticator.
  8. 6. ModularRealmAuthenticator receives IniRealm and returns Authentication information.
  9. If the returned authentication information is null, ModularRealmAuthenticator throws an exception (org. apache. shiro. authc. UnknownAccountException)
  10. 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
  1. ModularRealmAuthenticator is used for authentication. You need to call realm to query user information (user information exists in the Database)
  2. ModularRealmAuthenticator compares passwords (authentication process ).
  3. 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]

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.