首页>代码>SSM和Spring Security的整合后台权限基础框架>/mis/src/main/java/com/haoyu/security/MyAccessDecisionManager.java
package com.haoyu.security;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class MyAccessDecisionManager implements AccessDecisionManager {

	@Override
	public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
			throws AccessDeniedException, InsufficientAuthenticationException {
		
		if(configAttributes == null){
			throw new AccessDeniedException("no right");
		}
		//所请求的资源拥有的权限(一个资源对多个权限)
        Iterator<ConfigAttribute> ite = configAttributes.iterator();
 
        while (ite.hasNext()) {
 
            ConfigAttribute ca = ite.next();
            //访问所请求资源所需要的权限
            String needRole = ((SecurityConfig) ca).getAttribute();
            System.out.println("needRole is " + needRole);
            // ga 为用户所被赋予的权限。 needRole 为访问相应的资源应该具有的权限。
            for (GrantedAuthority ga : authentication.getAuthorities()) {
 
                if (needRole.trim().equals(ga.getAuthority().trim())) {
 
                    return;
                }
 
            }
 
        }
		throw new AccessDeniedException("no right");
	}

	@Override
	public boolean supports(ConfigAttribute attribute) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean supports(Class<?> clazz) {
		// TODO Auto-generated method stub
		return true;
	}

}
最近下载更多
ewan007  LV29 2023年7月4日
liaozhenlei  LV8 2022年6月7日
ssnqqq  LV3 2022年4月10日
wubinbin  LV11 2022年4月3日
jay1992  LV14 2022年1月17日
tiansitong  LV14 2021年12月5日
17704627276  LV17 2021年11月30日
lxp950227  LV3 2021年10月7日
2875754292  LV3 2021年6月5日
半月  LV2 2021年5月10日
最近浏览更多
minjing123  LV8 1月4日
iiiiiiixiiiiii  LV1 2023年12月31日
shuangfu  LV25 2023年12月2日
Hachi6  LV13 2023年8月17日
ewan007  LV29 2023年7月4日
buling-  LV1 2023年6月8日
wiston 2023年3月30日
暂无贡献等级
 LV7 2023年3月29日
zhenxingbei 2022年12月11日
暂无贡献等级
zdmxjxj  LV9 2022年12月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友