2016-01-17 146 views
1

,所以我有樣春季安全設置如下 -爲什麼春季安全攔截的URL爲了此事

<security:intercept-url pattern="/accounts/search" 
     access="hasAnyRole('ROLE_VIEWER')" /> 

    <security:intercept-url pattern="/accounts/*" 
     access="hasAnyRole('ROLE_ADMIN')" /> 

按照上面是用戶角色ROLE_VIEWER他應該能夠訪問/accounts/search其中與角色的用戶ROLE_ADMIN有也可以訪問從/accounts開始的所有工作流程。這裏ROLE_ADMIN的用戶無法訪問搜索。

所以我想知道什麼可以是這種設計的用例順序很重要?

如果控制器

@RequestMapping("/simple/*") 
public @ResponseBody String simple1() { 
    return "Hello world1!"; 
} 

@RequestMapping("/simple/test") 
public @ResponseBody String simple2() { 
    return "Hello world2!"; 
} 

/simple/test的情況下將匹配對應於simple2而非simple1處理方法無關的量級。

回答

0

一般來說:只要你在url-pattern中使用通配符,並且url開始匹配不同的url-patterns,那麼順序就很重要。

一個典型的用例是允許的網址的白名單:除了一些明確定義的網址,所有內容都被拒絕(或僅限管理員)。通過彈簧安全,您可以通過這種方式典型地配置白名單:

<!-- explicit allowed urls --> 
<security:intercept-url pattern="/somethingAllowd" access="permitAll" /> 
... 

<!-- "catch all" that deny all not explicit allowed urls except for admns --> 
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_ADMIN')" /> 

現在訂單很重要!