引入pom依赖
说明:Shiro获取权限相关信息可以通过数据库获取,也可以通过ini配置文件获取
这里演示从中获取。
2.登录认证
2.1登录认证概念
① 身份验证:就是判断一个用户是否为合法用户的处理过程。最常用的简单身份认证方式是系统通过核对用户输入的用户名和口令,看其是否与系统中存储的该用户的用户名和口令一致,来判断用户身份是否正确。
② 在shiro中,用户需要提供principals(身份)和credentials(证明)给shiro,从而应用能验证用户身份;
③ principals:身份,即主体的标识属性,可以是任何属性,如用户名、邮箱等,唯一即可。一个主体可以有多个principals,但只有一个主身份 ( Primary principals ),一般是用户名/邮箱/手机号。
④ credentials:证明/凭证,即只有主体知道的安全值,如密码/数字证书等。
最常见的principals和credentials组合就是用户名/密码
2.2登录认证基本流程
① 收集用户身份/凭证,即如用户名/密码
② 调用 Subject.login 进行登录,如果失败将得到相应 的 AuthenticationException异常,根据异常提示用户 错误信息;否则登录成功
③ 创建自定义的 Realm 类,继承 org.apache.shiro.realm.AuthenticatingRealm类,实现 doGetAuthenticationInfo() 方法
四步走:
public static void main(String[] args) {
//1.创建安全管理器对象
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//2.给安全管理器设置realm
securityManager.setRealm(new IniRealm("classpath:shiro.ini"));
//3.SecurityUtils给全局安全工具类设置安全管理器
SecurityUtils.setSecurityManager(securityManager);
//4.关键对象subject主体
Subject subject = SecurityUtils.getSubject();
//5.创建令牌
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","123456");
try {
System.out.println("认证状态"+subject.isAuthenticated());//false
//用户认证
subject.login(token);
System.out.println("登陆成功");
System.out.println("认证状态"+subject.isAuthenticated());
}catch (UnknownAccountException e){
e.printStackTrace();
System.out.println("认证失败,用户名不存在");
}catch (IncorrectCredentialsException e){
e.printStackTrace();
System.out.println("认证失败,密码错误");
}
} 常见的异常类型
DisabledAccountException(帐号被禁用)
LockedAccountException(帐号被锁定)
ExcessiveAttemptsException(登录失败次数过多)
ExpiredCredentialsException(凭证过期)等
通过分析源码可得:
自定义Realm的作用:放弃使用.ini文件,使用
上边的程序使用的是Shiro自带的IniRealm,IniRealm从ini配置文件中读取用户的信息,大部分情况下需要从系统的数据库中读取用户信息,所以需要自定义realm。
1.shiro提供的Realm
源码部分:
public class SimpleAccountRealm extends AuthorizingRealm {
//.......省略
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
SimpleAccount account = getUser(upToken.getUsername());
if (account != null) {
if (account.isLocked()) {
throw new LockedAccountException("Account [" + account + "] is locked.");
}
if (account.isCredentialsExpired()) {
String msg = "The credentials for account [" + account + "] are expired";
throw new ExpiredCredentialsException(msg);
}
}
return account;
}
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String username = getUsername(principals);
USERS_LOCK.readLock().lock();
try {
return this.users.get(username);
} finally {
USERS_LOCK.readLock().unlock();
}
}
} 3.自定义realm
package com.lut.realm;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
/**
* 自定义Realm 将认证/授权的数据的来源转为数据库的实现
*/
public class CustomerRealm extends AuthorizingRealm {
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
System.out.println("==================");
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//在token中获取 用户名
String principal = (String) token.getPrincipal();
System.out.println(principal);
//实际开发中应当 根据身份信息使用jdbc mybatis查询相关数据库
//在这里只做简单的演示
//假设username,password是从数据库获得的信息
String username="zhangsan";
String password="123456";
if(username.equals(principal)){
//参数1:返回数据库中正确的用户名
//参数2:返回数据库中正确密码
//参数3:提供当前realm的名字 this.getName();
SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(principal,password,this.getName());
return simpleAuthenticationInfo;
}
return null;
}
}
注意:真正项目中,这里重写的认证方法应该要根据token中传入的用户名到数据库中查询对应的密码。然后通过SimpleAuthenticationInfo 的构造器放入SimpleAuthenticationInfo 对象中,返回给安全管理器,安全管理器会帮我们把用户输入的密码和数据库中的密码进行对比验证。
4.使用自定义Realm认证
使用我们刚刚自定义Realm完成从数据库中获取认证数据源
package com.lut.test;
import com.lut.realm.CustomerRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
/**
* 测试自定义的Realm
*/
public class TestAuthenticatorCusttomerRealm {
public static void main(String[] args) {
//1.创建安全管理对象 securityManager
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//2.给安全管理器设置realm(设置为自定义realm获取认证数据)
defaultSecurityManager.setRealm(new CustomerRealm());
//IniRealm realm = new IniRealm("classpath:shiro.ini");
//3.给安装工具类中设置默认安全管理器
SecurityUtils.setSecurityManager(defaultSecurityManager);
//4.获取主体对象subject
Subject subject = SecurityUtils.getSubject();
//5.创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");
try {
subject.login(token);//用户登录
System.out.println("登录成功~~");
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户名错误!!");
}catch (IncorrectCredentialsException e){
e.printStackTrace();
System.out.println("密码错误!!!");
}
}
} 2.5使用MD5+Salt+Hash
补充:MD5算法
作用:一般用来加密或者签名(校验和)
特点:MD5算法不可逆如何内容相同无论执行多少次md5生成结果始终是一致
生成结果:始终是一个16进制32位长度字符串
使用场景:比较两个文件内容是否相同。可以对两个文件进行校验,如果校验之后两个生成的值是一样的,说明两个文件内容完全一致。
注意:
这里为了保证用户的安全,用户注册时:不能直接把密码明文存储在数据库中。要在密码上先拼接上salt,再使用MD5对整个字符串进行校验。最后把结果存储在数据库中。
用户登录时:也要在密码相同的位置(比如说注册和登录时都是在密码最后拼接salt)拼接salt。然后再进行MD5校验。
注册时和登录时使用的salt必须要是同一个。可以写在后端的配置文件中,也可以随机生成一个,然后跟注册时一起存到数据库中。总之必须保证注册时和登陆时拼接的salt是同一个。
MD5的基本使用
package com.lut.test;
import org.apache.shiro.crypto.hash.Md5Hash;
public class TestShiroMD5 {
public static void main(String[] args) {
//使用md5
Md5Hash md5Hash = new Md5Hash("123");
System.out.println(md5Hash.toHex());
//使用MD5 + salt处理
Md5Hash md5Hash1 = new Md5Hash("123", "X0*7ps");
System.out.println(md5Hash1.toHex());
//使用md5 + salt + hash散列(参数代表要散列多少次,一般是 1024或2048)
Md5Hash md5Hash2 = new Md5Hash("123", "X0*7ps", 1024);
System.out.println(md5Hash2.toHex());
}
}
输出结果:
202cb962ac59075b9b07152d234b70
bad42e603db5b50a78d600917c2b9821
7268f6d32ec8d6f4c305ae92395b00e8
自定义md5+salt的realm
package com.lut.realm;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
/**
* 使用自定义realm 加入md5 + salt +hash
*/
public class CustomerMd5Realm extends AuthorizingRealm {
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//获取 token中的 用户名
String principal = (String) token.getPrincipal();
//假设这是从数据库查询到的信息
String username="zhangsan";
String password="7268f6d32ec8d6f4c305ae92395b00e8";//加密后
//根据用户名查询数据库
if (username.equals(principal)) {
//参数1:数据库用户名
//参数2:数据库md5+salt之后的密码
//参数3:注册时的盐
//参数4:realm的名字
return new SimpleAuthenticationInfo(principal,
password,
ByteSource.Util.bytes("@#$*&QU7O0!"),
this.getName());
}
return null;
}
}
使用md5+salt 认证
package com.lut.test;
import com.lut.realm.CustomerMd5Realm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import java.util.Arrays;
public class TestCustomerMd5RealmAuthenicator {
public static void main(String[] args) {
//1.创建安全管理器
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
//2.注入realm
CustomerMd5Realm realm = new CustomerMd5Realm();
//3.设置realm使用hash凭证匹配器
HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
//声明:使用的算法
credentialsMatcher.setHashAlgorithmName("md5");
//声明:散列次数
credentialsMatcher.setHashIterations(1024);
realm.setCredentialsMatcher(credentialsMatcher);
defaultSecurityManager.setRealm(realm);
//4.将安全管理器注入安全工具
SecurityUtils.setSecurityManager(defaultSecurityManager);
//5.通过安全工具类获取subject
Subject subject = SecurityUtils.getSubject();
//6.认证
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "123");
try {
subject.login(token);
System.out.println("登录成功");
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("用户名错误");
}catch (IncorrectCredentialsException e){
e.printStackTrace();
System.out.println("密码错误");
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容