⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authenticationhelper.java

📁 anewssystem新闻发布系统集成使用了spring hibernate freemarker EXTJS等开源框架 可以作为学习参考
💻 JAVA
字号:
package anni.core.security;

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.ConfigAttributeEditor;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.GrantedAuthorityImpl;

import org.apache.commons.beanutils.BeanUtils;


/**
 * 授权帮助类.
 * 来自www.springside.org.cn
 *
 * @author Lingo
 * @since 2007-03-22
 * @version 1.0
 */
public final class AuthenticationHelper {
    /**
     * 工具类的私有构造方法.
     */
    private AuthenticationHelper() {
    }

    /**
     * 由GrantedAuthority Collection 转为 GrantedAuthority 数组.
     *
     * @param auths 授权集合
     * @return 授权数组
     */
    public static GrantedAuthority[] convertToGrantedAuthority(
        Collection<GrantedAuthorityImpl> auths) {
        return (GrantedAuthority[]) auths.toArray(new GrantedAuthority[auths
            .size()]);
    }

    /**
     * 把Bean中的某个属性的值转化为GrantedAuthority.
     *
     * @param list Collection 一组Bean
     * @param propertyName 属性名
     * @return GrantedAuthority[] GrantedAuthority数组
     */
    public static GrantedAuthority[] convertToGrantedAuthority(
        Collection list, String propertyName) {
        Set<GrantedAuthorityImpl> authorities = new HashSet<GrantedAuthorityImpl>();

        try {
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                String authority = (String) BeanUtils.getProperty(iter.next(),
                        propertyName);
                authorities.add(new GrantedAuthorityImpl(authority));
            }
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        } catch (InvocationTargetException e) {
            throw new IllegalArgumentException(e);
        } catch (NoSuchMethodException e) {
            throw new IllegalArgumentException(e);
        }

        return convertToGrantedAuthority(authorities);
    }

    /**
     * 把权限组转为 ConfigAttributeDefinition.
     *
     * @param authorities 权限
     * @param isProtectAllResource 是否保护所有资源
     * true,则所有资源默认为受保护
     * false则只有声明了并且与权限挂钩了的资源才会受保护
     * @return ConfigAttributeDefinition
     */
    public static ConfigAttributeDefinition getCadByAuthorities(
        Collection<GrantedAuthority> authorities,
        boolean isProtectAllResource) {
        if ((authorities == null) || (authorities.size() == 0)) {
            if (isProtectAllResource) {
                return new ConfigAttributeDefinition();
            } else {
                return null;
            }
        }

        ConfigAttributeEditor configAttrEditor = new ConfigAttributeEditor();

        //String authoritiesStr = " ";
        StringBuffer buff = new StringBuffer(" ");

        for (Iterator iter = authorities.iterator(); iter.hasNext();) {
            GrantedAuthority authority = (GrantedAuthority) iter.next();
            //authoritiesStr += (authority.getAuthority() + ",");
            buff.append(authority.getAuthority()).append(",");
        }

        String authoritiesStr = buff.toString();

        String authStr = authoritiesStr.substring(0,
                authoritiesStr.length() - 1);
        configAttrEditor.setAsText(authStr);

        return (ConfigAttributeDefinition) configAttrEditor.getValue();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -