com.mingsoft.basic.security.BaseAuthRealm.java Source code

Java tutorial

Introduction

Here is the source code for com.mingsoft.basic.security.BaseAuthRealm.java

Source

/**
The MIT License (MIT) * Copyright (c) 2016 (mingsoft.net)
    
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
    
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
    
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.mingsoft.basic.security;

import java.util.List;

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.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
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.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.mingsoft.base.entity.BaseEntity;
import com.mingsoft.basic.biz.IManagerBiz;
import com.mingsoft.basic.biz.IModelBiz;
import com.mingsoft.basic.entity.ManagerEntity;
import com.mingsoft.basic.entity.ModelEntity;
import com.mingsoft.util.StringUtil;

/**
 * ??
 * 
 * @author killfenQQ:78750478
 * @version ?<br/>
 *          201599<br/>
 *          ?<br/>
 */
public class BaseAuthRealm extends AuthorizingRealm {

    /**
     * ?
     */
    @Autowired
    private IManagerBiz managerBiz;

    /**
     * ?
     */
    @Autowired
    private IModelBiz modelBiz;

    /**
     * 
     */
    public BaseAuthRealm() {
        // TODO Auto-generated constructor stub
        super();
        // ?token
        setAuthenticationTokenClass(UsernamePasswordToken.class);
        // 
        setCredentialsMatcher(new SimpleCredentialsMatcher());
    }

    /**
     * ??
     */
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String loginName = (String) principalCollection.fromRealm(getName()).iterator().next();
        ManagerEntity manager = managerBiz.queryManagerByManagerName(loginName);
        if (null == manager) {
            return null;
        } else {
            SimpleAuthorizationInfo result = new SimpleAuthorizationInfo();
            // ?
            List<BaseEntity> models = modelBiz.queryModelByRoleId(manager.getManagerRoleID());
            for (BaseEntity e : models) {
                ModelEntity me = (ModelEntity) e;
                if (!StringUtil.isBlank(me.getModelUrl())) {
                    result.addStringPermission(me.getModelUrl());
                }
            }
            return result;

        }
    }

    /**
     * ?
     */
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        ManagerEntity manager = managerBiz.queryManagerByManagerName(upToken.getUsername());
        if (manager != null) {
            return new SimpleAuthenticationInfo(manager.getManagerName(), manager.getManagerPassword(), getName());
        }
        return null;
    }

    /**
     * ??.
     */
    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);
            }
        }
    }
}