說明css
(2)Spring Boot 2.0.6
(3)Spring Security 5.0.9
(4)Spring Data JPA 2.0.11.RELEASE
(5)hibernate5.2.17.Final
(6)MySQLDriver 5.1.47
(7)MySQL 8.0.12
需求緣起數據庫
在以前的章節中咱們介紹過經過註解的方式進行權限的控制了,這裏再詳細的講解下方法級安全的幾個註解。安全
1、註解式方法級安全開啓session
須要在WebSecuirtyConfig添加配置:併發
@Configuration
@EnableWebSecurity //啓用Spring Security.
////會攔截註解了@PreAuthrize註解的配置.
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
}
2、容許的註解app
這裏主要@PreAuthorize, @PostAuthorize, @Secured這三個註解可使用。ui
2.1 @Secured編碼
當@EnableGlobalMethodSecurity(securedEnabled=true)的時候,@Secured可使用:spa
@GetMapping("/helloUser")
@Secured({"ROLE_normal","ROLE_admin"})
public String helloUser() {
return "hello,user";
}
說明:擁有normal或者admin角色的用戶均可以方法helloUser()方法。另外須要注意的是這裏匹配的字符串須要添加前綴「ROLE_「。hibernate
若是咱們要求,只有同時擁有admin & noremal的用戶才能方法helloUser()方法,這時候@Secured就無能爲力了。
2.2 @PreAuthorize
Spring的 @PreAuthorize/@PostAuthorize 註解更適合方法級的安全,也支持Spring 表達式語言,提供了基於表達式的訪問控制。
當@EnableGlobalMethodSecurity(prePostEnabled=true)的時候,@PreAuthorize可使用:
@GetMapping("/helloUser")
@PreAuthorize("hasAnyRole('normal','admin')")
public String helloUser() {
return "hello,user";
}
說明:擁有normal或者admin角色的用戶均可以方法helloUser()方法。
此時若是咱們要求用戶必須同時擁有normal和admin的話,那麼能夠這麼編碼:
@GetMapping("/helloUser")
@PreAuthorize("hasRole('normal') AND hasRole('admin')")
public String helloUser() {
return "hello,user";
}
此時若是使用user/123登陸的話,就沒法訪問helloUser()的方法了。
2.3 @PostAuthorize
@PostAuthorize 註解使用並很少,在方法執行後再進行權限驗證,適合驗證帶有返回值的權限,Spring EL 提供 返回對象可以在表達式語言中獲取返回的對象returnObject。
當@EnableGlobalMethodSecurity(prePostEnabled=true)的時候,@PostAuthorize可使用:
@GetMapping("/helloUser")
@PostAuthorize(" returnObject!=null && returnObject.username == authentication.name")
public User helloUser() {
Object pricipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user;
if("anonymousUser".equals(pricipal)) {
user = null;
}else {
user = (User) pricipal;
}
return user;
}
這三個最經常使用也就是@PreAuthorize這個註解了,在使用中主要是配合Spring EL表達式。
歷史文章
215.Spring Boot+Spring Security:初體驗
216.Spring Boot+Spring Security:基於內存的認證信息
217.Spring Boot+Spring Security:基於內存的角色受權
218.Spring Boot+Spring Security:基於內存數據庫的身份認證和角色受權
219.Spring Boot+Spring Security:基於MySQL數據庫的身份認證和角色受權
220.Spring Boot+Spring Security:自定義登陸頁面和構建主頁
221.Spring Boot+Spring Security:登出和403處理
222.Spring Boot+Spring Security:動態加載角色
223.Spring Boot+Spring Security:原理1
224.Spring Boot+Spring Security:自定義Filter
246.Spring Boot+Spring Security:頁面白名單和獲取登陸信息
13. Spring Boot+Spring Security:基於URL動態權限n種方案
248.Spring Boot+Spring Security:基於URL動態權限:準備工做
249.Spring Boot+Spring Security:基於URL動態權限:擴展access()的SpEL表達式
250.Spring Boot+Spring Security:基於URL動態權限:自定義AccssDesionManager
251.Spring Boot+Spring Security:基於URL動態權限:自定義Filter
252.Spring Boot+Spring Security:標籤sec:authorize的使用
253.Spring Boot+Spring Security:獲取用戶信息和session併發控制
我就是我,是顏色不同的煙火。
我就是我,是不同凡響的小蘋果。
à悟空學院:http://t.cn/Rg3fKJD
學院中有Spring Boot相關的課程!
SpringBoot視頻:http://t.cn/R3QepWG
Spring Cloud視頻:http://t.cn/R3QeRZc
SpringBoot Shiro視頻:http://t.cn/R3QDMbh
SpringBoot交流平臺:http://t.cn/R3QDhU0
SpringData和JPA視頻:http://t.cn/R1pSojf
SpringSecurity5.0視頻:http://t.cn/EwlLjHh
Sharding-JDBC分庫分表實戰:http://t.cn/E4lpD6e