---
title: "Apache Shiro学习"
author: "stephansun"
date: "2012-04-01T16:59:59Z"
tags: []
categories: ["企业架构"]
slug: "apache-shiro"
draft: false
lastmod: "2012-04-04T23:06:18Z"
id: 1472426
url: "https://stephansun.iteye.com/blog/1472426"
---

提起Java社区中的安全框架，大家的第一反应应该是Spring Security，我曾经在09年看《Spring In Action 2nd Edition》时，学习了一下，按照书中的Demo搭建了一个Web项目，深感其配置之繁杂，浅尝辄止，索性就没管它了。去年江南白衣在公布SpringSide4的路线图中提及将使用Apache Shiro替换Spring Security，我才第一次知道Apache Shiro这个东西，看了一圈评论，都是“大家都说好，谁用谁知道”之类的。因为实际项目中还没有关于安全框架方面的需求，所以也没有跟进，最近上头跟我说了一个需求，我觉得用安全框架可以满足，这才把学习Apache Shiro摆上日程。

学习一门技术首先要看的就是看官方手册，下面列出Apache Shiro官方提供的教程和文档：

[http://shiro.apache.org/10-minute-tutorial.html](http://shiro.apache.org/10-minute-tutorial.html)：10分钟入门指南

[http://shiro.apache.org/java-authentication-guide.html](http://shiro.apache.org/java-authentication-guide.html)：Apache Shiro 授权 入门

[http://shiro.apache.org/java-authorization-guide.html](http://shiro.apache.org/java-authorization-guide.html)：Apache Shiro 认证 入门

[http://shiro.apache.org/reference.html](http://shiro.apache.org/reference.html)：Apache Shiro参考手册，目录结构：

I. 概览

1\. [介绍](http://shiro.apache.org/introduction.html)

2\. [入门](http://shiro.apache.org/tutorial.html)

3\. [架构](http://shiro.apache.org/architecture.html)

4\. [配置](http://shiro.apache.org/configuration.html)

II. 核心

5\. [授权](http://shiro.apache.org/authentication.html)

6\. [认证](http://shiro.apache.org/authorization.html)

6.1 [权限](http://shiro.apache.org/permissions.html)

7\. [Realms](http://shiro.apache.org/realm.html)

8\. [会话管理](http://shiro.apache.org/session-management.html)

9\. [密码加密](http://shiro.apache.org/cryptography.html)

III. Web应用

10\. [Web](http://shiro.apache.org/web.html)

10.1. [配置](http://shiro.apache.org/web.html#Web-configuration)

10.2. [\[urls\]（基于路径的安全控制）](http://shiro.apache.org/web.html#Web-webini)

10.3. [默认过滤器](http://shiro.apache.org/web.html#Web-defaultfilters)

10.4. [会话管理](http://shiro.apache.org/web.html#Web-sessionManagement)

10.5. [JSP标签库](http://shiro.apache.org/web.html#Web-taglibrary)

IV. 辅助支持

11\. [缓存](http://shiro.apache.org/caching.html)

12\. [并发和多线程](http://shiro.apache.org/concurrency.html)

13\. [测试](http://shiro.apache.org/testing.html)

14\. [定制Subjects](http://shiro.apache.org/subject.html)

V. 集成

15\. [Spring框架](http://shiro.apache.org/spring.html)

16\. [Guice](http://shiro.apache.org/guice.html)

17\. [CAS](http://shiro.apache.org/cas.html)

VI. 工具

18\. [命令行哈希校验器](http://shiro.apache.org/command-line-hasher.html)

VII. 索引

19\. [术语](http://shiro.apache.org/terminology.html)

 

在看完《10分钟入门指南》、《Apache Shiro 授权 入门》、《Apache Shiro 认证 入门》后，知道了Apache Shiro基本概念和代码写法。

接着就是搭建Web项目，Apache Shiro参考手册第10节讲了如何在Web应用中加入Apache Shiro，但是我搭建的Web项目基于SpringMVC，所以先看了第15节。

然后想到一个问题，《10分钟入门指南》和Apache Shiro参考手册第10节中都使用了shiro.ini作为Shiro的配置文件，但在和SpringMVC集成后，该配置文件就不需要了。

在stackoverflow上搜索到一个帖子 [http://stackoverflow.com/questions/7448460/spring-mvc-and-shiro-configuration-using-ini-files](http://stackoverflow.com/questions/7448460/spring-mvc-and-shiro-configuration-using-ini-files) 解答了我的疑惑。

最后便是看看江南白衣的SpringSide4的[mini-web模块](https://github.com/springside/springside4/blob/master/examples/mini-web/src/main/resources/applicationContext-shiro.xml)是如何应用Apache Shiro的。

我写的例子程序[samples-shiro](https://github.com/stephansun/samples/tree/master/samples-shiro)放在GitHub上了。

 

补充：

InfoQ上有一篇《[让Apache Shiro保护你的应用](http://www.infoq.com/cn/articles/apache-shiro)》，翻译的还不错。

IBM上还有一篇《[将 Shiro 作为应用的权限基础](http://www.ibm.com/developerworks/cn/opensource/os-cn-shiro/)》。

 

吐槽一下：Apache Shiro官方文档有很多错别字，或者重复输入的，看了这么多开源软件的官网文档，这种情况我还是第一次见。

 

\[2012.04.04 10:51 PM 正巧在Amazon购书有感] 补充：

java Authentication Guide | Apache Shiro

 

Remembered vs Authenticated

下面这个场景阐述了为什么isAuthenticated和isRemembered之间的区别是重要的。

假设你正在使用Amazon.com。你登陆后添加了一些书到购物车中。一天以后，你的用户会话（session）当然过期了，你已经登出了，但是Amazon“remember”你，通过名字和你打招呼，仍然给你个人书籍推荐。对于Amazon来说，isRemembered()返回TRUE。那么如果你试着使用信用卡结帐或者修改你的帐户信息会发生什么事呢？此时Amazon“remember”你，isRemembered() = TRUE，这并不意味着你就是事实上的你，因为isAuthenticated() = FALSE。所以在你进行下一步理智（sensitive）的行为之前，Amazon需要通过强制一个认证过程验证你的身份，这个过程就是返回登陆窗口。在登陆后，你的身份被确认了同时isAuthenticated() = TRUE。

该场景在web领域非常常见，因此Shiro内置了该功能，帮助你很容易的区分这两点。