spring security中的secure何时校验
spring security中的secure何时校验
初始动机
项目引入了spring-security 作为权限校验,在controller方法上加了 Secure 的注解,后面又加了 filter,发现 Secure 的调用是在 filter 之后的,所以想查一下源码看看原因是什么。
从401报错开始查找
Secure校验失败的时候会返回401的错误码,所以就从抛了AuthenticationCredentialsNotFoundException这个异常的代码反向查找,然后在AbstractSecurityInterceptor里看到了credentialsNotFound方法中会抛出这个异常,于是又顺着看,发现了beforeInvocation方法,这个方法应该是在配置了 Secure注解的controller方法调用前被执行的。

在beforeInvocation方法中,可以看到获取Secure值的地方
Collection<ConfigAttribute> attributes = this.obtainSecurityMetadataSource()
.getAttributes(object);
如果attributes值为空,则不做校验,否则会查看authentication是否为空
SecurityContextHolder.getContext().getAuthentication() == null
为空,则说明没有登录
本文探讨了Spring Security中Secure注解的校验时机及流程。通过分析源码,揭示了Secure注解如何在控制器方法调用前进行权限检查,并解释了401错误响应的触发条件。
3453

被折叠的 条评论
为什么被折叠?



