当前位置:网站首页>ruoyi接口权限校验
ruoyi接口权限校验
2022-07-03 06:03:00 【xuhss_com】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
此文章属于ruoyi项目实战系列
ruoyi系统在前端主要通过权限字符包含与否来动态显示目录和按钮。为了防止通过http请求绕过权限限制,后端接口也需要进行相关权限设计。
@PreAuthorize使用
由于对@PreAuthorize原理还不够深入了解,所以此处只粗浅讲解在ruoyi项目是如何应用的。
在请求调用接口前,被@preAuthorize注解的接口需要首先通过验证。通过注解参数value()返回值true和false来判断是否有权限。
public @interface PreAuthorize {
String value();
}
Ruoyi并没有使用原生的Spel表达式,而是使用了自定义的PermissionService类,通过其中自定义方法hasPermi(String Permission) 来进行权限判断。注解使用举例:@PreAuthorize("@ss.hasPermi('system:menu:list')")
public boolean hasPermi(String permission)
{
if (StringUtils.isEmpty(permission))//用注解就必须有permission值
{
return false;
}
LoginUser loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNull(loginUser) ||
CollectionUtils.isEmpty(loginUser.getPermissions()))
{
return false;
}
return hasPermissions(loginUser.getPermissions(), permission);
private boolean hasPermissions(Set permissions, String permission)
{
return permissions.contains(ALL_PERMISSION) ||
permissions.contains(StringUtils.trim(permission)); //判断是否持有"所有权限”字符,或者持有该权限
}
接口权限校验流程
粗略用两个例子来讲解前端请求如何经过后端接口权限校验。
Login匿名请求

- Login请求路径是
/login,在过滤器链中被AnnoymousAuthenticationFilter添加匿名authentication到Spring上下文里。由于/login请求在SecurityConfig.java里设置成匿名请求,所以可以成功到达SysLoginController。 - 调用
SysLoginService.login方法,关键的一行命令:
Authentication authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
authenticationManager.authenticate()是钩子方法,在AbstractUserDetailsAuthenticationProvider中实现,会根据传入的token类型来自动选择,此处UsernamePasswordAuthenticationToken将由DaoAuthenticationProvider来处理(不清楚的话可以前后打两个断点看调用栈)。
3. 在DaoAuthenticationProvider中可以看到关键的一行:
UserDetails loadedUser = this.getUserDetailsService()
.loadUserByUsername(username);
这会调用我们自定义实现的UserDetailsServiceImpl#loadUserByUsername方法(如流程图所示),获得user信息。至于为什么会使用自定义方法,因为在SecurityConfig.java中进行了配置
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
- 生成token,然后返回。
已登录请求
已登录请求流程较简单,在流程图里的some filters里会通过自定义的JwtAuthenticationFilter,其中会通过token获得user信息,然后装入Spring的上下文,方便提取使用。
曾纠结踩坑的点
由于对SpringSecurity较陌生,虽然功能强大,但其复杂性也是大大提高,所以调试项目的同时翻看了很多入门博客文章,其中都不约而同的提到了UsernamePasswordAuthenticationFilter,可是我在实战项目中反复调试都没有看到这个过滤器的调用。
原因:Security配置文件需要添加httpSecurity.formLogin()启用表单登录才会使用该filter。查看项目使用的所有filter可以使用以下测试代码:
class RuoYiApplicationTest {
@Autowired
private FilterChainProxy filterChainProxy;
@Test
public void test() {
List filterChains = filterChainProxy.getFilterChains();
for(SecurityFilterChain sfc:filterChains){
for(Filter filter:sfc.getFilters()){
System.out.println(filter.getClass().getName());
}
}
}
}
边栏推荐
- Get a screenshot of a uiscrollview, including off screen parts
- Jedis source code analysis (II): jediscluster module source code analysis
- Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
- Mysql database binlog log enable record
- Exportation et importation de tables de bibliothèque avec binaires MySQL
- Oracle database synonym creation
- Loss function in pytorch multi classification
- Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
- Kubesphere - Multi tenant management
- Decision tree of machine learning
猜你喜欢

Pytorch builds the simplest version of neural network

项目总结--2(Jsoup的基本使用)

智牛股--03

智牛股项目--04

项目总结--01(接口的增删改查;多线程的使用)

The most responsible command line beautification tutorial

从小数据量 MySQL 迁移数据到 TiDB

Convolution operation in convolution neural network CNN

Kubernetes notes (III) controller

Project summary --2 (basic use of jsup)
随机推荐
Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide
Advanced technology management - do you know the whole picture of growth?
Mysql database
phpstudy设置项目可以由局域网的其他电脑可以访问
项目总结--01(接口的增删改查;多线程的使用)
SVN分支管理
深度学习,从一维特性输入到多维特征输入引发的思考
Understand the first prediction stage of yolov1
Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow
JDBC connection database steps
Interesting research on mouse pointer interaction
Oracle database synonym creation
Sorry, this user does not exist!
Simple handwritten ORM framework
Analysis of Clickhouse mergetree principle
代码管理工具
Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
In depth analysis of kubernetes controller runtime
Creating postgre enterprise database by ArcGIS
Disruptor learning notes: basic use, core concepts and principles