java用户的授权及验证_Java环境下shiro的测试-认证与授权

Java环境下shiro的测试

1.导入依赖的核心jar包

org.apache.shiro

shiro-core

1.3.2

2.认证程序

2.1 构建users配置文件 xxx.ini doGetAuthenticationInfo方法从该配置文件中获取数据与token中比对

[users]

test=123456

lisi=123456

测试程序

public class TestShiro {

public static void main(String[] args) {

//获取安全管理器工厂

IniSecurityManagerFactory iniSecurityManagerFactory = new IniSecurityManagerFactory("classpath:shiro.ini");

//获取安全管理器

SecurityManager securityManager = iniSecurityManagerFactory.getInstance();

//set认证器

SecurityUtils.setSecurityManager(securityManager);

//subject发起认证,获取subject

Subject subject = SecurityUtils.getSubject();

AuthenticationToken authenticationToken = new UsernamePasswordToken("test","123456");

//认证失败会抛出异常 密码:CredentialsException 账户:UnknownAccountException

try {

subject.login(authenticationToken);

} catch (AuthenticationException e) {

e.printStackTrace();

}

//当前subject是否认证通过

boolean authenticated = subject.isAuthenticated();

System.out.println(authenticated);

}

}

认证流程:

token携带身份和凭证信息--->subject发起认证--->SimpleAccountRealm(doGetAuthenticationInfo)获取配置文件中的用户信息---->CredentialsMatcher接口的实现类SimpleCredentialsMatcher:doCredentialsMatch方法对配置文件中的信息与token携带的信息进行比对--->认证成功或者失败。

3.Shiro框架中的关键对象:

AuthenticatingRealm //抽象类

//3.关键属性 该属性为凭证匹配器

CredentialsMatcher credentialsMatcher;

//1.该方法为抽象方法 其作用使用来获取数据

protected abstract AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken var1) throws AuthenticationException;

SimpleAccountRealm

//2.实现了AuthenticatingRealm抽象方法,用来获取配置文件中的用户信息,该类不做数据比对

SimpleCredentialsMatcher

//4.shiro中默认的凭证匹配器

public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {

Object tokenCredentials = this.getCredentials(token);

Object account

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值