掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
環(huán)境:Springboot2.4.12 + Spring Security 5.4.9

創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元旺蒼做網(wǎng)站,已為上家服務(wù),為旺蒼各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
上一篇:《??Spring Security權(quán)限控制系列(五)??》
有如下接口:
@RestController
@RequestMapping("/business")
public class BussinessController {
@GetMapping("/{id}")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}
}
安全配置:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
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方法都是可以使用的。這些注解也可以直接加到接口方法上。
@Configuration
@EnableGlobalMethodSecurity(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注解的支持。
示例:
@GetMapping("/{id}")
@RolesAllowed("ROLE_USERS") // ①
@Secured("ROLE_USERS1") // ②
@PreAuthorize("hasRole('USERS')") // ③
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}本篇內(nèi)容只演示基于@PreAuthorize注解的權(quán)限控制,其它兩個(gè)都非常簡(jiǎn)單不做演示。
該注解用于指定方法訪問(wèn)控制表達(dá)式的注釋?zhuān)摫磉_(dá)式將被計(jì)算以確定是否允許方法調(diào)用。默認(rèn)支持的如下表達(dá)式:
示例1:
訪問(wèn)該接口必須具備USERS角色。
@PreAuthorize("hasRole('USERS')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}示例2:
訪問(wèn)該接口只要具有其中任意一種角色即可。
@PreAuthorize("hasAnyRole('USERS', 'ADMIN')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}示例3:
訪問(wèn)該接口必須擁有bus:news:see權(quán)限。
@PreAuthorize("hasAuthority('bus:news:see')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}實(shí)例4:
該接口只要擁有如下任意一個(gè)權(quán)限即可。
@PreAuthorize("hasAnyAuthority('bus:news:see', 'bus:news:write')")
public Object get(@PathVariable("id") Integer id) {
return "receive - " + id ;
}注意:這里的hasRole和hasAuthority區(qū)別?
權(quán)限認(rèn)證使用的 表達(dá)式根對(duì)象的基類(lèi)是SecurityExpressionRoot。該基類(lèi)中實(shí)現(xiàn)了相應(yīng)方法的調(diào)用
public abstract class SecurityExpressionRoot implements SecurityExpressionOperations {
private String defaultRolePrefix = "ROLE_";
@Override
public final boolean hasRole(String role) {
return hasAnyRole(role);
}
@Override
public final boolean hasAnyRole(String... roles) {
return hasAnyAuthorityName(this.defaultRolePrefix, roles);
}
@Override
public final boolean hasAuthority(String authority) {
return hasAnyAuthority(authority);
}
@Override
public final boolean hasAnyAuthority(String... authorities) {
return hasAnyAuthorityName(null, authorities);
}
private boolean hasAnyAuthorityName(String prefix, String... roles) {
Set 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_前綴。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流