Customize a realm class to implement Realm interface
Packagecom;ImportOrg.apache.shiro.authc.*;ImportOrg.apache.shiro.realm.Realm; Public classMyrealmImplementsRealm {/*** Get the name of the current realm *@return */@Override PublicString GetName () {return"Myrealm"; } /*** Determine if the current authentication method is user name and password *@paramAuthenticationtoken *@return */@Override Public Booleansupports (Authenticationtoken authenticationtoken) {returnAuthenticationtokeninstanceofUsernamepasswordtoken; } /*** Return to the authentication information, which can be used to determine the link database query * is only a simulated login, interested people can access the database for judgment *@paramAuthenticationtoken *@return * @throwsauthenticationexception*/@Override PublicAuthenticationInfo getauthenticationinfo (Authenticationtoken authenticationtoken)throwsauthenticationexception {/*Get user name*/String username=Authenticationtoken.getprincipal (). toString (); /*gets the password, because the password is encrypted, it must be converted to a char array and then to a string * otherwise unrecognized*/String Password=string.valueof ((Char[]) authenticationtoken.getcredentials ()); if(!" Admin. Equals (username)) { Throw Newauthenticationexception (); } if(!" 666 ". Equals (password)) { Throw Newauthenticationexception (); } /*returns the result of the authentication*/ return NewSimpleauthenticationinfo (Username,password, This. GetName ()); }}
To configure a custom realm class in an. ini file
[main] #这里相当于实例化了一个realm对象, here is the classpath myrealm=com. myrealm# here is equivalent to set in a parameter Securitymanager.realms=$myRealm #[users], because of the custom realm adopted, so here is not read #admin= admin[roles]admin=123
test the code click here to link http://www.cnblogs.com/qq376324789/p/8640651.html
* Tips
Subject is equivalent to the current user, realm is the equivalent of DAO, the above code should be written in step-by-step, it is clear that the direct copy should be able to run normally;
Really understand or need to have their own debug step by step to see, will continue to follow the next step.
Shiro (ii) Custom realm, mock database query validation