Apache Shiro 1.2.1版本发布

2012年7月29日Apache Shiro开源小组发布了Apache Shiro 1.2.1,此版本主要是修复了1.2.0版本中的11个bug。

Apache Shiro 1.2版本有哪些新功能点呢?通过其官方网站可以看到主要包含在4个方面:Tools, Core, Web, and Support Modules。

Tools

提供了一个命令行工具包可以为各种数据生成哈希值:shiro-tools-hasher-1.2.1-cli.jar

运行 java -jar shiro-tools-hasher-1.2.1-cli.jar 可看到帮助信息,此工具包可以为各种形式的数据生成哈希值,比如:标准的文本,密码,字符串,文件或是URL资源等。

帮助信息如下,已经很直观了:

usage: java -jar shiro-tools-hasher-.jar [options] []

Print a cryptographic hash (aka message digest) of the specified .
--
Options:
-a,--algorithm hash algorithm name. Defaults to SHA-256
when password hashing, MD5 otherwise.
-d,--debug show additional error (stack trace)
information.
-f,--format hash output format. Defaults to 'shiro1'
when password hashing, 'hex' otherwise.
See below for more information.
-gs,--gensalt generate and use a random salt. Defaults
to true when password hashing, false
otherwise.
-gss,--gensaltsize the number of salt bits (not bytes!) to
generate. Defaults to 128.
-help,--help show this help message.
-i,--iterations number of hash iterations. Defaults to
500000 when password hashing, 1 otherwise.
-ngs,--nogensalt do NOT generate and use a random salt
(valid during password hashing).
-p,--password hash a password (disable typing echo)
-pnc,--pnoconfirm hash a password (disable typing echo) but
disable password confirmation prompt.
-r,--resource read and hash the resource located at
. See below for more information.
-s,--salt use the specified salt. is
plaintext.
-sb,--saltbytes use the specified salt bytes. is
hex or base64 encoded text.

is optional only when hashing passwords (see below). It is
required all other times.

Password Hashing:
---------------------------------
Specify the -p/--password option and DO NOT enter a . You will
be prompted for a password and characters will not echo as you type.

Salting:
---------------------------------
Specifying a salt:

You may specify a salt using the -s/--salt option followed by the salt
value. If the salt value is a base64 or hex string representing a
byte array, you must specify the -sb/--saltbytes option to indicate this,
otherwise the text value bytes will be used directly.

When using -sb/--saltbytes, the -s/--salt value is expected to be a
base64-encoded string by default. If the value is a hex-encoded string,
you must prefix the string with 0x (zero x) to indicate a hex value.

Generating a salt:

Use the -sg/--saltgenerated option if you don't want to specify a salt,
but want a strong random salt to be generated and used during hashing.
The generated salt size defaults to 128 bits. You may specify
a different size by using the -sgs/--saltgeneratedsize option followed by
a positive integer (size is in bits, not bytes).

Because a salt must be specified if computing the
hash later, generated salts will be printed, defaulting to base64
encoding. If you prefer to use hex encoding, additionally use the
-sgh/--saltgeneratedhex option.

Files, URLs and classpath resources:
---------------------------------
If using the -r/--resource option, the represents a resource path.
By default this is expected to be a file path, but you may specify
classpath or URL resources by using the classpath: or url: prefix
respectively.

Some examples:

-r fileInCurrentDirectory.txt
-r ../../relativePathFile.xml
-r ~/documents/myfile.pdf
-r /usr/local/logs/absolutePathFile.log
-r url:http://foo.com/page.html
-r classpath:/WEB-INF/lib/something.jar

Output Format:
---------------------------------
Specify the -f/--format option followed by either 1) the format ID (as defined
by the org.apache.shiro.crypto.hash.format.DefaultHashFormatFactory
JavaDoc) or 2) the fully qualified org.apache.shiro.crypto.hash.format.HashFormat
implementation class name to instantiate and use for formatting.

The default output format is 'shiro1' which is a Modular Crypt Format (MCF)
that shows all relevant information as a dollar-sign ($) delimited string.
This format is ideal for use in Shiro's text-based user configuration (e.g.
shiro.ini or a properties file).


Core

增加了PasswordService组件,目的是将密码相关的一些逻辑和配置抽象出来,使程序的开发人员只关注密码本身即可。相应哈希算法,迭代,密码加盐等都在组件内部实现。

同时这种组件形式可以很好地复用。


增加了PasswordMatcher组件,PasswordMatcher实现了CredentialsMatcher接口,在认证过程中使用PasswordService时进行密码的比对。

password service 和 password matcher都可配置到Realm中使用。


如果使用文本方式配置用户账号的话,可以使用上述新增的组件和命令行工具在文本中安全地存储用户的密码。


认证缓存是一个新加的功能,主要应用在一些无状态的框架中,如REST或者基于SOAP的应用程序,在这些应用中每个请求都需要进行身份验证。这样就需要支持应用程序在相对较短的时间内对相同账号进行重复验证。如果每次验证时都需要访问数据源就会造成很大的压力,认证缓存将账号信息进行缓存以被短时间内的使用来减轻后台的数据源压力。

想要使用认证缓存机制必须实现3步:

1、你自己的认证Realm必须继承AuthenticatingRealm类或者是AuthorizingRealm;

2、必须为SecurityManager配置CacheManager. cacheManager = com.foo.some.CacheManager securityManager.cacheManager = $cacheManager

3、必须为认证Realm启用认证缓存 myRealm.authenicationCachingEnabled = true


WEB

增加了两个接口Environment 和 WebEnvironment。可允许开发人员通过Environment实例获得Shiro相关的实例对象。Shiro Filter将依赖于WebEnvironment中的对象,

当Servlet Context Listener初始化好后它将放到WebEnvironment实例中供应用程序来访问。你可以通过WebUtils类来访问WebEnvironment它包含对象


通过配置可非常方便地启用或停用过滤器,如下:

[main]
# uncomment the following line to disable the ‘ssl’ filter:
# ssl.enabled = false
[urls]
/securedEndpoint/** = ssl, someFilter, anotherFilter, ...
在开发的时候可以去掉注释ssl.enabled = false这样就不会执行ssl通讯,在实际应用时加上注释就会执行ssl。

新增了LogoutFilter实现将用户注销然后重定向到一个默认页。

新增了NoSessionCreationFilter过滤器主要应用于象REST或者基于SOAP这样的应用框架中。使用此过滤器可防止有人意外地创建会话,NoSessionCreationFilter对实现认证缓存的作用很大,可很好支持象REST这样的无状态应用。

New Support Modules

Apache Shiro本身就支持很多第三方的接口,例如Spring Framework 和 AspectJ等。在1.2版本里又新增了三个模块:

1、Shiro support for Google Guice

2、Shiro support for Jasig CAS

3、Apache Karaf features.xml


你可能感兴趣的:(Apache Shiro 1.2.1版本发布)