簡體   English   中英

Spring-Security 3 / Spring MVC和可怕的@Secured / RequestMapping

[英]Spring-Security 3/Spring MVC and the dreaded @Secured/RequestMapping

我在向控制器添加安全注釋時遇到了很多問題。

事實證明讓我的Controller實現一個InitializingBean是一個壞主意。

public class MyController implements InitializingBean {

    @Secured(value="ROLE_ADMIN")
    @RequestMapping(method = RequestMethod.GET, value = "/{id}/edit")
    public String getView(Model model, @PathVariable("id") long id) {
        return "some view";
    }
}

這失敗了:

WARN PageNotFound:962 - 找不到帶URI的HTTP請求的映射[...]

刪除@Secured Annotation會起作用,但顯然我不想這樣做。 在網上浪費了大量時間之后,我注意到工作和非工作控制器之間的最后一個區別是它實現了InitializingBean接口。 而現在這就像一個魅力:

public class MyController{

    @Secured(value="ROLE_ADMIN")
    @RequestMapping(method = RequestMethod.GET, value = "/{id}/edit")
    public String getView(Model model, @PathVariable("id") long id) {
        return "some view";
    }
}

任何人都可以幫我理解這種行為嗎?

發生這種情況是因為使用JDK動態代理應用安全方面時會丟失對注釋的訪問,這在默認情況下會在建議的bean實現任何接口時發生。

要解決此問題,您應該告訴Spring Security僅使用基於目標類的代理,使用<global-method-security proxy-target-class = "true" ...> ...<aop:config proxy-target-class = "true" />適用)。

更多關於AOP代理的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM