shiro权限框架

一、shiro框架架构
在这里插入图片描述
subject:可以是程序,也可是用户。(访问的主体)
SecurityManager:安全管理器
Authenticator:验证器,主体通过他来验证
Authorizer:授权器,主体通过他来授权
SessionManager:shiro对session管理
SessionDao:session通过SessionDao来管理数据,存放数据
CacheManager:缓存管理器,对数据的缓存。
Realms:域,领域,相当于数据源,通过realm存取认证、授权相关数据。
注意:Realm中存放授权和认证的逻辑
cryptography:密码管理,提供密码的加密算法。
二、shiro的jar包
导入即可
三、shiro的验证流程
在程序中

//我们编写的shiro-first.ini
[users]
zhangsan=111
lisi=111
//1.通过配置文件来创建securityFactory
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
//通过securityFactory对象来得到securityManager对象
SecurityManager securityManager = factory.getInstance();
//将securityManager对象配置到当前环境中去
SecurityUtils.setSecurityManager(securityManager);
//得到subject对象
Subject subject = SecurityUtils.getSubject();
//创建一个令牌token,令牌存放配置文件中的键值对
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111");
try {
			// --》6.执行认证提交,相当于将令牌加入subject中
			subject.login(token);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// --》7.是否认证通过
		boolean isAuthenticated = subject.isAuthenticated();

四、执行流程
1.通过ini配置文件创建securityManager
2.调用subjet.login(token)提交令牌
3.ModularRealmAuthenticator认证,调用IniRealm去ini配置文件,查询用户信息
4.IniRealm根据token和ini配置文件的账号查询用户信息
*查到—给ModularRealmAuthenticato返回用户信息
*查不到—给ModularRealmAuthenticato返回null
5.ModularRealmAuthenticato收到返回的认证信息
*若为null则抛异常
*若密码不一致也抛出异常
小结
1.subject.login(token)用户提交令牌
2.securityManager传给ModularRealmAuthenticator认证
3.ModularRealmAuthenticator给IniRealm认证
4.IniRealm通过账号从ini配置文件中找
5.返回给ModularRealmAuthenticator。
6.返回若为null,抛异常。密码不一致也抛异常。

ModularRealmAuthenticator:进行验证,需调用realm查询用户信息,通过账号
ModularRealmAuthenticator:返回了用户需要比对密码

五、realm接口
在实际开发中,我们需要自己编写realm
1.首先我们的继承AuthorizingRealm 这个类
2.在配置文件中配置自定义的realm,我们会通过过这个配置唔见来创建securityFactory工厂。
固定写法

[main]
#自定义realm
customRealm=自定义的类
#将这个realm设置securityManager(相当于Spring注入)
securityManager.realms=$customRealm

然后重写里面的方法:

//主要是重写这个方法
@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//得到临牌中的账号
		String userCode =(String) token.getPrincipal();
		//通过这个账户查询用户,我们这里使用伪数据
		String password = "111";
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo("asdasd", password, this.getName());
		
		return info;
	}



	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
		// TODO Auto-generated method stub
		return null;
	}

之后我们测试类里面也一样的代码,不过inirealm变成了我们自定义的realm

六、散列算法
当我们导入完jar包可以使用它的

1.Md5Hash类来生成密码
//密码明文
String password = "111";
//盐
String salt = "abc";
//散列次数
int times = 2;
//得到了这个散列后的值
Md5Hash value = new Md5Hash(password,salt,times);
2.使用SimpleHash
//使用方式
SimpleHash simple = SimpleHash("md5",password,salt,times);
得到了散列后的值

配置我们定义的含有散列算法的realm类

[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列次数
credentialsMatcher.hashIterations=1

#将凭证匹配器设置到realm
customRealm=我们自定义的realm类
customRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$customRealm

自定义的realm类

@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

		String userCode = (String) token.getPrincipal();
		//伪数据密码
		String password = "800ad4b7e619512e25c46faae0e14a62";
		//伪数据盐
		String salt = "abc";
		//返回的这个盐参数写法,规定如此
		SimpleAuthenticationInfo  info = new SimpleAuthenticationInfo(userCode, password,ByteSource.Util.bytes(salt),this.getName());
		
		return info;
	}

七、小结shiro框架的验证的使用
1.配置我们自己编写的realm类(验证的逻辑)
2.根据配置文件得到securityManager
3.我们登陆输入了userCode 和password
4.将userCode和password放在token令牌里面
5.根据userCode去自定义的myRealm类里查询数据库的用户信息,
并返回包含userCode,password,(盐),this.getName()的对象
6.ModularRealmAuthenticator对返回的这个对象处理
6.1若空,抛异常
6.2若密码不对,抛异常
7若正确返回true

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值