SpringSecurity(二)——授权实现

server/2024/10/19 0:22:57/

一、授权基本思路

在SpringSecurity中,会使用默认的FilterSecurityInterceptor来进行权限校验。在 FilterSecurityInterceptor中会从SecurityContextHolder获取其中的Authentication,然后获取其中的 权限信息。当前用户是否拥有访问当前资源所需的权限。

所以我们在项目中只需要把当前登录用户的权限信息也存入Authentication。然后设置我们的资源所需 要的权限即可

二、实现过程

(1)开启相关配置

java">@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig{.....
}

然后就可以使用对应的注解。@PreAuthorize在各接口

java">@RestController
public class HelloController {@RequestMapping("/hello")@PreAuthorize("hasAuthority('test')")public String hello(){return "hello";}}

(2)自定义LoginUser,封装权限信息

我们之前定义了UserDetails的实现类LoginUser,想要让其能封装权限信息就要对其进行修改。

java">@Data
@NoArgsConstructor
public class LoginUser implements UserDetails{private User user;//查询到的权限列表private List<String> list;public LoginUser(User user, List<String> list) {this.list = list;this.user = user;}//自定义一个权限列表的集合  中转操作@JSONField(serialize = false)List<SimpleGrantedAuthority> authorities;//返回权限@Overridepublic Collection<? extends GrantedAuthority> getAuthorities() {if (authorities != null) {return authorities;}authorities = new ArrayList<>();for (String item : list) {SimpleGrantedAuthority authority = new SimpleGrantedAuthority(item);authorities.add(authority);}return authorities;}//获取密码@Overridepublic String getPassword() {return user.getPassword();}//获取用户名@Overridepublic String getUsername() {return user.getUserName();}//判断账号是否未过期@Overridepublic boolean isAccountNonExpired() {return true;}//判断账号是否没有锁定@Overridepublic boolean isAccountNonLocked() {return true;}//判断账号是否没有超时@Overridepublic boolean isCredentialsNonExpired() {return true;}//判断账号是否可用@Overridepublic boolean isEnabled() {return true;}}

(3)从数据库查询权限信息

RBAC模型

我们可以在UserDetailsServiceImpl中去调用该mapper的方法查询权限信息封装到LoginUser对象 中即可。

java">@Service
public class UserDetailServiceImpl implements UserDetailsService {@Autowiredprivate UserMapper userMapper;@Autowiredprivate MenuMapper menuMapper;@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//1.查询用户信息QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.eq("user_name", username);User user = userMapper.selectOne(queryWrapper);//如果没有查询到用户,就抛出异常if (Objects.isNull(user)) {throw new RuntimeException("用户名或密码错误");}//2.查询用户对应的权限信息
//        List<String> list = new ArrayList<>();
//        list.add("select");
//        list.add("delete");List<String> list =  menuMapper.selectPermsByUserId(user.getId());//3.返回UserDetails对象return new LoginUser(user, list);}
}

http://www.ppmy.cn/server/131983.html

相关文章

P1439 【模板】最长公共子序列 Python 题解

【模板】最长公共子序列 题目描述 给出 1 , 2 , … , n 1,2,\ldots,n 1,2,…,n 的两个排列 P 1 P_1 P1​ 和 P 2 P_2 P2​ &#xff0c;求它们的最长公共子序列。 输入格式 第一行是一个数 n n n。 接下来两行&#xff0c;每行为 n n n 个数&#xff0c;为自然数 1 …

【C语言刷力扣】1748.唯一元素的和

题目&#xff1a; 法一 解题思路&#xff1a; 由于 nums.length 小于100&#xff0c;新建数组 num[101]&#xff0c;用来遍历存放 nums[i]出现的次数。 int sumOfUnique(int* nums, int numsSize) {int result 0;int num[101] {0}; // memset(num, 0, sizof(num));for (int…

RK3568笔记六十五:LIVE555交叉编译测试

若该文为原创文章,转载请注明原文出处。 在开发项目时有用到LIVE555,使用是其他芯片,功能是LIVE555拉流,通过LVGL显示摄像头数据。 这里记录如何交叉编译,测试一下,为后续增加拉流和推流准备,使用zlmedia也可以,但live555用的比较多。 想实现的是: 一、使用LIVE555…

小猿口算安卓端安装包PK一题秒过关。。。

大家好&#xff0c;我是小黄。 近段时间&#xff0c;越来越多的同学都想去小猿口算里面虐小学生&#xff0c;但是发现越来越多的计算机学生带着科技与他们进行对抗&#xff0c;这样非计算机专业的大学生们​苦不堪言。 现在&#xff0c;非计算机大学生们翻身的机会来了&#…

v-model双向绑定组件通信

给input元素绑定原生input事件&#xff0c;触发input事件时&#xff0c;进而触发update:model-value事件 <template> <Child v-model"lastName" v-if"true"></Child> <p >{{lastName}}</p> </template> <script&g…

如何理解应用 Java 多线程与并发编程?

如何理解应用 Java 多线程与并发编程&#xff1f; 在日常开发中&#xff0c;随着硬件性能的提升&#xff0c;尤其是多核处理器的普及&#xff0c;如何让应用程序更好地利用这些资源&#xff0c;成为每个程序员需要考虑的问题。这时候&#xff0c;多线程与并发编程就显得尤为重…

windows自动化(一)---windows关闭熄屏和屏保

电脑设置关闭屏幕和休眠时间不起作用解决方案 一共三个方面注意&#xff1a; 一、关闭屏保设置&#xff1a; 二、电源管理设置 三、关闭盖子不做操作&#xff1a; 第一点很重要&#xff0c;就算二三都做了&#xff0c;一没做&#xff0c;照样不行。

OKHTTP 如何处理请求超时和重连机制

&#x1f604;作者简介&#xff1a; 小曾同学.com,一个致力于测试开发的博主⛽️&#xff0c;主要职责&#xff1a;测试开发、CI/CD 如果文章知识点有错误的地方&#xff0c;还请大家指正&#xff0c;让我们一起学习&#xff0c;一起进步。 &#x1f60a; 座右铭&#xff1a;不…