[英]How to use Spring Security .antMatchers() with multiple paths
如果我使用Spring Securitys的“ antMatchers()”方法,是否會有區別?
.antMatchers(
"/",
"/app/**",
"/profiles/**",
"/captcha/**",
c440_START_PAGE,
FAVICON_ICO,
C440_LOGIN,
getCustomerRessourcePath(),
getCustomerWebRessourcePath(),
"/services/userService/**",
"/services/applicationService/**",
"/services/textContentService/**",
"/services/textContentBlockService/**",
"/services/menuItemService/**",
"/services/calculatorService/**"
).permitAll()
或者
.antMatchers("/").permitAll()
.antMatchers("/app/**").permitAll()
.antMatchers("/profiles/**").permitAll()
.antMatchers("/captcha/**").permitAll()
.antMatchers(c440_START_PAGE).permitAll()
.antMatchers(FAVICON_ICO).permitAll()
.antMatchers(C440_LOGIN).permitAll()
.antMatchers(getCustomerRessourcePath()).permitAll()
.antMatchers(getCustomerWebRessourcePath()).permitAll()
.antMatchers("/services/userService/**").permitAll()
.antMatchers("/services/applicationService/**").permitAll()
.antMatchers("/services/textContentService/**").permitAll()
.antMatchers("/services/textContentBlockService/**").permitAll()
.antMatchers("/services/menuItemService/**").permitAll()
.antMatchers("/services/calculatorService/**").permitAll()
? 我是Spring Security的新手,對此不確定...
兩者都與您的實現相同。 但是第二種方式在提供基於角色的授權等方面提供了更大的靈活性。
例如,如果您希望授權角色ADMIN
訪問"/"
,而授權USER
訪問"/app/*"
,則可以通過以下方式實現:
.antMatchers("/").hasRole("ADMIN")
.antMatchers("/app/**").hasRole("USER")
請注意,對於具有相同配置的.permitAll()
模式,不必在末尾僅添加一次.permitAll()
,而不必在每行中都添加一次。
antMatchers
方法的簽名之一是
public C antMatchers(java.lang.String... antPatterns)
這意味着您可以將一個或多個模式傳遞給該方法。 有關更多信息,請參見antMatchers的Spring 文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.