淺談SpringSecurity注解與AOP切面執(zhí)行順序
Spring Security注解@PreAuthorize與AOP切面執(zhí)行順序
引入Spring Security后,在Controller的方法中會(huì)出現(xiàn)Spring Security的方法注解與AOP同時(shí)存在的問題,這是就會(huì)設(shè)計(jì)順序問題
@Controller
public class HelloController{
@RequestMapping(value = "/hello")
@BeforeController
@PreAuthorize("validate(#user)")
public void hello(@RequestParam("user) String user) {
// 業(yè)務(wù)代碼
}
}上述Controller中 @BeforeController 是一個(gè)業(yè)務(wù)AOP, @PreAuthorize 是來授權(quán)校驗(yàn)user。按照業(yè)務(wù)邏輯,執(zhí)行順序應(yīng)該是如下:
Created with Raphaël 2.3.0 request @PreAuthorize @BeforeController 業(yè)務(wù)代碼
但實(shí)際是 @BeforeController 在 @PreAuthorize 之前。 其實(shí) @PreAuthorize 也是依賴于AOP實(shí)現(xiàn),當(dāng)多個(gè)AOP在一個(gè)方法上時(shí)就會(huì)有順序問題。在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 注解里面的值是一個(gè)整數(shù),數(shù)值越大優(yōu)先級(jí)越低,默認(rèn)是 Integer.MIN_VALUE ,即最低優(yōu)先級(jí)。
2、實(shí)現(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 實(shí)現(xiàn)Redis存儲(chǔ)復(fù)雜json格式數(shù)據(jù)并返回給前端
這篇文章主要介紹了Java 實(shí)現(xiàn)Redis存儲(chǔ)復(fù)雜json格式數(shù)據(jù)并返回給前端操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
java對(duì)象list使用stream根據(jù)某一個(gè)屬性轉(zhuǎn)換成map的3種方式舉例
開發(fā)小伙伴們通常會(huì)需要使用到對(duì)象和Map互相轉(zhuǎn)換的開發(fā)場(chǎng)景,下面這篇文章主要給大家介紹了關(guān)于java對(duì)象list使用stream根據(jù)某一個(gè)屬性轉(zhuǎn)換成map的3種方式,需要的朋友可以參考下2024-01-01
Netty源碼分析NioEventLoop執(zhí)行select操作入口
這篇文章主要介紹了Netty源碼分析NioEventLoop執(zhí)行select操作入口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
使用eclipse打包Maven項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了使用eclipse打包Maven項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Java優(yōu)化重復(fù)冗余代碼的8種方式總結(jié)
日常開發(fā)中,我們經(jīng)常會(huì)遇到一些重復(fù)代碼,最近小編優(yōu)化了一些系統(tǒng)中的重復(fù)代碼,用了好幾種的方式,感覺挺有用的,所以本文給大家講講優(yōu)化重復(fù)代碼的幾種方式2023-08-08
springBoot 整合ModBus TCP的詳細(xì)過程
ModBus是一種串行通信協(xié)議,用于從儀器和控制設(shè)備傳輸信號(hào)到主控制器或數(shù)據(jù)采集系統(tǒng),它分為主站和從站,主站獲取和編寫數(shù)據(jù),從站則是設(shè)備,本文給大家介紹springBoot 整合ModBus TCP的詳細(xì)過程,感興趣的朋友一起看看吧2025-01-01

