Apache Shiro User's Manual (iv) REALM implementation

Source: Internet
Author: User

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. Matching verification of user information
4. Validation by returning 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.
Java code
  1. protected AuthenticationInfo dogetauthenticationinfo (Authenticationtoken authctoken) throws Authenticationexception {
  2. Usernamepasswordtoken token = (usernamepasswordtoken) Authctoken;
  3. User user = Accountmanager.finduserbyusername (token.getusername ());
  4.          if   (user != < span class= "keyword" style= "color:rgb (127, 0, 85); Font-weight:bold ">null )  {  
  5.              return  new   Simpleauthenticationinfo (User.getusername (),  user.getpassword (),  getname ());   
  6. } Else {
  7. return null;
  8. }
  9. }

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.
Java code
  1. protected Authorizationinfo Dogetauthorizationinfo (principalcollection principals) {
  2. String userName = (string) Principals.fromrealm (GetName ()). Iterator (). Next ();
  3. User user = Accountmanager.finduserbyusername (userName);
  4. if (user = null) {
  5.              simpleauthorizationinfo info = new  simpleauthorizationinfo ();   
  6. for (Group group:user.getGroupList ()) {
  7. Info.addstringpermissions (Group.getpermissionlist ());
  8. }
  9. return info;
  10. } Else {
  11. return null;
  12. }
  13. }


Source:http://kdboy.iteye.com/blog/1169631

From for notes (Wiz)

Apache Shiro User's Manual (iv) REALM implementation

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.