新建springboot项目,导入依赖
<!-- ini整合方式的依赖 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
md5的练习
package com.shrimpking;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/10/2 17:35
*/
@SpringBootTest
public class MD5Test
{
@Test
public void test(){
String password = "1234";
//使用md5,创建加密
Md5Hash md5Hash1 = new Md5Hash(password);
System.out.println(password + "加密后= " + md5Hash1.toHex());
//1234加密后= 81dc9bdb52d04dc20036dbd8313ed055
String salt = "abc";
//使用md5,加盐,创建加密
Md5Hash md5Hash2 = new Md5Hash(password,salt);
System.out.println(password + "加密加盐后 = " + md5Hash2);
//1234加密加盐后 = a141c47927929bc2d1fb6d336a256df4
//使用md5,加盐,散列次数
Md5Hash md5Hash3 = new Md5Hash(password,slat,1024);
System.out.println(password + "加密加盐1024次后 = " + md5Hash3);
//1234加密加盐1024次后 = f26da6ea39b7e049d1c816585d867893
}
}
自定义MD5的realm类
package com.shrimpking.method2;
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;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/10/2 18:04
* 使用md5加密的自定义realm类
*/
public class CustomerMD5Realm extends AuthorizingRealm
{
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
{
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException
{
//获取身份信息
String principal = authenticationToken.getPrincipal().toString();
//获取凭证,模拟从数据库中获取
String password = "a141c47927929bc2d1fb6d336a256df4";
//加密盐
String salt = "abc";
//判断
if("zhangsan".equals(principal)){
//参数1,身份信息
//参数2,数据库中的密码
//参数3,加密盐
//参数4,realm的类名称
return new SimpleAuthenticationInfo(
principal,
password,
ByteSource.Util.bytes(salt),
getName()
);
}
return null;
}
}
测试
package com.shrimpking;
import com.shrimpking.method2.CustomerMD5Realm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
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 org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/10/2 18:09
*/
@SpringBootTest
public class MethodTwoTest
{
@Test
public void test(){
//创建安全管理器
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//创建自定义realm
CustomerMD5Realm customerMD5Realm = new CustomerMD5Realm();
//创建加密策略
HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
//设置加密策略
matcher.setHashAlgorithmName("md5");
//设置realm的加密策略
customerMD5Realm.setCredentialsMatcher(matcher);
//设置realm
securityManager.setRealm(customerMD5Realm);
//设置安全管理器
SecurityUtils.setSecurityManager(securityManager);
//获取用户主体
Subject subject = SecurityUtils.getSubject();
//创建令牌token
AuthenticationToken token = new UsernamePasswordToken("zhangsan","1234");
//登录认证
try
{
System.out.println("认证前状态: " + subject.isAuthenticated());
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("认证结果:密码错误");
}
}
}