spring security 核心切面

摘要:
{MethodInterceptorHolderholder=MethodInterceptor holder.create(methodInvocation);MethodInterceptorsContextparamContext=holder.createParamContext();AuthContextcontext=newAuthContext(身份验证;

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;
                }
            }
        };
    }

免责声明:文章转载自《spring security 核心切面》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Qt 5 如何修改打包好的应用程序图标SSH下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

结构体数组(C++)

1.定义结构体数组 和定义结构体变量类似,定义结构体数组时只需声明其为数组即可。如: struct Student{ int num; char name[20]; char sex[5]; int age; float score; char addr[30]; }; Stude...

idea常用配置

idea 下载其他版本: https://www.jetbrains.com/idea/download/other.html Tomcat配置VM Options:  -XX:PermSize=512m -XX:MaxPermSize=1024m 1.IDEA卡顿,修改IDEA使用内存 修改idea配置文件 在IDEA的安装目录下的bin目录下: 用记事...

vue 点击显示隐藏,鼠标移动上去显示隐藏

1,鼠标点击显示隐藏,样式可以自己调,我就写个dome。 <div class="min"> <button @click="change">点击我</button> <div v-show="show"> 显示隐藏的内容 </div>...

原装js轮播图,鼠标移入停止轮播,移出继续轮播

要求:1、点击按钮,切换图片;    2、图片能够自动轮播;       3、鼠标移入,轮播停止;移出继续轮播; 知识点:1、定时器:setInterval();     2、鼠标移入事件:onmouseenter/onmouseover;       鼠标移出事件:onmouseleave/onmouseout; 难点:假设轮播图轮播到第二张图片,此时点...

Xamarin.Forms学习系列之Syncfusion 制作图形报表

Syncfusion是一家微软生态下的第三方组件/控件供应商,除了用于HTML5和JavaScript的控件外,他们产品还涉及如下领域: WEB ASP.NET MVC ASP.NET WebForms HTML5/JavaScript LightSwitch Silverlight MOBILE iOS Android Windows Phone...

Eclipse同时编译多个cpp文件

步骤1、新建工程 File -> new -> project -> C/C++ Project 选择Makefile Project 中的Empty Project. 步骤2、添加测试源文件 在helloworld工程上右键,新建文件夹src和Debug 分别用来存放源文件和可执行文件(new -> Folder) 在src...