com.thinkgem.jeesite.modules.sys.security.PhoneAuthorizingRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.thinkgem.jeesite.modules.sys.security.PhoneAuthorizingRealm.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.thinkgem.jeesite.modules.sys.security;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
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.credential.HashedCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import com.thinkgem.jeesite.common.servlet.ValidateCodeServlet;
import com.thinkgem.jeesite.common.utils.Encodes;
import com.thinkgem.jeesite.common.utils.SpringContextHolder;
import com.thinkgem.jeesite.modules.sys.entity.Menu;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.sys.web.LoginController;

/**
 * ?
 * @author ThinkGem
 * @version 2013-5-29
 */
@Service
@DependsOn({ "userDao", "roleDao", "menuDao" })
public class PhoneAuthorizingRealm extends AuthorizingRealm {

    private SystemService systemService;

    /**
     * ?, 
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
            throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

        if (LoginController.isValidateCodeLogin(token.getUsername(), false, false)) {
            // ??
            Session session = SecurityUtils.getSubject().getSession();
            String code = (String) session.getAttribute(ValidateCodeServlet.VALIDATE_CODE);
            if (token.getCaptcha() == null || !token.getCaptcha().toUpperCase().equals(code)) {
                throw new CaptchaException("??.");
            }
        }

        User user = getSystemService().getUserByPhone(token.getUsername());
        if (user != null) {
            byte[] salt = Encodes.decodeHex(user.getPassword().substring(0, 16));
            return new SimpleAuthenticationInfo(new Principal(user), user.getPassword().substring(16),
                    ByteSource.Util.bytes(salt), getName());
        } else {
            return null;
        }
    }

    /**
     * ?, ???
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        Principal principal = (Principal) getAvailablePrincipal(principals);
        //      Long userId = (Long) principals.fromRealm(getName()).iterator().next();
        //      User user = getSystemService().getUserByPhone(userId+"");
        User user = getSystemService().getUserByPhone(principal.getLoginName());
        if (user != null) {
            UserUtils.putCache("user", user);
            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
            List<Menu> list = UserUtils.getMenuList();
            for (Menu menu : list) {
                if (StringUtils.isNotBlank(menu.getPermission())) {
                    // Permission???
                    for (String permission : StringUtils.split(menu.getPermission(), ",")) {
                        info.addStringPermission(permission);
                    }
                }
            }
            // IP
            getSystemService().updateUserLoginInfo(user.getId());
            return info;
        } else {
            return null;
        }
    }

    /**
     * ?Hash
     */
    @PostConstruct
    public void initCredentialsMatcher() {
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(SystemService.HASH_ALGORITHM);
        matcher.setHashIterations(SystemService.HASH_INTERATIONS);
        setCredentialsMatcher(matcher);
    }

    /**
     * ?????
     */
    public void clearCachedAuthorizationInfo(String principal) {
        SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
        clearCachedAuthorizationInfo(principals);
    }

    /**
     * ??
     */
    public void clearAllCachedAuthorizationInfo() {
        Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
        if (cache != null) {
            for (Object key : cache.keys()) {
                cache.remove(key);
            }
        }
    }

    /**
     * ?
     */
    public SystemService getSystemService() {
        if (systemService == null) {
            systemService = SpringContextHolder.getBean(SystemService.class);
        }
        return systemService;
    }

    /**
     * ??
     */
    //   public static class Principal implements Serializable {
    //
    //      private static final long serialVersionUID = 1L;
    //      
    //      private String id;
    //      private String loginName;
    //      private String name;
    //      private Map<String, Object> cacheMap;
    //
    //      public Principal(User user) {
    //         this.id = user.getId();
    //         this.loginName = user.getPhone();
    //         this.name = user.getName();
    //      }
    //
    //      public String getId() {
    //         return id;
    //      }
    //
    //      public String getLoginName() {
    //         return loginName;
    //      }
    //
    //      public String getName() {
    //         return name;
    //      }
    //
    //      public Map<String, Object> getCacheMap() {
    //         if (cacheMap==null){
    //            cacheMap = new HashMap<String, Object>();
    //         }
    //         return cacheMap;
    //      }
    //
    //   }
}