增加shiro测试代码

This commit is contained in:
lixiangrong
2016-01-26 16:51:50 +08:00
parent c1445e3b31
commit d7bddaee3b
4 changed files with 47 additions and 8 deletions

View File

@@ -0,0 +1,33 @@
package osc.git.eh3.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.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Assert;
import org.junit.Test;
public class LoginLogoutTest {
@Test
public void testHelloworld() {
// 1、获取SecurityManager工厂此处使用Ini配置文件初始化SecurityManager
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
// 2、得到SecurityManager实例并绑定给SecurityUtils
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
// 3、得到Subject及创建用户名/密码身份验证Token即用户身份/凭证)
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
try {
// 4、登录即身份验证
subject.login(token);
} catch (AuthenticationException e) {
// 5、身份验证失败
}
Assert.assertEquals(true, subject.isAuthenticated()); // 断言用户已经登录
// 6、退出
subject.logout();
}
}

View File

@@ -1,8 +0,0 @@
package osc.git.test;
public class Test {
public static void main(String[] args) {
System.out.println("Hello Git!!!");
}
}

View File

@@ -0,0 +1,3 @@
[users]
zhang=123
wang=123