【转载】spring-security-oauth2 (二十八) RBAC权限控制

 

一般的内部管理系统,权限比较复杂,通常权限都是动态配置的,也就是下面我们要讲的的rbac模型

RBAC

详细原理请度娘,这里只是涉及到企业权限的功能级权限,数据级权限还要我们自己控制(用户只能看见它相关的业务表数据)

Spring Security中支持我们自定义的实现,就像这样调用,下面我们来代码实现 

config.anyRequest().access("@rbacService.hasPermission(request,authentication)")

权限控制

新建一个authorize项目实现rbac权限,pom文件如下


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>tiger-auth </artifactId>
  7. <groupId>com.rui.tiger </groupId>
  8. <version>1.0-SNAPSHOT </version>
  9. </parent>
  10. <modelVersion>4.0.0 </modelVersion>
  11. <groupId>com.rui.tiger </groupId>
  12. <artifactId>tiger-auth-authorize </artifactId>
  13. <dependencies>
  14. <dependency>
  15. <groupId>javax.servlet </groupId>
  16. <artifactId>javax.servlet-api </artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.security </groupId>
  20. <artifactId>spring-security-core </artifactId>
  21. </dependency>
  22. </dependencies>
  23. </project>

定义rbac接口及实现


 
 
  1. package com.rui.tiger.auth.authorize.service;
  2. import org.springframework.security.core.Authentication;
  3. import javax.servlet.http.HttpServletRequest;
  4. /**
  5. * 用户权限判断接口
  6. * @author CaiRui
  7. * @date 2019-05-07 08:02
  8. */
  9. public interface RbacService {
  10. boolean hasPermission(HttpServletRequest request, Authentication authentication);
  11. }

 
 
  1. package com.rui.tiger.auth.authorize.service;
  2. import org.springframework.security.core.Authentication;
  3. import org.springframework.security.core.userdetails.UserDetails;
  4. import org.springframework.stereotype.Component;
  5. import org.springframework.util.AntPathMatcher;
  6. import javax.servlet.http.HttpServletRequest;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11. /**
  12. * @author CaiRui
  13. * @date 2019-05-07 08:05
  14. */
  15. @Component( "rbacService")
  16. public class RbacServiceImpl implements RbacService {
  17. private AntPathMatcher antPathMatcher= new AntPathMatcher();
  18. @Override
  19. public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
  20. Object principal = authentication.getPrincipal();
  21. boolean hasPermission = false;
  22. if (principal instanceof UserDetails) {
  23. String username = ((UserDetails) principal).getUsername();
  24. //读取用户所拥有权限的所有URL 这个应该根据用户名从数据库中查询 此处只是模拟
  25. List<String> urls = new ArrayList<>();
  26. for (String url : urls) {
  27. //匹配 /user/* 这种格式
  28. if (antPathMatcher.match(url, request.getRequestURI())) {
  29. hasPermission = true;
  30. break;
  31. }
  32. }
  33. }
  34. return hasPermission;
  35. }
  36. }

demo项目中引入此依赖,并配置


 
 
  1. package com.rui.tiger.auth.demo.security;
  2. import com.rui.tiger.auth.core.authorize.AuthorizeConfigProvider;
  3. import org.springframework.core.annotation.Order;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * 业务模块权限配置实现
  9. * @author CaiRui
  10. * @date 2019-05-06 12:16
  11. */
  12. @Component
  13. @Order(Integer.MAX_VALUE)
  14. public class DemoAuthorizeConfigProvider implements AuthorizeConfigProvider {
  15. @Override
  16. public boolean config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
  17. config.antMatchers(
  18. "/user/regist", // 注册请求
  19. "/error",
  20. "/connect/*",
  21. "/auth/*",
  22. "/signin",
  23. "/social/signUp", // app注册跳转服务
  24. "/swagger-ui.html",
  25. "/swagger-ui.html/**",
  26. "/webjars/**",
  27. "/swagger-resources/**",
  28. "/v2/**"
  29. ).permitAll();
  30. /*
  31. 利用@Order注解实现CommonAuthorizeConfigProvider中的放行路径不需要自定义权限验证
  32. */
  33. config.anyRequest()
  34. .access( "@rbacService.hasPermission(request,authentication)");
  35. return true;
  36. }
  37. }

 

文章转载至:https://blog.csdn.net/ahcr1026212/article/details/89913728

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值