导入依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.3</version>
</dependency>
在resources目录下创建shiro-first.ini文件
[users]
admin=111111
aaaaa=aaaaa
测试代码
/**
* 从shiro-first.ini文件中的账号密码进行认证
*/
@Test
public void testLoginAndLogout(){
//通过ini配置文件创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro/shiro-first.ini");
//创建SecurityManager
SecurityManager securityManager = factory.getInstance();
//将SecurityManager设置在当前的运行环境中
SecurityUtils.setSecurityManager(securityManager);
//从SecurityUtils中获取一个subject
Subject subject = SecurityUtils.getSubject();
//创建令牌
UsernamePasswordToken token = new UsernamePasswordToken("admin", "111111");
subject.login(token);
System.out.println("认证状态:"+subject.isAuthenticated());
subject.logout();
System.out.println("认证状态:"+subject.isAuthenticated());
}
输出结果
认证状态:true
认证状态:false