四个包:commons-logging.jar
shiro-core.jar
slf4j-api.jar
sljf-simple.jar
在类下创建一个用户配置文件users.ini
内容:
[users]
zhang=123
wang=123
创建类TestShiro.java
import org.apache.shiro.SecurityUtils;
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;
import org.junit.Test;
public class TestShiro {
@Test
public void testShiro001(){
//1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
Factory<org.apache.shiro.mgt.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("zhang", "123");
try{
subject.login(token);
System.out.println("SUCCESS");
}catch(Exception e){
System.out.println("ERROR");
}
}
}
测试结束
转载于:https://blog.51cto.com/52wanit/1837218