2015-04-01 58 views
0

在我的春季安全配置我有以下設置:通的HttpServletRequest在調用hasPermission表達

@Override 
     protected void configure(HttpSecurity http) throws Exception 
     { 
      http 
     .authorizeRequests() 
      .antMatchers("/login.htm", "/signup.htm").permitAll() 
      .antMatchers("/page1.htm", "/page2.htm", "/page3.htm").access("@permission.hasPermission(principal.username)) 
    .... 
    } 

其中包含了方法調用hasPermission的@permission是@Component豆這決定了主要的用戶名是否具有訪問頁面。在我使用我的dao方法來確定這個bean。但是,我需要更多的知識才能做出決定,因爲它不是一個頁面。例如,有沒有什麼方法可以知道用戶請求了哪個頁面,並將其傳遞給hasPermission方法?換句話說,我想要做類似的事情:

.antMatchers("/page1.htm", "/page2.htm", "/page3.htm").access("@permission.hasPermission(principal.username, HttpServletRequest http)) 

查看方法的第二個參數。這是請求的頁面的http請求,所以我會知道用戶是否請求page1,page2或page3 .. 或者如果我不能將該參數作爲參數傳遞,我如何在我的hasPermission方法實現中獲取當前請求的頁面?

回答

1

您應該可以使用訪問它下面:

@Override 
    protected void configure(HttpSecurity http) throws Exception 
    { 
     http 
     .authorizeRequests() 
      .antMatchers("/login.htm", "/signup.htm").permitAll() 
      .antMatchers("/page1.htm", "/page2.htm", "/page3.htm").access("@permission.hasPermission(principal.username,request)) 
.... 
} 

這是由於事實WebSecurityExpressionRoot.request財產公開爲公衆最終變量