新建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>
在src/main/java文件夹下,新建包method1,编写自定义realm
package com.shrimpking.method1.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;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/10/2 12:46
* 自定义realm类
*/
public class CutomerRealm extends AuthorizingRealm
{
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
{
return null;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException
{
//获取用户身份信息
String principal = authenticationToken.getPrincipal().toString();
System.out.println(principal);
//模拟数据库获取用户名称,判断是否一致
String username = "zhangsan";
if(username.equals(principal)){
//参数1,用户名;
//参数2,数据库中的密码
//参数3,调用的realm的名称
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
principal,"1234",this.getName());
return info;
}
return null;
}
}
在test文件夹下,编写测试类
package com.shrimpking;
import com.shrimpking.method1.realm.CutomerRealm;
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.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 12:48
* 使用自定义realm测试
*/
@SpringBootTest
public class MethodOneTest
{
@Test
public void test(){
//创建安全管理器
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//设置自定义realm
securityManager.setRealm(new CutomerRealm());
//设置安全管理器
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){
System.out.println("认证结果:用户不存在");
e.printStackTrace();
}
catch (IncorrectCredentialsException e){
System.out.println("认证结果:密码错误");
e.printStackTrace();
}
catch (AuthenticationException e)
{
System.out.println("认证结果:登录失败");
e.printStackTrace();
}
}
}
通过切换string类型的用户名,查看测试结果。