自定义 hasPermission 校验规则
自定义一个 Spring Security hasPermission 校验规则:
在 tienchin-framework 模块当中进行自定义,新建 CustomSecurityExpressionRoot.java 自定义 hasPermission 判断逻辑类:
/**
* @author BNTang
* @version 1.0
* @description 自定义 hasPermission 判断逻辑
* @since 2023-08-26
**/
public class CustomSecurityExpressionRoot
extends SecurityExpressionRoot
implements MethodSecurityExpressionOperations {
private Object filterObject;
private Object returnObject;
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
/**
* Creates a new instance
*
* @param authentication the {@link Authentication} to use. Cannot be null.
*/
public CustomSecurityExpressionRoot(Authentication authentication) {
super(authentication);
}
/**
* 判断当前对象是否具备某一个权限
*
* @param permission 权限
* @return boolean
* @author BNTang
* @since 2023/08/26 08:43:56
*/
public boolean hasPermission(String permission) {
// 获取当前登录用户所具有的权限
// 这里实际上调用到的是 top.it6666.common.core.domain.model.LoginUser.getAuthorities 方法的返回值
Collection extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
return false;
}
/**
* 是否具备多个权限中的任意一个权限
*
* @param permissions 权限
* @return boolean
* @author BNTang
* @since 2023/08/26 08:44:52
*/
public boolean hasAnyPermissions(String... permissions) {
if (permissions == null || permissions.length == 0) {
return false;
}
Collection extends GrantedAuthority> authorities = authentication.getAuthorities();
for (GrantedAuthority authority : authorities) {
for (String permission : permissions) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
return true;
}
}
}
return false;
}
/**
* 是否具备拥有所有权限
*
* @param permissions 权限
* @return boolean
* @author BNTang
* @since 2023/08/26 08:44:26
*/
public boolean hasAllPermissions(String... permissions) {
Collection extends GrantedAuthority> authorities = authentication.getAuthorities();
if (permissions == null || permissions.length == 0) {
return false;
}
for (String permission : permissions) {
boolean flag = false;
for (GrantedAuthority authority : authorities) {
if (antPathMatcher.match(authority.getAuthority(), permission)) {
flag = true;
}
}
if (!flag) {
return false;
}
}
return true;
}
@Override
public void setFilterObject(Object filterObject) {
this.filterObject = filterObject;
}
@Override
public Object getFilterObject() {
return filterObject;
}
@Override
public void setReturnObject(Object returnObject) {
this.returnObject = returnObject;
}
@Override
public Object getReturnObject() {
return returnObject;
}
@Override
public Object getThis() {
return this;
}
}
新建自定义 hasPermission 判断逻辑处理器类:
/**
* @author BNTang
* @version 1.0
* @description 自定义 hasPermission 判断逻辑处理器
* @since 2023-08-26
**/
public class CustomMethodSecurityExpressionHandler
extends DefaultMethodSecurityExpressionHandler {
@Override
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication,
MethodInvocation invocation) {
CustomSecurityExpressionRoot root = new CustomSecurityExpressionRoot(authentication);
root.setTrustResolver(getTrustResolver());
root.setPermissionEvaluator(getPermissionEvaluator());
root.setRoleHierarchy(getRoleHierarchy());
return root;
}
}
注册一下自定义 hasPermission 判断逻辑处理器,更改 ResourcesConfig:
/**
* 自定义 hasPermission 判断逻辑处理器
*
* @return {@code CustomMethodSecurityExpressionHandler }
* @author BNTang
* @since 2023/08/26 08:57:19
*/
@Bean
CustomMethodSecurityExpressionHandler customMethodSecurityExpressionHandler() {
return new CustomMethodSecurityExpressionHandler();
}
更改 LoginUser,完善一下 LoginUser 当中的 getAuthorities 方法:
@Override
public Collection extends GrantedAuthority> getAuthorities() {
if (permissions != null && !permissions.isEmpty()) {
return permissions.stream()
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
}
return Collections.emptyList();
}
编写查询接口
更改 ChannelController:
/**
*
* 渠道管理表 前端控制器
*
*
* @author BNTang
* @since 2023-08-22
*/
@RestController
@RequestMapping("/tienchin/channel")
public class ChannelController extends BaseController {
@Resource
private IChannelService iChannelService;
@PreAuthorize("hasPermission('tienchin:channel:list')")
@GetMapping("/list")
TableDataInfo list() {
startPage();
return getDataTable(iChannelService.selectChannelList());
}
}
更改 IChannelService:
/**
*
* 渠道管理表 服务类
*
*
* @author BNTang
* @since 2023-08-22
*/
public interface IChannelService
extends IService {
/**
* 查询渠道列表
*
* @return {@code List }
* @author BNTang
* @since 2023/08/26 09:32:57
*/
List selectChannelList();
}
更改 ChannelServiceImpl:
/**
*
* 渠道管理表 服务实现类
*
*
* @author BNTang
* @since 2023-08-22
*/
@Service
public class ChannelServiceImpl
extends ServiceImpl
implements IChannelService {
@Resource
private ChannelMapper channelMapper;
@Override
public List selectChannelList() {
return channelMapper.selectChannelList();
}
}
更改 ChannelMapper:
/**
*
* 渠道管理表 Mapper 接口
*
*
* @author BNTang
* @since 2023-08-22
*/
public interface ChannelMapper extends BaseMapper {
/**
* 查询渠道列表
*
* @return {@code List }
* @author BNTang
* @since 2023/08/26 09:33:46
*/
List selectChannelList();
}
更改 ChannelMapper.xml: