2016-11-28 11 views
2

私はSpring MVCコントローラを持っており、Spring Method Securityで保護したいと考えています。それは動作しません。この例Spring Security:@PreAuthorizeは@RequestMappingと一緒にしか動作しません。

@Controller 
public class MyController { 

    @RequestMapping(value = "/test", method = {RequestMethod.POST, RequestMethod.GET}) 
    @PreAuthorize("isAuthenticated()") 
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 
     return test(request, response); 
    } 

    public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception { 
     ... 
    } 

- @RequestMapping@PreAuthorize注釈の異なる方法:

@Controller 
public class MyController { 

    @RequestMapping(value = "/test", method = {RequestMethod.POST, RequestMethod.GET}) 
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { 
     return test(request, response); 
    } 

    @PreAuthorize("isAuthenticated()") 
    public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception { 
     ... 
    } 
@RequestMapping@PreAuthorize注釈同じ方法 - 次の例では

それが作品


この奇妙なbの理由は何かあなたは?

+1

http://stackoverflow.com/a/19421786/1291150 –

+0

ありがとう - このことができます! – olivmir

答えて

2

testメソッドは、handleRequestメソッドから直接呼び出されています。 Springには、同じクラスのメソッド呼び出しをインターセプトするメカニズムはありません。したがって、@PreAutorizeのプロキシ/ AOPメソッドの開始は決して呼び出されません。

More on the topic of Spring Proxy

+0

ありがとうございます - 素晴らしい答えです。私は、 '@ PreAuthorize'が記述されているチュートリアルのどこにも言及していないのはなぜでしょうか。 :|どのように良い、それはスタックオーバーフローの専門家がある。 – olivmir

+2

@olivmirほとんどのSpringのドキュメントでSpringがどのように動作するかについては、必要な知識が必要です。ドキュメンテーションがこの想定された知識を持たないフィーチャを記述しようとした場合、またはSpringの動作との関連では、ドキュメントは非常に乱雑で反復的です。 – MarkOfHall

関連する問題