Apache Shiro User's Manual (iv) REALM implementation

Source: Internet
Author: User

Apache Shiro User's Manual (iv) REALM implementation

In the authentication, authorization internal implementation mechanism is mentioned, the final processing will be handed over to real for processing. Because in Shiro, it is ultimately through realm to get the user, role, and permission information in the application. Typically, the validation information required by Shiro is obtained directly from our data source in realm. It can be said that realm is a DAO that is dedicated to the security framework.

First, the implementation of certification

As mentioned earlier, the Shiro certification process will eventually be handed over to realm, and the realm's Getauthenticationinfo method will be called.

The method mainly performs the following actions:

1. Check the token information submitted for authentication

2. Obtain user information from a data source (typically a database) based on the token information

3, the user information to match authentication.

4. Validation will return a AuthenticationInfo instance that encapsulates the user's information.

5, the validation fails to throw Authenticationexception exception information.

What we want to do in our application is to customize a realm class, inherit the Authorizingrealm abstract class, Overload dogetauthenticationinfo (), and rewrite the method of getting the user information.

Protected AuthenticationInfo Dogetauthenticationinfo (Authenticationtoken authctoken) throws Authenticationexception {Usernamepasswordtoken token = (usernamepasswordtoken) Authctoken; User user = Accountmanager.finduserbyusername (token.getusername ()); if (user! = null) {return new Simpleauthenticationinfo (User.getusername (), User.getpassword (), GetName ()));} else {retur n null; } }

Second, the authorization to achieve

And the authorization implementation is very similar to the authentication implementation, in our custom realm, the overloaded Dogetauthorizationinfo () method, overriding the way to get user permissions.

Protected Authorizationinfo Dogetauthorizationinfo (PrincipalCollection principals) {String userName = (string) Principals.fromrealm (GetName ()). Iterator (). Next (); User user = Accountmanager.finduserbyusername (userName); if (user! = null) {Simpleauthorizationinfo info = new Simpleauthorizationinfo (); for (Group group:user.getGroupList ()) {info.addstringpermissions (Group.getpermissionlist ());} return info; } else {return null;}}

The above is the Apache Shiro user manual (iv) REALM implementation content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.