首页>代码>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;
	}

}
最近下载更多
15719908287  LV10 2024年11月1日
微信网友_7134912998903808  LV15 2024年9月14日
taoshen95  LV16 2024年5月15日
ewan007  LV30 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日
最近浏览更多
微信网友_7826662819680256 2025年12月24日
暂无贡献等级
15719908287  LV10 2024年11月1日
微信网友_7134912998903808  LV15 2024年9月14日
TY0165  LV20 2024年6月24日
taoshen95  LV16 2024年5月15日
minjing123  LV8 2024年1月4日
iiiiiiixiiiiii  LV1 2023年12月31日
shuangfu  LV25 2023年12月2日
Hachi6  LV13 2023年8月17日
ewan007  LV30 2023年7月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友