ApacheShiro User Manual (IV) Realm implementation

Source: Internet
Author: User
As mentioned in the internal implementation mechanism of authentication and authorization, the final processing will be handed over to Real for processing. In Shiro, users, roles, and permissions in applications are obtained through Realm. Generally, in Realm, Shiro needs verification information directly from our data source. It can be said that Realm is a DAO dedicated to the security framework.

Apache Shiro User Manual (IV) Realm implementation

As mentioned in the internal implementation mechanism of authentication and authorization, the final processing will be handed over to Real for processing. In Shiro, users, roles, and permissions in applications are obtained through Realm. Generally, in Realm, Shiro needs verification information directly from our data source. It can be said that Realm is a DAO dedicated to the security framework.

I. authentication implementation

As mentioned above, the Shiro authentication process will be handed over to Realm for execution, and the getAuthenticationInfo (token) method of Realm will be called.

This method mainly performs the following operations:

1. check the submitted token for authentication

2. obtain user information from the data source (usually the database) based on the token information

3. verify the user information.

4. after verification, an AuthenticationInfo instance that encapsulates user information is returned.

5. if verification fails, the AuthenticationException is thrown.

What we need to do in our application is to customize a Realm class, inherit the AuthorizingRealm abstract class, overload doGetAuthenticationInfo (), and override the method for getting 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 { return null; } }

II. authorization implementation

Authorization implementation is very similar to Authentication Implementation. in our custom Realm, reload the doGetAuthorizationInfo () method and override the method for obtaining 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 implementation of the Apache Shiro User Manual (IV) Realm. For more information, see The PHP Chinese website (www.php1.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.