Shiro (ii) Custom realm, mock database query validation

Source: Internet
Author: User

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

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.