当前位置:网站首页>Ruoyi interface permission verification
Ruoyi interface permission verification
2022-07-03 06:29:00 【xuhss_ com】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
This article belongs to ruoyi Project practice series
ruoyi In the front end, the system dynamically displays directories and buttons mainly through whether permission characters are included or not . To prevent passage http Request to bypass permission restrictions , The back-end interface also needs to be designed with relevant permissions .
@PreAuthorize Use
Due to @PreAuthorize The principle is not well understood , So here is only a brief explanation in ruoyi How the project is applied .
Before requesting to call the interface , By @preAuthorize The annotated interface needs to be validated first . Via annotation parameters value() Return value true and false To determine whether you have permission .
public @interface PreAuthorize {
String value();
}
Ruoyi Not using native Spel expression , It's using custom PermissionService class , Through which custom methods hasPermi(String Permission) To judge the authority . Use examples of annotations :@PreAuthorize("@ss.hasPermi('system:menu:list')")
public boolean hasPermi(String permission)
{
if (StringUtils.isEmpty(permission))// With annotations, there must be permission value
{
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)); // Determine whether to hold " All permissions ” character , Or hold the permission
}
Interface permission verification process
Use two examples to explain how the front-end request passes through the back-end interface permission verification .
Login Anonymous request

- Login The request path is
/login, In the filter chainAnnoymousAuthenticationFilterAdd anonymousauthenticationTo Spring In the context . because/loginRequest inSecurityConfig.javaSet anonymous request in , So we can successfully reachSysLoginController. - call
SysLoginService.loginMethod , A key line of command :
Authentication authentication = authenticationManager
.authenticate(new UsernamePasswordAuthenticationToken(username, password));
authenticationManager.authenticate() It's the hook method , stay AbstractUserDetailsAuthenticationProvider To realize , According to the incoming token Type to automatically select , here UsernamePasswordAuthenticationToken Will be made by DaoAuthenticationProvider To deal with it ( If you are not clear, you can set two breakpoints before and after to see the call stack ).
3. stay DaoAuthenticationProvider You can see the key line in :
UserDetails loadedUser = this.getUserDetailsService()
.loadUserByUsername(username);
This will invoke our custom implementation UserDetailsServiceImpl#loadUserByUsername Method ( As shown in the flow chart ), get user Information . As for why you use custom methods , Because in SecurityConfig.java Configured in
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
- Generate token, Then return .
Logged in request
The logged in request process is simple , In the flow chart some filters It will be customized JwtAuthenticationFilter, It will pass token get user Information , And then load Spring The context of , Easy to extract and use .
Once tangled with the point of stepping on the pit
Due to SpringSecurity Relatively unfamiliar , Although powerful , But its complexity is also greatly improved , So while debugging the project, I read many introductory blog posts , All of them mentioned UsernamePasswordAuthenticationFilter, However, I did not see the call of this filter during repeated debugging in the actual project .
reason :Security Configuration files need to be added httpSecurity.formLogin() Enable form login to use this filter. View all... Used by the project filter You can use the following test code :
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());
}
}
}
}
边栏推荐
- Request weather interface format, automation
- Pdf files can only print out the first page
- 使用 Abp.Zero 搭建第三方登录模块(一):原理篇
- 【C#/VB.NET】 将PDF转为SVG/Image, SVG/Image转PDF
- Shell conditional statement
- 简易密码锁
- Merge and migrate data from small data volume, sub database and sub table Mysql to tidb
- When PHP uses env to obtain file parameters, it gets strings
- MySQL帶二進制的庫錶導出導入
- What's the difference between using the Service Worker Cache API and regular browser cache?
猜你喜欢

Project summary --04

Svn branch management

Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"

2022 CISP-PTE(三)命令执行

Mysql

Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide

Merge and migrate data from small data volume, sub database and sub table Mysql to tidb

100000 bonus is divided up. Come and meet the "sister who braves the wind and waves" among the winners

Oauth2.0 - use database to store client information and authorization code

Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
随机推荐
Condition annotation in uni-app realizes cross segment compatibility, navigation jump and parameter transfer, component creation and use, and life cycle function
【5G NR】UE注册流程
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
PMP notes
Openresty best practices
Various usages of MySQL backup database to create table select and how many days are left
10万奖金被瓜分,快来认识这位上榜者里的“乘风破浪的姐姐”
About the difference between count (1), count (*), and count (column name)
[system design] proximity service
代码管理工具
MySQL带二进制的库表导出导入
JMeter performance automation test
Redis cluster creation, capacity expansion and capacity reduction
Floating menu operation
Mysql database binlog log enable record
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
Paper notes vsalm literature review "a comprehensive survey of visual slam algorithms"
Cesium 点击获三维坐标(经纬度高程)
Exportation et importation de tables de bibliothèque avec binaires MySQL
MySQL帶二進制的庫錶導出導入