Mall 动态权限学习参考

代码仓库

https://github.com/Rain-with-me/JavaStudyCode/tree/main/4-springboot-security-dynic

  • 本文查看 Mall 的动态权限管理

动态权限的思路

路由拦截思路

  • 把需要权限拦截的路由放在数据库中,当请求发送过来进行判断,如果是白名单的直接通过,然后看他是否添加权限路由,如果有就进行比对,这个以后含有这个权限就可以访问。

Mall 动态权限学习参考

实现思路

可以先过一遍 Mall 的文档: https://www.macrozheng.com/mall/technology/permission_back.html#基于路径的动态权限控制

  • 使用一个拦截器进行鉴权
  • 一个用户的注册,就分配了用户权限的级别,然后有他包含的权限
  • 白名单跳过,没有在权限表的跳过
  • 首先获取访问的路径,这里是使用的 Ant 风格,然后和用户包含的权限进行匹配
  • 这里的实现首先把所有的权限名字放在一个 Map 里面,Mao 里面的加载使用 loadDataSource 当权限表进行更改,就刷新里面的值
    Mall 动态权限学习参考

Mall 动态权限学习参考

获取用户权限列表

Mall 动态权限学习参考
 @Override
    public UserDetails loadUserByUsername(String username) {
        //获取用户信息
        UmsAdmin admin = getAdminUsername(username);
        if (admin != null) {
            List resourceList = umsResourceService.getResourceList(admin.getId());
            return new AdminUserDetails(admin,resourceList);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    }

添加白名单

  • 建议使用多环境 (dev,test)
-是数组
secure:
  ignored:
    urls: #安全路径白名单
      - /swagger-ui.html
      - /swagger-resources/**
      - /swagger/**
      - /**/v2/api-docs
      - /**/*.js
      - /**/*.css
      - /**/*.png
      - /**/*.ico
      - /webjars/springfox-swagger-ui/**
      - /actuator/**
      - /druid/**
      - /admin/login
      - /admin/register
      - /admin/info
      - /admin/logout
      - /minio/upload
  • 获取白名单数组
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;

/**
 * SpringSecurity白名单资源路径配置
 * Created by macro on 2018/11/5.

 */
@Getter
@Setter
@ConfigurationProperties(prefix = "secure.ignored")
public class IgnoreUrlsConfig {

    private List urls = new ArrayList<>();

}

  • SecurityConfig 配置进去
   @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();

        // 不需要保护的资源路径允许访问
        for (String url : ignoreUrlsConfig().getUrls()) {
            registry.antMatchers(url).permitAll();
        }

        registry.antMatchers(HttpMethod.OPTIONS).permitAll();

        // 其他任何请求都需要身份认证
        registry.and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                // 关闭跨站请求防护及不使用session
                .and()
                .csrf()
                .disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                // 自定义权限拒绝处理类
                .and()
                .exceptionHandling()
                .accessDeniedHandler(restfulAccessDeniedHandler)
                .authenticationEntryPoint(restAuthenticationEntryPoint)
                // 自定义权限拦截器JWT过滤器
                .and()
                .addFilterBefore(jwtAuthenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
    }

       /**
     * @Description: 配置文件读取白名单
     * @Author: 雨同我
     * @DateTime: 2022/9/20 8:58
    */
    @Bean
    public IgnoreUrlsConfig ignoreUrlsConfig() {
        return new IgnoreUrlsConfig();
    }

Original: https://www.cnblogs.com/rain-me/p/16724964.html
Author: 雨同我
Title: Mall 动态权限学习参考

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611808/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球