com.imgeeks.shiro.UserRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.imgeeks.shiro.UserRealm.java

Source

/*
 * Copyright (c) 2014, 2015, XIANDIAN and/or its affiliates. All rights reserved.
 * XIANDIAN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.imgeeks.shiro;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.imgeeks.common.service.UserService;
import com.imgeeks.common.service.impl.UserServiceImpl;
import com.imgeeks.common.bean.User;

/**
 * realm
 * 
 * @author ?
 * @since  V1.0
 * 
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    /**
     * ??
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //      String username = (String) getAvailablePrincipal(principals);
        String username = (String) principals.getPrimaryPrincipal();

        //      List<UserRole> urlist = userService.getUserRole(username);

        //      Set<Role> roleSet = userService.findUserByUsername(username).getRoleSet();
        //???
        Set<String> roles = new HashSet<String>();
        //?????
        Set<String> permissions = new HashSet<String>();

        //      for (UserRole ur:urlist)
        //      {
        //         String rolekey = ur.getRole().getRolekey();
        //         roles.add(rolekey);
        //      }
        //      Iterator<Role> it = roleSet.iterator();
        //      while(it.hasNext()){
        //         roles.add(it.next().getName());
        ////         for(Permission per:it.next().getPermissionSet()){
        ////            permissions.add(per.getName());
        ////         }
        //      }

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

        authorizationInfo.addRoles(roles);
        authorizationInfo.addStringPermissions(permissions);

        return authorizationInfo;
    }

    /**
     * ??
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String email = (String) token.getPrincipal();
        User user = userService.getUserByEmail(email);

        if (user == null) {
            //
            throw new UnknownAccountException("????");
        }
        /* if(Boolean.TRUE.equals(user.getLocked())) {  
           throw new LockedAccountException(); //???  
             } */

        /**
         * AuthenticatingRealmCredentialsMatcher????  
         */
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getEmail(), user.getPassword(),
                getName());

        return info;
    }

    @Override
    public String getName() {
        return getClass().getName();
    }

}