Realms概述
Realm 是一个能够访问应用程序特定的安全数据(如用户、角色及权限)的组件。
Realm 通常和数据源是一对一的对应关系,如关系数据库,LDAP 目录,文件系统,或其他类似资源。Realm 实质上就是一个特定安全的DAO。
因为这些数据源大多通常存储身份验证数据(如密码的凭证)以及授权数据(如角色或权限),每个Realm能够执行身份验证和授权操作。
这个在前面讲过了,这个就不去赘述了
理解Realms的认证实现
该方法通常会在org.apache.shiro.realm.AuthenticatingRealm中实现,当然,这个方法中会调用到具体realm实现的方法。
1、检查提交的进行认证的令牌信息
2、根据令牌信息从数据源(通常为数据库)中获取用户信息
3、对用户信息进行匹配验证。
4、验证通过将返回一个封装了用户信息的AuthenticationInfo实例。
5、验证失败则抛出AuthenticationException异常信息。
这是对所有Realm getAuthenticationInfo 实现的最高级别的工作流。
验证通过后,就返回一个非空的AuthenticationInfo 实例来代表来自于该数据源的Subject 帐户信息。
Shiro默认的Realms的认证实现
如果要使用Hashing Credentials,那么需要在配置中告诉验证器,使用相应的匹配器,这个在前面示例过。
但是前面直接使用的Sha256Matcher,已经不推荐使用了,现在推荐使用统一的HashedCredentialsMatcher,然后配置具体的算法名称,这些名称按照Java Security Framework里面的标准名称来配置。常见的名称有:
MD5、AES 、DES 、SHA-1、SHA-256、SHA-384、SHA-512……很多
具体可以参见:http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html
新的实现配置示例:
[main]
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName = SHA-256
sha256Matcher.storedCredentialsHexEncoded=false
iniRealm.credentialsMatcher = $sha256Matcher
myRealm1=cn.javass.hello.MyRealm
myRealm1.credentialsMatcher = $sha256Matcher
authenticator = org.apache.shiro.authc.pam.ModularRealmAuthenticator
authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy
authenticator.authenticationStrategy = $authcStrategy
authenticator.realms=$myRealm1,iniRealm
securityManager.authenticator = $authenticator
[users]
javass =NVsbv8lnJc3Oj0onCP2jEKgObRMxWuxOXu0qdf6AMs4=,role1
[roles]
role1 = p1,p2
当然,别忘了在MyRealm中,创建SimpleAuthenticationInfo时传的密码就应该是加密后的字符串了
[main]
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName = SHA-256
sha256Matcher.storedCredentialsHexEncoded=false
sha256Matcher.hashIterations = 10
myRealm1=cn.javass.hello.MyRealm
myRealm1.credentialsMatcher = $sha256Matcher
authenticator = org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticator.realms=$myRealm1
securityManager.authenticator = $authenticator
[users]
javass =kExd2f52W1M/wXidIRjOfMDj76DVo6e2md+7Rn4ubmY=,role1
[roles]
role1 = p1,p2
String ss = new Sha256Hash("cc","javass",10).toBase64();
在自定义的realm中,返回的SimpleAuthenticationInfo需要修改一下,要加入salt的信息,当然,密码也需要是加密后的字符串,修改为:
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(
username,
"kExd2f52W1M/wXidIRjOfMDj76DVo6e2md+7Rn4ubmY=" .toCharArray(),
ByteSource.Util.bytes("javass".getBytes()),
getName()
);
要保证你的Realm 实现必须返回一个SaltedAuthenticationInfo 实例而不是一个普
通的AuthenticationInfo 实例。
使用默认的JdbcRealm
<bean id="myRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
<property name="dataSource" ref="dataSource" />
<property name="authenticationQuery"
value="select u.pwd from tbl_user u where u.uuid = ?" />
<property name="userRolesQuery"
value="select r.uuid from tbl_user_role ur left join tbl_role r on ur.roleUuid = r.uuid where ur.userUuid = ? " />
<property name="permissionsQuery"
value="select p.uuid from tbl_role r left join tbl_role_permission rp on r.uuid = rp.roleUuid left join tbl_permission p on rp.permissionUuid = p.uuid where r.uuid = ? " />
<property name="permissionsLookupEnabled" value="true" />
<property name="saltStyle" value="NO_SALT" />
</bean>
自定义Realm
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
//token中储存着输入的用户名和密码
UsernamePasswordToken upToken = (UsernamePasswordToken)token;
String username = upToken.getUsername();
//通常是根据用户名去数据库中查询相应信息,这里就省略了
//当然这里也可以对用户名和密码进行校验
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password .toCharArray(),getName());
return info;
}