SpringSecurity基于数据库RBAC数据模型控制权限
⒈通用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数据模型控制权限的更多相关文章
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
1.前言 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelp ...
- springsecurity基于数据库验证用户
之前的springsecurity程序都是将数据存放在内存中的,通过 <security:user-service> <security:user name="user&q ...
- SpringBoot2.0整合mybatis、shiro、redis实现基于数据库权限管理系统
转自https://blog.csdn.net/poorcoder_/article/details/71374002 本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管 ...
- 简单的RBAC用户角色权限控制
Java web项目中,无论项目是大是小,或多或少都会涉及到用户访问权限的控制,权限管理总体的设计思路就是,不该看的不看,不该做的不做!据我目前的了解,我所知道的几种实现访问权限控制的方式有: JQu ...
- SpringSecurity——基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则
本文转自:https://www.cnblogs.com/weilu2/p/springsecurity_custom_decision_metadata.html 本文在SpringMVC和MyBa ...
- 利用基于@AspectJ的AOP实现权限控制
一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...
- 基于Vue实现后台系统权限控制
原文地址:http://refined-x.com/2017/08/29/基于Vue实现后台系统权限控制/,转载请注明出处. 用Vue/React这类双向绑定框架做后台系统再适合不过,后台系统相比普通 ...
- Spring Security的RBAC数据模型嵌入
1.简介 基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成 ...
- 扩展RBAC用户角色权限设计方案(转载)
扩展RBAC用户角色权限设计方案 来源:https://www.cnblogs.com/zwq194/archive/2011/03/07/1974821.html https://blog.csd ...
随机推荐
- 深入理解JS函数中this指针的指向
函数在执行时,会在函数体内部自动生成一个this指针.谁直接调用产生这个this指针的函数,this就指向谁. 怎么理解指向呢,我认为指向就是等于.例如直接在js中输入下面的等式: console.l ...
- 8个Python小Tips
原创: 金牌小编 行走在Python的江湖,那能不收藏一些锦囊妙计,今天分享给大家8个小技巧,希望在实战的时候有帮助! 01. 条件判断 condition=Trueif condition: ...
- C++学习之回调函数
回调函数: 通过函数指针进行调用的函数. 回调函数不是由实现方进行调用,而是将函数指针传入,在特殊条件或者状态下进行触发调用. 譬如: 在自定义按钮控件时,当点击按钮,触发点击事件,调用指定函数. 注 ...
- 互联网运营+SEO:推荐必看的5本书籍
本文首发于:风云社区(scoee.com) 最近开始学习和研究互联网运营和SEO,对于我这个小白来讲,还是有些吃力,毕竟从来没接触这方面的,尽管在之前的软件公司做过售前和产品相关的工作,但毕竟与互联网 ...
- 01--STL泛型编程了解
开始学习侯捷老师的课程了~~ 一:六大组件关系 容器(Container) 算法(Algorithm) 迭代器(Iterator) 仿函数(Function object) 适配器(Adaptor) ...
- CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)
https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...
- vue2.0 子组件 父组件之间的传值
常用的子组件给父组件传值/调父组件方法 //子组件let val = "";//可以是任意类型this.$emit('fatherFun', val); //父组件<fath ...
- SQL Server进阶 遍历表的几种方法
https://www.cnblogs.com/mcgrady/p/4182486.html
- 30. Spring Boot ActiveMQ
1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- 12.scrapy框架
一.Scrapy 框架简介 1.简介 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个 ...