Shiro笔记之Hello world

Shiro笔记之Hello World

在上一篇文章中 Shiro笔记之简介 中已经详细的介绍了Shiro的核心概念,还记得: Authentication(认证),Authorization(授权)…等概念吗? 知道了概念,是时候实践一把了,今天为大家讲解入门教程,Hello World

1.1 环境准备

该项目使用Maven,以下为依赖包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<!--shiro 默认使用 org.slf4j记录日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>

1.2 添加身份并认证

添加使用shiro.ini,添加到classpath路径下

1
2
3
4
# 添加两个用户 andy, juqian
[users]
andy = 123456
juqian=123456

以下为核心代码,避免篇幅过长,包的引入已经去掉.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* @description: 测试Shiro的授权以及认证信息
* @author: Andy
* @date: 2017-02-09
* @blog: www.andyqian.com
*/
public class TestShiroHelloWorld {

private static final Logger log = LoggerFactory.getLogger(TestShiroHelloWorld.class);
/**
* 测试Shiro的认证
*/
@Test
public void testShiro(){
//1. 获取shiro配置,该路径支持: 1. classpath:xxx 2. url:xx 3. file:
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
SecurityManager securityManager = factory.getInstance();
//2. 获取securityManager并绑定SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
//3. 获取配置中的帐号信息
Subject currentUser = SecurityUtils.getSubject();
if (!currentUser.isAuthenticated()) {
//该参数为(用户名,密码)
UsernamePasswordToken token = new UsernamePasswordToken("andy", "123456");
//记住密码
token.setRememberMe(true);
try {
currentUser.login(token);
log.info("登录成功");
} catch (UnknownAccountException uae) {
log.info("There is no user with username of " + token.getPrincipal());
} catch (IncorrectCredentialsException ice) {
log.info("密码不正确:" + token.getPrincipal());
} catch (LockedAccountException lae) {
log.info("帐号已锁定: " + token.getPrincipal() + " is locked. ");
}
catch (AuthenticationException ae) {
log.info("未知错误信息");
}
}
Assert.assertEquals(true, currentUser.isAuthenticated());//用户已经登录
//4. 用户退出
currentUser.logout();
}

1.1 通过new IniSecurityManagerFactory 加载 shiro.ini并创建SecurityManager工厂.
1.2 通过 IniSecurityManagerFactory工厂获取到 SecurityManager实例,
1.3 将SecurityManager对象设置到SecurityUtils中
1.4 SecurityUtils.getSubject();获取对象,该方法的代码块为:

1
2
3
4
5
6
7
8
public static Subject getSubject() {
Subject subject = ThreadContext.getSubject();
if (subject == null) {
subject = (new Subject.Builder()).buildSubject();
ThreadContext.bind(subject);
}
return subject;
}

在该方法中,首先判断当前线程是否已经有Subject,如果为空,则实例化,并绑定到当前线程上下文中,(PS: 嗯哼,这不是一个单例模式吗?)
1.5 捕获相关的AuthenticationException,
异常相关: AuthenticationException 中有三个直接子类,分别为 AccountException(账户异常),CredentialsException(凭证异常),UnsupportedoTokenException(不支持token异常).
AccountException(账户异常):包括DisabledAccountException(禁用的帐号),LockedAccountException(锁定的帐号),ConcurrentAccessException(并发访问),ExcessiveAttemptsException(登录次数过多),UnknownAccountException(未知账户)等,详细的子父类层级图为:
异常全部子类的层级

1.6 最后调用currentUser.logout()用户退出

小结

在该篇文章中,给出了简单使用Shrio的案例,

参考链接

开涛的博客
Shiro官网

插件推荐

如果使用IntelliJ IDEA,推荐一个打开ini文件的插件, Ini4ldea

欢迎关注个人公众号 andyqian
个人公众号