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 ...
随机推荐
- sql server开发工具
查询分析器的使用 SQL语言包含四个部分: 1.数据定义语言(DDL) : 例如 create, drop, alter等语句 2.数据操作语言(DML) : 例如 insert,delete, up ...
- vue的一些小坑
1.$refs使用时机 尝试在watch的时候使用$refs,发现里面都是空的,然后google了一下,$refs需要在整个组件挂载完成后才能使用 解决方法:使用setTimeout setTImeo ...
- linux shell变量的截取
变量的截断,经常用到的是${},##和%%几个特殊符号.假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt ,可以用${ }分别替换得到不同的值: ${file ...
- hadoop mapreduce 基础实例一记词
mapreduce实现一个简单的单词计数的功能. 一,准备工作:eclipse 安装hadoop 插件: 下载相关版本的hadoop-eclipse-plugin-2.2.0.jar到eclipse/ ...
- 面向对象【day07】:面向对象使用场景(十)
本节内容 1.概述 2.知识回顾 3.使用场景 一.概述 之前我们学了面向对象知识,那我们在什么时候用呢?不可能什么时候都需要用面向对象吧,除非你是纯的面向对象语言,好的,我们下面就来谈谈 二.知识回 ...
- No Spring WebApplicationInitializer types detected on classpath
情况: 1.使用 Tomcat 8 Maven 插件 2.使用 ServletContainerInitializer 模式配置 Spring 配置好后发现只会加载 spring-web 配置的 We ...
- Enum入门【原】
package com.bobo.www.cxf.impl; public enum Traffic { Red(1), Green(2), Yellow(3);//必须最前面 private int ...
- js中html拼接
https://i.cnblogs.com/EditPosts.aspx?postid=10620765&update=1
- npm与nrm
npm npm是Node.js 平台的默认包(模块依赖)管理工具 Node Package Manager nrm 一个npm的源管理器(管理工具) 允许快速的在 npm 源间切换 两者关系 npm是 ...
- None.js 第一步 开启一个服务 hello world
引入 http 模块 var http = require('http'); 创建服务器 http.createServer(function (request, response) { // 发送一 ...