第三节 整合springsecurity实现基于RBAC的用户登录

svbadmin学习日志

本学习日志是使用Springboot和Vue来搭建的后台管理系统:
演示地址:http://118.31.68.110:8081/index.html
账号:root
密码:123
所有代码可以在gitbub上找到,切换到相应分支即可。【代码传送门

正篇

第一节 spring boot 模块化构建项目
第二节 整合mybatisplus完成用户增删改查
第三节 整合springsecurity实现基于RBAC的用户登录
第四节 springsecurity结合jwt实现前后端分离开发
第五节 使用ResponseBodyAdvice格式化接口输出
第六节 springboot结合redis实现缓存策略
第七节 springboot结合rabbitmq实现队列消息
第八节 springboot结合rabbitmq实现异步邮件发送
第九节 利用springboot的aop实现行为日志管理
第十节 利用Quartz实现数据库定时备份
第十一节 springboot配置log输出到本地文件
第十二节 使用flyway对数据库进行版本管理
第十三节 springboot配合VbenAdmin实现前端登录
第十四节 springboot配合VbenAdmin实现用户CURD
第十五节 基于RBAC的权限管理VbenAdmin前端实现
第十六节 springboot 打包vue代码实现前后端统一部署

番外

2.1 数据库设计原则
3.1 配置apifox自动获取登录的token
13.1 springboot 全局捕捉filter中的异常
14.1 springsecurity整合mybatisplus出现isEnable的问题和解决方案


前言

权限认证中还是spring security 用的比较多,不过spring boot 2.7以后有些配置有改动。


一、基于RBAC的权限控制

RBAC就不多介绍了,没听过的同学可以看下底下的参考文档。加上之前建立的user表共五张表,如下(建表sql语句可以到文章最后的代码链接中找到):
在这里插入图片描述

二、添加spring security

以下只贴核心代码,完整代码可以到文章最后的代码链接中找到

1.admin-web加入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

2.创建实体类

User 需实现 UserDetails 以及 Role 和 Permission

@TableField(exist = false)
private List<Role> roles;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
    List<SimpleGrantedAuthority> authorities = new ArrayList<>();
    for (Role role: roles) {
        authorities.add(new SimpleGrantedAuthority(role.getName()));
    }
    return authorities;
}

3.创建UserService

UserServiceImpl 需要实现UserDetailsService

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("username",username);
    User user = userMapper.selectOne(queryWrapper);
    System.out.println(user);
    if (user == null){
        throw new UsernameNotFoundException("账户不存在");
    }
    user.setRoles(userMapper.getUserRolesByUid(user.getId()));
    System.out.println(user);
    return user;
}

3.创建UserMapper

UserMapper 要继承 BaseMapper

//根据用户id,获得他所有的角色
List<Role> getUserRolesByUid(BigInteger id);
//获取用户列表,及其包含的角色信息
List<User> getUsersWithRoles();

如果要在mapper中自定查询方法,需要在admin-mapper中创建resources文件,然后创建xml

4.创建配置文件

主配置文件 SecurityConfiguration

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                @Override
                public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                    object.setSecurityMetadataSource(cfisms());
                    object.setAccessDecisionManager(cadm());
                    return object;
                }
            })
            .and()
            .formLogin()
            .loginProcessingUrl("/login").permitAll()
            .and()
            .csrf().disable()
            .userDetailsService(userService);
    return http.build();
}

根据请求路径筛选角色配置

@Override
public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
    FilterInvocation filterInvocation = (FilterInvocation) object;
    String requestUrl =filterInvocation.getRequestUrl(); // 请求url
    String method = filterInvocation.getHttpRequest().getMethod(); // 请求的方法
    List<Permission> allPermission = permissionMapper.getAllPermissions();
    List<String> roleArr = new ArrayList<String>();
    for (Permission permission : allPermission) {
        if(antPathMatcher.match(permission.getPattern(),requestUrl)){ // 先判断URL路径是否符合
            if ("ANY".equals(permission.getMethod())
                    || method.equals(permission.getMethod())){  // 再判断方法是否符合
                List<Role> roles = permission.getRoles();
                for (int i = 0; i < roles.size(); i++) {
                    roleArr.add(roles.get(i).getName());
                }
            }
        }
    }
    if (roleArr.size() > 0){ // 找到匹配的角色
        String[] roleNames = new String[roleArr.size()];
        roleArr.toArray(roleNames);
        return SecurityConfig.createList(roleNames);
    }
    return SecurityConfig.createList("ROLE_LOGIN");
}

匹配登录角色配置

public void decide(Authentication auth, Object object, Collection<ConfigAttribute> ca) throws AccessDeniedException, InsufficientAuthenticationException {
    Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();// 当前用户拥有的角色
    for (ConfigAttribute configAttribute : ca) { // 当前URL需要的角色
        if ("ROLE_LOGIN".equals(configAttribute.getAttribute()) && auth instanceof UsernamePasswordAuthenticationToken){
            return; // 无需权限的直接访问
        }
        for (GrantedAuthority authority : authorities) {
            if (configAttribute.getAttribute().equals(authority.getAuthority())){ // 找到匹配项
                return;
            }
        }
    }
    throw new AccessDeniedException("权限不足");
}

三 使用apifox测试

这里由于还没有集成jwt,测试的时候还是基于Cookie的。可以先用浏览器访问登陆界面获得到Cookie值,然后放到apifox中测试POST接口。

使用 user/123 登陆,获得Cookie。

在这里插入图片描述

由于user这个用户并没有新增用户的权限,所以POST的新增请求返回的是403。

在这里插入图片描述

如果使用 admin/123 登陆,则可以顺利新增成功。

如此,基于RBAC的标准restful请求已完成。


总结

  1. spring boot 2.7 以后废弃了WebSecurityConfigurerAdapter ,需要注意
  2. 为了实现标准restful请求的规范,我们在permission这边表加入了method这个字段用来判断

问题

  1. SpringBoot整合SpringSecurity出现Illegal overloaded getter method with ambiguous type for property enable
  2. 进入 SpringBoot2.7,有一个重要的类过期了

代码地址

代码


参考文档:
RBAC简介
如何使用postman测试springsecurity的代码

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

F_angT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值