审查视图

src/main/java/com/tianbo/warehouse/security/MyAccessDecisionManager.java 4.0 KB
朱兆平 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package com.tianbo.warehouse.security;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

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

@Service
public class MyAccessDecisionManager implements AccessDecisionManager{
朱兆平 authored
16
     /**这里没用AccessDecisionVoter访问投票管理,自定义用户的role_name与URL需要的ROLE_NAME对碰决定,参考资料:https://blog.csdn.net/kaikai8552/article/details/3965841
17 18 19 20 21 22 23 24 25 26
     * decide方法接收三个参数,decide 方法是判定是否拥有权限的决策方法
     * 其中第一个参数中保存了当前登录用户的角色信息,authentication 是释CustomUserService中循环添加到 GrantedAuthority 对象中的权限信息集合.
     * object 包含客户端发起的请求的requset信息,可转换为 HttpServletRequest request = ((FilterInvocation) object).getHttpRequest();
     * 第三个参数则是MyInvocationSecurityMetadataSourceService中的getAttributes方法传来的,表示当前请求需要的角色(可能有多个),此方法是为了判定用户请求的url 是否在权限表中,如果在权限表中,则返回给 decide 方法,用来判定用户是否有此权限。如果不在权限表中则放行。
     * @param authentication
     * @param object
     * @param configAttributes
     * @throws AccessDeniedException
     * @throws InsufficientAuthenticationException
     */
朱兆平 authored
27 28 29 30 31 32 33 34 35 36 37
    @Override
    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException{

        if(null== configAttributes || configAttributes.size() <=0) {
            return;
        }
        ConfigAttribute c;
        String needRole;
        for(Iterator<ConfigAttribute> iter = configAttributes.iterator(); iter.hasNext(); ) {
            c = iter.next();
            needRole = c.getAttribute();
朱兆平 authored
38
39
朱兆平 authored
40
朱兆平 authored
41 42
            //authentication 为在注释1 中循环添加到 GrantedAuthority 对象中的权限信息集合
            for(GrantedAuthority ga : authentication.getAuthorities()) {
43 44 45 46 47 48

                //如果URL需要的权限为匿名访问,返回
                if(("ROLE_admin").equals(ga.getAuthority())){
                    return;
                }
朱兆平 authored
49 50 51 52 53
                if(needRole.trim().equals(ga.getAuthority())) {
                    return;
                }
            }
        }
54
        throw new AccessDeniedException("权限不足!");
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

        /**
         * ExceptionTranslationFilter

         ExceptionTranslationFilter 是Spring Security的核心filter之一,用来处理AuthenticationException和AccessDeniedException两种异常。

         在我们的例子中,AuthenticationException指的是未登录状态下访问受保护资源,AccessDeniedException指的是登陆了但是由于权限不足(比如普通用户访问管理员界面)。

         ExceptionTranslationFilter 持有两个处理类,分别是AuthenticationEntryPoint和AccessDeniedHandler。

         ExceptionTranslationFilter 对异常的处理是通过这两个处理类实现的,处理规则很简单:

         规则1. 如果异常是 AuthenticationException,使用 AuthenticationEntryPoint 处理
         规则2. 如果异常是 AccessDeniedException 且用户是匿名用户,使用 AuthenticationEntryPoint 处理
         规则3. 如果异常是 AccessDeniedException 且用户不是匿名用户,如果否则交给 AccessDeniedHandler 处理。
         */
朱兆平 authored
71 72 73 74 75 76 77 78 79 80 81 82
    }

    @Override
    public boolean supports(ConfigAttribute var1){
        return true;
    }

    @Override
    public boolean supports(Class<?> var1){
        return true;
    }
}