1 package cn.lqp.shrio.test;
2
3 import org.apache.shiro.SecurityUtils;
4 import org.apache.shiro.authc.AuthenticationException;
5 import org.apache.shiro.authc.UsernamePasswordToken;
6 import org.apache.shiro.config.IniSecurityManagerFactory;
7 import org.apache.shiro.mgt.SecurityManager;
8 import org.apache.shiro.subject.Subject;
9
10 public class ShrioTest {
11
12 public static void main(String[] args) {
13
14 //读取配置文件
15 IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
16 SecurityManager securityManager = factory.createInstance();
17
18 //第二步 L构建验证信息Token
19 UsernamePasswordToken token = new UsernamePasswordToken("admin-ERROR","1234567");
20
21 //第三步:获得一个没有权限信息的身份对象
22 SecurityUtils.setSecurityManager(securityManager);
23 Subject subject = SecurityUtils.getSubject();
24
25 //第四步 : 校验
26 try {
27 Subject resultSubject = securityManager.login(subject, token);
28 System.out.println("获得校验后的用户名:"+resultSubject.getPrincipal());
29 System.out.println("判断校验是否通过:"+resultSubject.isAuthenticated());
30
31
32 } catch (AuthenticationException e) {
33 // TODO Auto-generated catch block
34 e.printStackTrace();
35 }
36
37 }
38
39 }
![](https://img2018.cnblogs.com/blog/1734439/201907/1734439-20190705203312960-258336350.png)