淺談SpringSecurity注解與AOP切面執(zhí)行順序
Spring Security注解@PreAuthorize與AOP切面執(zhí)行順序
引入Spring Security后,在Controller的方法中會出現(xiàn)Spring Security的方法注解與AOP同時存在的問題,這是就會設(shè)計順序問題
@Controller
public class HelloController{
@RequestMapping(value = "/hello")
@BeforeController
@PreAuthorize("validate(#user)")
public void hello(@RequestParam("user) String user) {
// 業(yè)務(wù)代碼
}
}上述Controller中 @BeforeController 是一個業(yè)務(wù)AOP, @PreAuthorize 是來授權(quán)校驗user。按照業(yè)務(wù)邏輯,執(zhí)行順序應(yīng)該是如下:
Created with Raphaël 2.3.0 request @PreAuthorize @BeforeController 業(yè)務(wù)代碼
但實際是 @BeforeController 在 @PreAuthorize 之前。 其實 @PreAuthorize 也是依賴于AOP實現(xiàn),當(dāng)多個AOP在一個方法上時就會有順序問題。在Aop中指定順序的方法有:
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BeforeController {
}1、 @Order 注解
@Aspect
@Component
@Order(0)
public class BeforeControllerAction {
@Before("@annotation(org.numb.web.aop.BeforeController)")
public void before(final JoinPoint joinPoint) {
// before切面業(yè)務(wù)代碼
}
}@Order 注解里面的值是一個整數(shù),數(shù)值越大優(yōu)先級越低,默認是 Integer.MIN_VALUE ,即最低優(yōu)先級。
2、實現(xiàn) org.springframework.core.Ordered 接口
@Aspect
@Component
public class BeforeControllerAction implements Ordered {
@Before("@annotation(org.numb.web.aop.BeforeController)")
public void before(final JoinPoint joinPoint) {
// before切面業(yè)務(wù)代碼
}
@Override
public int getOrder() {
return 0;
}
}3、通過配置文件配置順序
<aop:config expose-proxy="true">
<aop:pointcut id="beforeController" expression="@annotation(org.numb.web.aop.BeforeController)"/>
<aop:aspect ref="beforeControllerAction" id="beforeControllerAction">
<aop:before method="before" pointcut-ref="beforeController"/>
</aop:aspect>
</aop:config>而Spring Security的執(zhí)行順序本質(zhì)上也是由AOP決定,可以通過指定order的方式確定:
@EnableGlobalMethodSecurity(order = 0)
查看源碼可見
public @interface EnableGlobalMethodSecurity {
/**
* Indicate the ordering of the execution of the security advisor when multiple
* advices are applied at a specific joinpoint. The default is
* {@link Ordered#LOWEST_PRECEDENCE}.
* @return the order the security advisor should be applied
*/
int order() default Ordered.LOWEST_PRECEDENCE;
}到此這篇關(guān)于淺談SpringSecurity注解與AOP切面執(zhí)行順序的文章就介紹到這了,更多相關(guān)注解與AOP執(zhí)行順序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的while無限循環(huán)結(jié)構(gòu)及實例
這篇文章主要介紹了Java中的while無限循環(huán)結(jié)構(gòu)及實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
spring+maven實現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細介紹了spring+maven實現(xiàn)發(fā)送郵件功能,利用spring提供的郵件工具來發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Spring的FactoryBean<Object>接口示例代碼
FactoryBean是Spring框架中的一個接口,用于創(chuàng)建和管理Bean對象,它的作用是將Bean的創(chuàng)建過程交給FactoryBean實現(xiàn)類來完成,而不是直接由Spring容器來創(chuàng)建,本文給大家介紹Spring的FactoryBean<Object>接口,感興趣的朋友一起看看吧2023-11-11
Java使用StampedLock實現(xiàn)高效讀寫功能
StampedLock 是 Java 8 引入的高性能鎖,提供了三種鎖模式:寫鎖、悲觀讀鎖和樂觀讀鎖,與傳統(tǒng)的 ReentrantReadWriteLock 相比,StampedLock 更注重性能,特別適合讀多寫少的場景,所以本文給大家介紹了Java使用StampedLock實現(xiàn)高效讀寫功能,需要的朋友可以參考下2025-01-01
Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解
這篇文章主要介紹了Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04

