当前位置:   article > 正文

Springsecurity配置 & RBAC模型

Springsecurity配置 & RBAC模型

Spring Security配置 & RBAC模型

Spring Security作为安全框架的核心功能:

认证:验证当前访问系统的是不是本系统的用户,并且要确认具体是哪个用户

授权:经过认证后判断当前用户是否有权限进行某个操作


Spring Security的配置

1.properties包下的ConfigPropertites类。

用来将配置文件中的属性映射到实体上

@ConfigurationProperties(prefix = "buxiwan")
@Component
@Data
public class ConfigProperties {
    private String[] nonFilterPath; //定义了不需要经过过滤器的路径。

    private String[] jwtFilterPath; //定义了需要经过JWT过滤器的路径。

    private String[] staticSource; //定义了静态资源路径。
    //定义了访问令牌和刷新令牌的过期时间。
    private long accessTokenExpiration;
    private long refreshTokenExpiration;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

使用该类结合SecurityConfig,可以快速设置需要鉴权的接口。

2.config.permission.expression包下RbacExpressionRoot

基于角色的自定义鉴权 鉴权时调用

@Component("rbacExpressionRoot")
public class RbacExpressionRoot {
    public boolean hasAuthority(String authority) {
        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new BizException(ErrorCode.NO_SIGN_IN);
        }
        Object loginUser = authentication.getPrincipal();
        if (loginUser instanceof UserPermission) {
            return ((UserPermission) loginUser).getAuthorities().contains(authority);
        } else {
            return false;
        }
    }

    public boolean hasRole(String role) {
        //获取当前用户角色
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            throw new BizException(ErrorCode.NO_SIGN_IN);
        }
        Object loginUser = authentication.getPrincipal();
        if (loginUser instanceof UserPermission) {
            return ((UserPermission) loginUser).getRoles().contains(role);
        } else {
            return false;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
3.config包下的SecurityConfig

Spring Security配置

@Configuration
//启用全局方法安全性,其中prePostEnabled = true表示启用@PreAuthorize和@PostAuthorize注解。
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter; //JwtAuthenticationTokenFilter放在所有过滤器前面,检查浏览器携带的token是否合法

    @Autowired
    AccessDeniedHandler accessDeniedHandler;

    @Autowired
    AuthenticationEntryPointImpl authenticationEntryPoint; //用户未登录时访问需要jwt接口

    @Autowired
    ConfigProperties configProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //允许跨域
                .cors().and()
                //关闭csrf  前后端分离项目不必担心csrf攻击
                .csrf().disable()
                //不通过Session获取SecurityContext。  设置为无状态的Session管理,因为项目采用JWT,不需要HttpSession。
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests() //开始定义请求权限。
                // 允许匿名访问的接口(只允许未登录时访问)
                .antMatchers(configProperties.getNonFilterPath()).anonymous()
                //静态资源(swagger)放行
                .antMatchers(configProperties.getStaticSource()).permitAll()
                //基于配置为某个接口设置权限。使用到之前的configProperties,rbacExpressionRoot。
                //对于指定路径 configProperties.getJwtFilterPath(),只有具有 'user' 角色的用户才能访问。
                .antMatchers(configProperties.getJwtFilterPath()).access("@rbacExpressionRoot.hasRole('user')")
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();


        //添加过滤器。在默认的UsernamePasswordAuthenticationFilter之前添加自定义的JWT过滤器。
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //配置自定义异常处理器。设置自定义的认证入口点和访问被拒绝的处理器。
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

    }

    //暴露AuthenticationManagerBean
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //自定义密码encoder
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

RBAC模型

1. RBAC是什么

RBAC(Role-Based Access Control),基于角色的访问控制。通过用户关联角色,角色关联权限,来间接的为用户赋予权限。
在这里插入图片描述

2.分类

RBAC0 模型

最简单的用户、角色、权限模型。包含两种:

  1. 用户和角色是多对一关系。
  2. 用户和角色是多对多关系。

适合:系统功能单一,使用人员较少,岗位权限清晰,不会出现兼岗。


RBAC1 模型

RBAC0加上 角色继承

子角色可以继承父角色的所有权限。

适合:有权限继承关系。如经理、主管、专员。主管的权限不能大于经理,专员的权限不能大于主管。


RBAC2 模型

RBAC0加上 对角色的一些限制:角色互斥、基数约束、先决条件角色、运行时互斥

角色互斥:同一用户不能分配到一组互斥角色(权限互相制约)集合中的多个角色。
基数约束:一个角色被分配的用户数量有限制。
先决条件角色:要想获得较高的权限,首先拥有低一级的权限。
运行时互斥:允许一个用户具有两个角色,但运行中不可同时激活这两个角色。


RBAC3 模型

称为统一模型,综合了 RBAC0RBAC1RBAC2 的所有特点。

用户组

“用户组”,就是将相同属性的用户归类到一起。

数量有限制。

先决条件角色:要想获得较高的权限,首先拥有低一级的权限。
运行时互斥:允许一个用户具有两个角色,但运行中不可同时激活这两个角色。


RBAC3 模型

称为统一模型,综合了 RBAC0RBAC1RBAC2 的所有特点。

用户组

“用户组”,就是将相同属性的用户归类到一起。

例如公司有多个部门,每个部门中的用户权限相同。这样分配权限就能以部门为单位,方便管理,减少工作量。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/459932
推荐阅读
相关标签
  

闽ICP备14008679号