⒈通用RBAC(Role - Based Access Control)数据模型

⒉如何使用

  1.

  

 package cn.coreqi.ssoserver.rbac;

 import org.springframework.security.core.Authentication;

 import javax.servlet.http.HttpServletRequest;

 public interface RbacService {

     /**
*
* @param request 当前请求的信息
* @param authentication 当前用户的信息
* @return 是否拥有访问权限
*/
boolean hasPermission(HttpServletRequest request, Authentication authentication);
}

  2.

 package cn.coreqi.ssoserver.rbac.impl;

 import cn.coreqi.ssoserver.rbac.RbacService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher; import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set; @Component("rbacService")
public class RbacServiceImpl implements RbacService { private AntPathMatcher antPathMatcher = new AntPathMatcher(); /**
*
* @param request 当前请求的信息
* @param authentication 当前用户的信息
* @return 是否拥有访问权限
*/
@Override
public boolean hasPermission(HttpServletRequest request, Authentication authentication) {
Object principal = authentication.getPrincipal();
boolean hasPermission = false;
if(principal instanceof UserDetails){
String username = ((UserDetails)principal).getUsername();
//在数据库中读取用户所拥有权限的所有URL
//在这里使用Set模拟
Set<String> urls = new HashSet<>();
for (String url : urls){
if(antPathMatcher.match(url,request.getRequestURI())){
hasPermission = true;
break;
}
}
}
return hasPermission;
}
}

  3.写一个权限表达式,让SpringSecurity调用我们的方法

 @EnableWebSecurity
public class SsoWebSecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.and()
.authorizeRequests()
.anyRequest().access("@rbacService.hasPermission(request, authentication)") //为了避免该配置被覆盖,必要时需要使用@Order注解设置优先级。
.and()
.csrf().disable(); //禁用CSRF
} }

SpringSecurity基于数据库RBAC数据模型控制权限的更多相关文章

  1. SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例

    1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...

  2. springsecurity基于数据库验证用户

    之前的springsecurity程序都是将数据存放在内存中的,通过 <security:user-service> <security:user name="user&q ...

  3. SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统

    转自https://blog.csdn.net/poorcoder_/article/details/71374002 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管 ...

  4. 简单的RBAC用户角色权限控制

    Java web项目中,无论项目是大是小,或多或少都会涉及到用户访问权限的控制,权限管理总体的设计思路就是,不该看的不看,不该做的不做!据我目前的了解,我所知道的几种实现访问权限控制的方式有: JQu ...

  5. SpringSecurity——基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则

    本文转自:https://www.cnblogs.com/weilu2/p/springsecurity_custom_decision_metadata.html 本文在SpringMVC和MyBa ...

  6. 利用基于@AspectJ的AOP实现权限控制

    一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...

  7. 基于Vue实现后台系统权限控制

    原文地址:http://refined-x.com/2017/08/29/基于Vue实现后台系统权限控制/,转载请注明出处. 用Vue/React这类双向绑定框架做后台系统再适合不过,后台系统相比普通 ...

  8. Spring Security的RBAC数据模型嵌入

    1.简介 ​ 基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成 ...

  9. 扩展RBAC用户角色权限设计方案(转载)

    扩展RBAC用户角色权限设计方案  来源:https://www.cnblogs.com/zwq194/archive/2011/03/07/1974821.html https://blog.csd ...

随机推荐

  1. python自动化开发-[第十三天]-javascript

    今日概要 1.javascript简单语法 1.javascript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名S ...

  2. fuel6.0安装部署

    在经过一系列安装openstack方式后,个人觉得fuel的安装方式相对简易,接下来记录下安装部署fuel6.0的过程.本教程适合想把fuel6.0部署后,云主机需要连接外网的需求. 安装virtua ...

  3. javaweb简单登陆例子

    JSP+Servlet+JavaBean简单程序例子——用户名密码登陆,摘自<Tomcat&JavaWeb 技术手册>,亲测可用. IDE环境:MyEclipse10 1.建立We ...

  4. springmvc上传图片《2》

    创建springboot项目 编写配置 server: port: 8082 spring: application: name: upload-service servlet: multipart: ...

  5. HDU 1031(服装打分 **)

    题意是有 n 个人要对 m 件服装打分,按总分从高到低排序,再将总分排在前 k 名的服装按编号的从高到低输出,结构体排序. 代码如下: #include <bits/stdc++.h> u ...

  6. HDU 6375(双端队列 ~)

    题意是有至多150000个双端队列,400000次简单操作,直接开会导致内存超限,所以用 STL 中的 map 和 deque ,而读入过大已经在题目中有所说明,直接用已经给出的快速读入即可.要注意的 ...

  7. Openresty 学习笔记(二)Nginx Lua 正则表达式相关API

    ngx.re.match 语法: captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?) 环境: init_w ...

  8. python3 bytes数据类型探讨

    python3中str和bytes分开了,那么bytes与str之间到底是什么关系呢?下面从表现形式.处理方式.存储形式三个方面来阐述其区别 1. 在字符串前面加上b,就表示bytes数据类型 s1 ...

  9. maven更新项目版本

    mvn versions:set -DnewVersion=1.0.1-SNAPSHOT // 此操作会生成pom备份文件,推荐使用 mvn versions:set -DnewVersion=1.1 ...

  10. mosh

    mosh 是一款使用 UDP 连接 C/S 的终端工具, 服务器只需安装好 mosh 套件, 并启动 SSH 服务, 等待 Client 连接即可. Client (mosh-client) 连接时, ...