Shiro身份验证

Shiro 可以帮助我们完成:认证、授权、加密、会话管理、与 Web 集成、缓存等。
示例
s
在 shiro 中,用户需要提供 principals (身份-用户名)和 credentials(证明-密码)给 shiro,从而应用能验证用户身份。

UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
	subject.login(token);
} catch (AuthenticationException e) {}
Assert.assertEquals(true, subject.isAuthenticated()); //断言用户已经登录
subject.logout();

Authenticator 可能会委托给相应的 AuthenticationStrategy 进行多 Realm 身份验证。

public class MyRealm1 implements org.apache.shiro.realm.Realm {
	@Override
	public String getName() {
		return "myrealm1";
	}
	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) 
	    throws AuthenticationException {
	    String username = (String)token.getPrincipal();  //得到用户名
	    String password = new String((char[])token.getCredentials()); //得到密码
	    return new SimpleAuthenticationInfo(username, password, getName());
	}
}

ModularRealmAuthenticator,其委托给多个 Realm 进行验证

FirstSuccessfulStrategy:只要有一个 Realm 验证成功即可,只返回第一个 Realm 身份验证成功的认证信息,其他的忽略
AtLeastOneSuccessfulStrategy(默认):只要有一个 Realm 验证成功即可,和 FirstSuccessfulStrategy 不同,返回所有 Realm 身份验证成功的认证信息;
AllSuccessfulStrategy:所有 Realm 验证成功才算成功,且返回所有 Realm 身份验证成功的认证信息,如果有一个失败就失败了。

myrealm=com.nfcm.shiro.Realm.MyRealm
myrealm2=com.nfcm.shiro.Realm.MyRealm2
#设置策略,必须所有的realm都通过
authcStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
#配置认证器
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
#将验证器和策略关联起来
authenticator.authenticationStrategy=$authcStrategy
#注入认证器
securityManager.authenticator=$authenticator
#设置realm,这个要最后设置,如果后设置认证器或者授权器,则里面的realms都是空的
securityManager.realms=$myrealm,$myrealm2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值