shiro系列三、定义Realm
自定义realm;
/**
* 认证和授权
*/
@Component
public class UserRealm extends AuthorizingRealm { @Autowired
private UserService userService;
@Autowired
private AuthService authService; /**
* 授权(验证权限时调用)
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
User user = (User)principals.getPrimaryPrincipal();
Integer userId = user.getUserId();
List<String> permsList = null; //系统管理员,拥有最高权限
if(userId == ){
List<Auth> menuList = authService.queryList(new HashMap<String, Object>());
permsList = new ArrayList<String>(menuList.size());
for(Auth menu : menuList){
permsList.add(menu.getPerms());
}
}else{
permsList = userService.queryAllPerms(userId);
}
//用户权限列表
Set<String> permsSet = new HashSet<String>();
for(String perms : permsList){
if(StringUtils.isBlank(perms)){
continue;
}
permsSet.addAll(Arrays.asList(perms.trim().split(",")));
} SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.setStringPermissions(permsSet);
return info;
} /**
* 认证(登录时调用)
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken)authcToken; //查询用户信息
User user = userService.queryByUserName(token.getUsername());
//账号不存在
if(user == null) {
throw new UnknownAccountException("账号不存在");
}
SimpleAuthenticationInfo info =null;
info = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()),getName());
return info;
} @Override
public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
HashedCredentialsMatcher shaCredentialsMatcher = new CustomUserMatche();
shaCredentialsMatcher.setHashAlgorithmName(ShiroUtils.hashAlgorithmName);
shaCredentialsMatcher.setHashIterations(ShiroUtils.hashIterations);
super.setCredentialsMatcher(shaCredentialsMatcher);
}
}
1、UserRealm父类AuthorizingRealm将获取Subject相关信息分成两步:获取身份验证信息(doGetAuthenticationInfo)及授权信息(doGetAuthorizationInfo);
2、doGetAuthenticationInfo获取身份验证相关信息:首先根据传入的用户名获取User信息;然后如果user为空,那么抛出没找到帐号异常UnknownAccountException;如果user找到但锁定了抛出锁定异常LockedAccountException;最后生成AuthenticationInfo信息,交给间接父类AuthenticatingRealm使用CredentialsMatcher进行判断密码是否匹配,如果不匹配将抛出密码错误异常IncorrectCredentialsException;另外如果密码重试此处太多将抛出超出重试次数异常ExcessiveAttemptsException;在组装SimpleAuthenticationInfo信息时,需要传入:身份信息(用户名)、凭据(密文密码)、盐(username+salt),CredentialsMatcher使用盐加密传入的明文密码和此处的密文密码进行匹配。
3、doGetAuthorizationInfo获取授权信息:PrincipalCollection是一个身份集合,因为我们现在就一个Realm,所以直接调用getPrimaryPrincipal得到之前传入的用户名即可;然后根据用户名调用UserService接口获取角色及权限信息。
shiro系列三、定义Realm的更多相关文章
- Apache Shiro系列三,概述 —— 10分钟入门
一.介绍 看完这个10分钟入门之后,你就知道如何在你的应用程序中引入和使用Shiro.以后你再在自己的应用程序中使用Shiro,也应该可以在10分钟内搞定. 二.概述 关于Shiro的废话就不多说了 ...
- shiro系列三、ssm框架整合shiro实现权限控制
shiro权限框架是一个非常优秀的框架,前面的几篇文章对shiro进行了非常详细的介绍和原理分析,那么接下来让我们开始在web项目中使用它(javase也能用shiro): 一.数据库表结构设计 二. ...
- Apache Shiro系列之五,概述 —— 配置
Shiro设计的初衷就是可以运行于任何环境:无论是简单的命令行应用程序还是复杂的企业集群应用.由于运行环境的多样性,所以有多种配置机制可用于配置,本节我们将介绍Shiro内核支持的这几种配置机制. ...
- Apache Shiro系列四,概述 —— Shiro的架构
Shiro的设计目标就是让应用程序的安全管理更简单.更直观. 软件系统一般是基于用户故事来做设计.也就是我们会基于一个客户如何与这个软件系统交互来设计用户界面和服务接口.比如,你可能会说:“如 ...
- shiro实战系列(七)之Realm
Realm 是一个能够访问应用程序特定的安全数据(如用户.角色及权限)的组件.Realm 将应用程序特定的数据转 换成一种 Shiro 能够理解的格式,这样 Shiro 能够提供一个单一的易理解的 S ...
- WCF编程系列(三)地址与绑定
WCF编程系列(三)地址与绑定 地址 地址指定了接收消息的位置,WCF中地址以统一资源标识符(URI)的形式指定.URI由通讯协议和位置路径两部分组成,如示例一中的: http://loc ...
- Shiro 系列 - 基本知识
和 Spring Security 项目一样, Apache Shiro 也是一个被广泛使用安全框架, 它们都能完成认证.授权.会话管理等. 简单对比一下 Apache Shiro 和 Spring ...
- 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gulp专家
系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...
- SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型
原文:SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server ...
随机推荐
- tomcat 8.xxx ssl使用websocket Failed to close the ServletOutputStream connection cleanly
报错: 2019-09-17 10:30:17.277 default [https-jsse-nio-8110-exec-20] INFO o.a.t.w.s.WsRemoteEndpointImp ...
- xdebug调试的原理
转自 https://segmentfault.com/a/1190000002528341 使用PhpStorm+xdebug单步调试程序可以快速帮助自己熟悉项目代码! 运行原理 在实际使用前,我们 ...
- 使用PostMan测试WebService接口
使用PostMan测试WebService接口 参考资料: 通过XML请求WebServer https://blog.csdn.net/qq_33933408/article/details/53 ...
- List的add方法与addAll方法的区别、StringBuffer的delete方法与deleteCharAt的区别
List的add方法与addAll方法 区别 add add是将传入的参数作为当前List中的一个Item存储,即使你传入一个List也只会另当前的List增加1个元素 addAll addAll是传 ...
- 01.04 linux命令(2
======================Linux下的用户管理==============用户信息保存/etc/passwd ,一般用户都有读的权限真正的用户:修改密码,可以登录伪用户:应用程序在 ...
- 【计算机视觉】深度相机(五)--Kinect v2.0
原文:http://blog.csdn.NET/qq1175421841/article/details/50412994 ----微软Build2012大会:Kinect for Windows P ...
- IO-file 01 名称或路径
package com.bwie.io; import java.io.File; /** * 名称或路径 * [getName:名称 * getPath * getAbsolutePath:绝对路径 ...
- Spring mybatis源码篇章-Mybatis主文件加载
通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-SqlSessionFactory 前话 本文承接前文的内容继续往下扩展,通过Spring与Mybatis的 ...
- 记一次INFA抽取数据报 WRT_8165 - TIMEOUT_BASED_COMMIT_POINT错误
今天抽取数据报1千多W的数据报错 powercenter workflow卡死不动,也没报错就显示 timeout based commit point WRT_8165 - TIMEOUT_BAS ...
- Elasticsearch使用小结之冷热分离
Elasticsearch使用小结之冷热分离 索引迁移 索引setting中的index.routing.allocation.exclude和index.routing.allocation.inc ...