• spring security 核心切面


    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
    
    import java.lang.reflect.Method;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.Set;
    
    /**
     * Authorize切点
     **/
    public class AopAuthorizeAdvisor extends StaticMethodMatcherPointcutAdvisor {
    
        public static final String THREAD_LOCAL_AUTH_CONTEXT = "authContext";
    
        public AopAuthorizeAdvisor(AuthorizeService authorizeService) {
            super((MethodInterceptor) methodInvocation -> {
                MethodInterceptorHolder holder = MethodInterceptorHolder.create(methodInvocation);
                MethodInterceptorContext paramContext = holder.createParamContext();
    
                Authentication authentication = Authentication.current().orElseThrow(UnauthorizedException::new);
    
                AuthContext context = new AuthContext(authentication, paramContext);
                ThreadLocalUtil.put(THREAD_LOCAL_AUTH_CONTEXT, context);
                authorizeService.handle(context);
    
                Set<String> tempIncludes = new HashSet<>();
                Set<String> tempExcludes = new HashSet<>();
    
                paramContext.getParameter(Param.class).ifPresent(
                        param -> {
                            tempIncludes.addAll(param.getIncludes());
                            tempExcludes.addAll(param.getExcludes());
                        }
                );
    
                Object obj = methodInvocation.proceed();
                ThreadLocalUtil.remove(THREAD_LOCAL_AUTH_CONTEXT);
                if (obj != null && obj instanceof ResponseMessage) {
                    ResponseMessage responseMessage = (ResponseMessage) obj;
                    if (CollectionUtils.isNotEmpty(tempIncludes)) {
                        // 设置可以显示的字段
                        responseMessage.setFields(new LinkedHashSet<>(CollectionUtils.removeAll(tempIncludes, tempExcludes)));
                    } else {
                        responseMessage.setFields(new LinkedHashSet<>());
                    }
                }
                return obj;
            });
        }
    
        @Override
        public boolean matches(Method method, Class<?> aClass) {
            //对controller进行控制, 只判断类, 或者父类上是否有Authorize标签.
            return ClassUtil.getAnnotation(aClass, Authorize.class) != null;
        }
    }
    
    
        @Bean
        public AuthenticationSupplier authenticationSupplier() {
            return () -> {
                if (SecurityContextHolder.getContext().getAuthentication() == null) {
                    return null;
                } else {
                    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                    if (principal == null) {
                        return null;
                    } else {
                        return principal instanceof AuthUserDetails ? ((AuthUserDetails)principal).getAuthentication() : null;
                    }
                }
            };
        }
    
  • 相关阅读:
    SQL Server 中关于EXCEPT和INTERSECT的用法
    SQL SERVER 索引中聚集索引分析和Transact-SQL语句优化
    DATEDIFF() 返回2个日期之间的间隔
    CharIndex()
    poj1470 Closest Common Ancestors [ 离线LCA tarjan ]
    2014 蓝桥杯 预赛 c/c++ 本科B组 第九题:地宫取宝(12') [ dp ]
    hdu 2438 Turn the corner [ 三分 ]
    poj 3295 Tautology [ 栈 ]
    hdu 4923 Room and Moor [ 找规律 + 单调栈 ]
    1597: [Usaco2008 Mar]土地购买 [ dp+斜率优化 ] 未完
  • 原文地址:https://www.cnblogs.com/liuyupen/p/14009238.html
Copyright © 2020-2023  润新知