Shiro Custom Realm support MD5 algorithm (vi)

Source: Internet
Author: User

1.1 Hashing algorithm

Passwords are usually hashed, often with md5, sha,

For the MD5 password, if you know the value after the hash can be used by the exhaustive algorithm, the MD5 password corresponding to the Plaintext.

It is recommended to add salt (salt) when hashing the md5, which is equivalent to hashing the original password + Salt. (salt is equivalent to adding a random number)

Hash method when normal use:

Hashes the original password + salt in the program, stores the hash value in the database, and also stores the salt in the Database.

If the password is compared, the original password + salt is hashed and compared using the same method.

1.2 MD5 Test

 packagecn.qlq.authentication;Importorg.apache.shiro.crypto.hash.Md5Hash;Importorg.apache.shiro.crypto.hash.SimpleHash;/*** Test MD5 algorithm *@author: qlq* @date: July 28, 2017 pm 9:31:45*/ public classMd5test { public Static voidmain (string[] Args) {//Original PasswordString Source = "111111"; //SaltString Salt = "qwerty"; //Number of hashes        intHashiterations = 2; //Top Hash 1 times: f3694f162729b7d0254c6e40260bf15c//Top Hash 2 times: 36F2DFA24D0A9FA97276ABBE13E596FC//in the construction method://First parameter: Clear text, original password//second parameter: salt, by using random numbers//third parameter: number of hashes, such as hashing two times, equivalent to MD5 (md5 ("))Md5hash Md5hash =NewMd5hash (source, salt, hashiterations); String PASSWORD_MD5=md5hash.tostring ();                System.out.println (password_md5); //the second method of//first parameter: hash algorithmSimplehash Simplehash =NewSimplehash ("md5", source, salt, hashiterations);                            System.out.println (simplehash.tostring ()); }}

---------------------------------------------------------------------------------------custom Realm Support Hashing algorithm----------------- -------------------------------------------------------------------------------------------------

New Realm (CustomRealmMd5)
 packagecn.qlq.realm;Importorg.apache.shiro.authc.AuthenticationException;Importorg.apache.shiro.authc.AuthenticationInfo;Importorg.apache.shiro.authc.AuthenticationToken;Importorg.apache.shiro.authc.SimpleAuthenticationInfo;Importorg.apache.shiro.authz.AuthorizationInfo;Importorg.apache.shiro.realm.AuthorizingRealm;Importorg.apache.shiro.subject.PrincipalCollection;Importorg.apache.shiro.util.ByteSource;/*** Custom Realm implementation MD5 hashing algorithm encryption *@author: qlq* @date: July 28, 2017 pm 9:33:14*/ public classCustomRealmMd5extendsAuthorizingrealm {//Set the Realm's name@Override public voidsetName (String Name) {Super. SetName ("customRealmMd5"); }    //for Certification@Overrideprotectedauthenticationinfo dogetauthenticationinfo (authenticationtoken token)throwsauthenticationexception {//token is entered by the user//the first step is to remove the identity from tokenString Usercode =(String) Token.getprincipal (); //step two: Query from database according to user input Usercode// ....        //If the query does not return null//the user account in the database is Zhangsansan        /** If (!usercode.equals ("zhangsansan")) {//return null;} */        //simulate query from database to password, hash valueString Password = "f3694f162729b7d0254c6e40260bf15c"; //get salt from databaseString Salt = "qwerty"; //the upper hash value and the salt corresponding to the plaintext: 111111//If the query returns authentication information AuthenticationInfoSimpleauthenticationinfo Simpleauthenticationinfo =Newsimpleauthenticationinfo (usercode, password, ByteSource.Util.bytes (salt), this. GetName ()); returnsimpleauthenticationinfo; }    //for authorization@Overrideprotectedauthorizationinfo dogetauthorizationinfo (principalcollection principals) {//TODO auto-generated Method Stub        return NULL; }}

         in theRealmConfigure credential matching in

Test:

//Custom Realm Implementation hash value matching@Test public voidtestCustomRealmMd5 () {//Create a SecurityManager factory, create a SecurityManager factory with an INI configuration fileFactory<securitymanager> Factory =NewInisecuritymanagerfactory ("classpath:shiro-realm-md5.ini"); //Create SecurityManagerSecurityManager SecurityManager =factory.getinstance (); //set SecurityManager in the current running environmentSecurityutils.setsecuritymanager (securitymanager); //create a subject from inside the SecurityutilsSubject 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 ("zhangsan",                    "111111"); Try {                //Perform certification submissionsSubject.login (token); } Catch(authenticationexception E) {//TODO auto-generated Catch blockE.printstacktrace (); }            //Whether certification is passed            BooleanIsAuthenticated =subject.isauthenticated (); System.out.println ("whether the Certification passed:" +isauthenticated); }        

Shiro Custom Realm support MD5 algorithm (vi)

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.