1.在身份认证过程中往往会涉及加密,如果不加密那么数据信息不安全,Shiro内部实现比较多的散列算法。如MD5,SHA等。并且提供了加盐功能,如1111的md5码为b59c67bf196a4758191e42f76670ceba,这个md5码有很多破解网站上可以破解。但是如果1111+姓名,那么破解的难度会增加。
2.测试MD5算法:
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
public class shiroMD5_demo {
public static void main(String[] args) {
//使用md5加密
Md5Hash md5 = new Md5Hash("1111");
System.out.println(md5.toString());
//加盐
md5 = new Md5Hash("1111", "lyh");
System.out.println(md5.toString());
//迭代次数
md5 = new Md5Hash("1111", "lyh",10);
System.out.println(md5.toString());
//SimpleHash类也可以实现
SimpleHash simpleHash = new SimpleHash("md5", "1111", "lyh", 10);
System.out.println(simpleHash.toString());
}
}
3.在自定义Realm中使用散列算法
ini配置文件:
[main]
#定义凭证匹配器
credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher
#定义你要选择的散列算法
credentialsMatcher.hashAlgorithmName=md5
#散列迭代次数
credentialsMatcher.hashIterations=10
#将定义好的凭证器设置进自定义Realm里
UserRealm=com.lyh.UserRealm_demo
UserRealm.credentialsMatcher=$credentialsMatcher
securityManager.realms=$UserRealm
[users]
自定义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;
import org.apache.shiro.util.ByteSource;
public class UserRealm_demo extends AuthorizingRealm{
@Override
public String getName() {
return "UserRealm";
}
//授权方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
}
//完成身份认证(我们只是取数据),并且返回身份信息,如果失败返回null
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//获取身份信息,获取用户输入的用户名
String username = (String)authenticationToken.getPrincipal();
System.out.println("username==========="+username);
//根据用户名到数据库查询密码
//模拟从数据库获得1111加密后的密码,获取加盐的salt值
String pwd="2a3d662192d00d5a906a067a665aedcc";
String salt="lyh";
//将从数据库中查询的信息,封装到SimpleAuthenticationInfo中
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd, ByteSource.Util.bytes(salt),getName());
return info;
}
}
shiro测试代码:
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
public class jiami_demo {
public static void main(String[] args) {
//1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
Factory<SecurityManager> factory= new IniSecurityManagerFactory("classpath:shiro.ini");
//2、得到SecurityManager实例 并绑定给SecurityUtils
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
//3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("wangwu", "1111");
try {
subject.login(token);
if(subject.isAuthenticated()){
System.out.println("验证通过");
}
} catch (AuthenticationException e) {
e.printStackTrace();
System.out.println("验证失败");
}
}
}
网友评论