shiro学习之认证

shiro框架的认证机制是基于securityManager的

认证流程:

1.创建securityManager对象并指定realm,此处我使用了自定义的realm

org.apache.shiro.mgt.SecurityManager securityManager = new DefaultSecurityManager(new MyRealm());
		SecurityUtils.setSecurityManager(securityManager);
		
		Subject subject = SecurityUtils.getSubject();

 

自定义realm的代码

public class MyRealm extends AuthorizingRealm{
	private String username = "zs";
	
	private String password = "123";
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		String uname = (String) token.getPrincipal();
		String pw = new String((char[])token.getCredentials());
		
		AuthenticationInfo info;
		if(username.equals(uname)&&password.equals(pw)){
			//成功后手动封装一个登陆信息类
			info = new  SimpleAuthenticationInfo(uname,pw,getName());
			return info;
		}else{
			throw new UnknownAccountException();
		}
		

2.创建token对象.通常实用usernameAndPasswordToken来构建

AuthenticationToken token = new UsernamePasswordToken("zst","123");

3.调用subject对象中的login方法.

try {
			subject.login(token);
		} catch (AuthenticationException e) {
			System.out.println("用户名或密码错误");
		}

shiro框架做了什么呢?

首先在调用login方法后会将令牌和subject对象交给securityManager来进行认证

   172457_cen4_3521492.png

然后会交给authenticator验证身份,调用doAuthenticate方法

172554_aIoM_3521492.png

通过realm对象完成验证

172609_FRtE_3521492.png

验证完成后生成登录成功对象

 

转载于:https://my.oschina.net/dreamForMe/blog/1532064

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值