Spring Security權(quán)限控制系列(六)
環(huán)境:Springboot2.4.12 + Spring Security 5.4.9
本篇主要內(nèi)容:
- 業(yè)務(wù)接口權(quán)限認(rèn)證
上一篇:《??Spring Security權(quán)限控制系列(五)??》
演示案例
有如下接口:
("/business")
public class BussinessController {
("/{id}")
public Object get( ("id") Integer id) {
return "receive - " + id ;
}
}
安全配置:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable() ;
http.authorizeRequests().antMatchers("/resources/**", "/cache/**", "/process/login").permitAll() ;
http.authorizeRequests().antMatchers("/demos/**").hasRole("USERS") ;
http.authorizeRequests().antMatchers("/api/**").hasRole("ADMIN") ;
// 上面的配置都是基于之前的文章,這里我們不需要關(guān)心,僅僅看下面這個(gè)接口配置接口
// 這里我們會(huì)要求所有以/business開(kāi)始的所有請(qǐng)求
http.authorizeRequests().antMatchers("/business/**").authenticated() ;
}
}
有了上面的配置,啟動(dòng)服務(wù)訪問(wèn)http://localhost:8080/business/100接口時(shí)會(huì)要求登錄,只要登錄成功,接口就可以訪問(wèn)。
這里我不希望通過(guò)如下方式進(jìn)行的權(quán)限設(shè)置:
// hasRole("xxx") 或 hasAuthority("xxxx")
http.authorizeRequests().antMatchers("/business/**").hasRole("xxx")
這種寫(xiě)法限定了所有的/business開(kāi)頭的請(qǐng)求都由于固定的權(quán)限,/business可能會(huì)有很多的子接口,每種子接口可能我們都需要定義不同的權(quán)限才可訪問(wèn),這時(shí)候如果在通過(guò)上面的方式配置就太繁瑣了。Spring Security還提供了基于訪問(wèn)注解的方式細(xì)化接口權(quán)限的控制定義,接下來(lái)使用基于注解的方式控制Controller接口權(quán)限。
注意:并不是基于注解的權(quán)限控制只能應(yīng)用到Controller上,只是我們一般都會(huì)加到Controller上;其實(shí)任何Service方法都是可以使用的。這些注解也可以直接加到接口方法上。
開(kāi)啟方法認(rèn)證
(jsr250Enabled = true, prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
屬性說(shuō)明:
jsr250Enabled:?jiǎn)⒂脤?duì)JSR-250注釋的支持。@RolesAllowed。
prePostEnabled:?jiǎn)⒂没诒磉_(dá)式的語(yǔ)法支持(jsr250Enabled和securedEnabled都是基于簡(jiǎn)單角色的約束)。@PreAuthorize。
securedEnabled:?jiǎn)⒂聾Secured注解的支持。
示例:
"/{id}")(
("ROLE_USERS") // ①
("ROLE_USERS1") // ②
("hasRole('USERS')") // ③
public Object get( ("id") Integer id) {
return "receive - " + id ;
}
- 接收一個(gè)String[] 數(shù)組,可以定義多個(gè)角色。
- 接收一個(gè)String[] 數(shù)組,可以定義多個(gè)角色。
- 可以使用SpEL表達(dá)式。
本篇內(nèi)容只演示基于@PreAuthorize注解的權(quán)限控制,其它兩個(gè)都非常簡(jiǎn)單不做演示。
PreAuthorize注解使用
該注解用于指定方法訪問(wèn)控制表達(dá)式的注釋,該表達(dá)式將被計(jì)算以確定是否允許方法調(diào)用。默認(rèn)支持的如下表達(dá)式:
示例1:
訪問(wèn)該接口必須具備USERS角色。
"hasRole('USERS')")(
public Object get( ("id") Integer id) {
return "receive - " + id ;
}
示例2:
訪問(wèn)該接口只要具有其中任意一種角色即可。
"hasAnyRole('USERS', 'ADMIN')")(
public Object get( ("id") Integer id) {
return "receive - " + id ;
}
示例3:
訪問(wèn)該接口必須擁有bus:news:see權(quán)限。
"hasAuthority('bus:news:see')")(
public Object get( ("id") Integer id) {
return "receive - " + id ;
}
實(shí)例4:
該接口只要擁有如下任意一個(gè)權(quán)限即可。
"hasAnyAuthority('bus:news:see', 'bus:news:write')")(
public Object get( ("id") Integer id) {
return "receive - " + id ;
}
注意:這里的hasRole和hasAuthority區(qū)別?
權(quán)限認(rèn)證使用的 表達(dá)式根對(duì)象的基類是SecurityExpressionRoot。該基類中實(shí)現(xiàn)了相應(yīng)方法的調(diào)用
public abstract class SecurityExpressionRoot implements SecurityExpressionOperations {
private String defaultRolePrefix = "ROLE_";
public final boolean hasRole(String role) {
return hasAnyRole(role);
}
public final boolean hasAnyRole(String... roles) {
return hasAnyAuthorityName(this.defaultRolePrefix, roles);
}
public final boolean hasAuthority(String authority) {
return hasAnyAuthority(authority);
}
public final boolean hasAnyAuthority(String... authorities) {
return hasAnyAuthorityName(null, authorities);
}
private boolean hasAnyAuthorityName(String prefix, String... roles) {
Set<String> roleSet = getAuthoritySet();
for (String role : roles) {
// 拼接ROLE_前綴
String defaultedRole = getRoleWithDefaultPrefix(prefix, role);
if (roleSet.contains(defaultedRole)) {
return true;
}
}
return false;
}
}
通過(guò)上面的源碼知道,不管是hasRole還是hasAuthority最終都是調(diào)用的hasAnyAuthorityName方法,而hasRole方法拼接ROLE_前綴。
總結(jié):
- 業(yè)務(wù)接口Controller權(quán)限控制個(gè)各種方式。