1、Spring Security默認是禁用注解的,要想開啟注解, 需要在繼承WebSecurityConfigurerAdapter的類上加@EnableGlobalMethodSecurity注解, 來判斷用戶對某個控制層的方法是否具有訪問權限
@Configuration
@EnableWebSecurity
@EnableAutoConfiguration
@EnableGlobalMethodSecurity(prePostEnabled =true)
public class WebSecurityConfigextends WebSecurityConfigurerAdapter {……}
2、例如下面代碼就表示如果用戶具有admin角色,就能訪問listAllUsers方法,但是如果方法前不加@preAuthorize注解,意味著所有用戶都能訪問listAllUsers
@PreAuthorize("hasRole(‘admin‘)")
@RequestMapping(value = "/user/", method = RequestMethod.GET)
@ResponseBody
publicList listAllUsers() {
……
}
3、@EnableGlobalMethodSecurity詳解
3.1、@EnableGlobalMethodSecurity(securedEnabled=true) 開啟@Secured 注解過濾權限
3.2、@EnableGlobalMethodSecurity(jsr250Enabled=true)開啟@RolesAllowed 注解過濾權限
3.3、@EnableGlobalMethodSecurity(prePostEnabled=true) 使用表達式時間方法級別的安全性 4個注解可用
@PreAuthorize 在方法調用之前,基于表達式的計算結果來限制對方法的訪問
@PostAuthorize 允許方法調用,但是如果表達式計算結果為false,將拋出一個安全性異常
@PostFilter 允許方法調用,但必須按照表達式來過濾方法的結果
@PreFilter 允許方法調用,但必須在進入方法之前過濾輸入值
作者:誰在烽煙彼岸
鏈接:http://www.itdecent.cn/p/41b7c3fb00e0
來源:簡書
著作權歸作者所有。商業(yè)轉載請聯(lián)系作者獲得授權,非商業(yè)轉載請注明出處。