shiro知识汇总
来源:互联网 发布:淘宝转换微信链接 编辑:程序博客网 时间:2024/05/22 04:36
一、身份认证
1、身份认证流程:
1)、调用subject.login( token ),自动委托给Securityanager,调用需通过SecurityUtils.setSecurityManager()绑定SercurityManager(全局设置执行一次即可);
2)、SecurityManager负责真正的身份认证逻辑,会委托给Authenticator进行身份认证;
3)、Authentitor为Shiro API核心身份认证入口(此处可以插入自定义实现);
4)、Authentitor委托给AuthenticationStrategy(认证策略)进行多Realm身份认证(默认ModularRealmAuthenticator调用AuthenticationStrategy进行认证);
5)、Authenticator将token传入Realm,从realm中获取身份认证信息,若没有返回、抛出异常就表示身份认证失败
2、SecurityManager集成Authenticator,另外还有个实现ModularRealmAuthenticator,其委托Realm进行认证,认证策略有AuthenticationStrategy接口指定,
默认提供了以下三个实现:
1)、FirstSuccessfulStrategy
2)、AtLeastIneSuccessfulStrategy
3)、AllSuccessfulStrategy
3、配置
4、Realm继承图
二、授权
1、Shiro支持粗粒度权限(以角色为单位)、细粒度权限(以资源为单位)
2、授权流程:
1)、首先调用subject.isPermitted/hasRole,自动委托给securityManager,接着委托给Authorizer;
2)、Authorizer是真正的授权者,当我们调用isPermited( “user:view” )时会通过PermissionResolver将字符窜转换成Permission实例;
3)、进行授权前,会调用相应的realm获取subject相应的角色/群贤用于匹配传入的角色/权限;
4)、Authorizer判断Realm中的角色/权限是否与传入的角色/权限匹配,若有多个Realm,则委托给ModularRealmAutorizer循环判断
3、ModularRealmAuthorizer进行多Realm匹配流程:
1)、首先检查realm是否实现Authorizer;
2)、若实现Authorizer则调用其相应的isPermitted/hasRole;
3)、如果有一个匹配则返回true,否则返回false;
4、实现AuthorizreRealm的realm进行授权流程:
1)、如果调用hasRole,则直接获取AuthorizationInfo.getRoles()与传入的角色进行匹配即可;
2)、如果调用isPermited( “user:view” ),则:
a)、通过PermissionResolver(默认使用WildcardPermissionResolver)将字符串转换成Permission(默认为WildcardPermission);
b)、获取用户Permission实例集合
方式一:AuthorizationInfo.getObjectPermissions( )
方式二:AuthorizationInfo.getStringPermissions( )
c)、获取用户角色,并通过RolePermissionResolver解析角色对应的权限集合(默认没有实现,可以自定义)
d)、调用Permission.implies( Permission p)逐个与传入的权限比较,有匹配上则返回true,否则返回false;
5、配置:
三、编码与加密
1、编码/解码方式:
1)、Base64
2)、Hex
3)、MD5
4)、SHA-1
//Base64String base64Encoded = Base64.encodeToString("admin".getBytes()); //编码String base64Decoded = Base64.decodeToString(base64Encoded) //解码//HexStirng hexEncoded = Hex.encodeToString("admin".getBytes()); //编码String hexDecoded = new String(Hex.decode(hexEncoded.getBytes())); //解码//MD5String md5 = new MD5Hash(str,salt)toString();//SHA-1String sha1Hash = new SimpleHash("SHA-1",str,salt).toString();
5)、为了方便,shiro提供了HashService(默认实现:DefaultHashService)
2、DefaultPasswordService+PasswordMatcher实现加解密
1)、自定义Realm
public class MyRealm extends AuthorizingRealm { private PasswordService passwordService; public void setPasswordService(PasswordService passwordService) { this.passwordService = passwordService; } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 。。。 } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { return new SimpleAuthenticationInfo( "wu",//实际为存进数据库中的加密密文 passwordService.encryptPassword("123"), getName()); }}
2)、配置
[main] passwordService=org.apache.shiro.authc.credential.DefaultPasswordService hashService=org.apache.shiro.crypto.hash.DefaultHashService passwordService.hashService=$hashService hashFormat=org.apache.shiro.crypto.hash.format.Shiro1CryptFormat passwordService.hashFormat=$hashFormat hashFormatFactory=org.apache.shiro.crypto.hash.format.DefaultHashFormatFactory passwordService.hashFormatFactory=$hashFormatFactory passwordMatcher=org.apache.shiro.authc.credential.PasswordMatcher passwordMatcher.passwordService=$passwordService myRealm=com.github.zhangkaitao.shiro.chapter5.hash.realm.MyRealm myRealm.passwordService=$passwordService myRealm.credentialsMatcher=$passwordMatcher securityManager.realms=$myRealm
图解:
3、MD5+HashedCredentialsMatcher实现加解密
1)、生成密码散列值
String algorithmName = "md5";String userName = "admin";String password = "admin";String salt1 = username;String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();String salt = salt1 + salt2;int hashIterations = 2;SimpleHash hash = new SimpleHash(algorithmName,password,salt,hashIterations)
2)、自定义realm
public class MyRealm2 extends AuthorizingRealm { ... @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = "liu"; String password = "be320beca57748ab9632c4121ccac0db"; //加密后的密码 SimpleAuthenticationInfo ai = new SimpleAuthenticationInfo(username, password, getName());String salt = username + salt2; ai.setCredentialsSalt(ByteSource.Util.bytes(salt)); //盐是用户名+随机数 return ai; }}
3)、配置
[main] credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher credentialsMatcher.hashAlgorithmName=md5 credentialsMatcher.hashIterations=2 credentialsMatcher.storedCredentialsHexEncoded=true myRealm=com.github.zhangkaitao.shiro.chapter5.hash.realm.MyRealm2 myRealm.credentialsMatcher=$credentialsMatcher securityManager.realms=$myRealm
图解:
4、继承图
- shiro知识汇总
- shiro知识汇总
- Shiro异常汇总
- shiro标签使用汇总
- shiro知识截取
- Shiro知识复习
- 知识汇总
- 知识汇总
- 知识汇总
- 一、shiro入门知识笔记
- 知识管理软件厂商汇总
- web2.0知识汇总
- 结构体知识汇总
- 结构体知识汇总
- 常用成语知识汇总
- 汇编知识大汇总
- 结构体知识汇总
- 索引相关知识汇总
- C语言小游戏---扫雷
- python(3)——实现购物车的程序
- vhost xml配置
- python复习
- 大明A+B(大数相加)解题报告
- shiro知识汇总
- Nepire的校OJ入门题解—蓝桥选拔篇(伪最终回)
- 景格虚拟教具混合动力汽车动力系统虚拟结构原理展示台复制狗
- 优先队列---二叉堆
- Partition Equal Subset Sum解题报告
- Java虚拟机基础知识
- arduino学习笔记5 舵机控制
- fresco加载图片 动图
- 如何遍历Map