核心jar包 shiro.ini
shiro.ini:
[users]
zb=123
lwk=1
身份认证代码:
// 读取配置文件shiro.ini,初始化SecurityManager工厂
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 获取SecurityManager实例
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
// 把绑定SecurityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 得到当前执行用户
Subject currentUser = SecurityUtils.getSubject();
// 创建token令牌,用户名/密码
UsernamePasswordToken token = new UsernamePasswordToken("zb", "123");
try {
// 身份认证
currentUser.login(token);
System.out.println("身份认证成功!" + token.getUsername());
} catch (IncorrectCredentialsException e) {
System.out.println("登录密码错误");
} catch (ExcessiveAttemptsException e) {
System.out.println("登录失败次数过多");
} catch (LockedAccountException e) {
System.out.println("帐号已被锁定");
} catch (DisabledAccountException e) {
System.out.println("帐号已被禁用");
} catch (ExpiredCredentialsException e) {
System.out.println("帐号已过期");
} catch (UnknownAccountException e) {
System.out.println("帐号不存在");
}
// 退出
currentUser.logout();
网友评论