Shiro的Realms

为什么80%的码农都做不了架构师?>>>   hot3.png

Realms概述

n概述

  Realm 是一个能够访问应用程序特定的安全数据(如用户、角色及权限)的组件。

  Realm 通常和数据源是一对一的对应关系,如关系数据库,LDAP 目录,文件系统,或其他类似资源。Realm 实质上就是一个特定安全的DAO。

  因为这些数据源大多通常存储身份验证数据(如密码的凭证)以及授权数据(如角色或权限),每个Realm能够执行身份验证和授权操作。

 

n关于Realm的配置

  这个在前面讲过了,这个就不去赘述了

 

理解Realms的认证实现

n前面学到过,Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。

  该方法通常会在org.apache.shiro.realm.AuthenticatingRealm中实现,当然,这个方法中会调用到具体realm实现的方法。

n该方法主要执行以下操作:

1、检查提交的进行认证的令牌信息

2、根据令牌信息从数据源(通常为数据库)中获取用户信息

3、对用户信息进行匹配验证。

4、验证通过将返回一个封装了用户信息的AuthenticationInfo实例。

5、验证失败则抛出AuthenticationException异常信息。

 

  这是对所有Realm getAuthenticationInfo 实现的最高级别的工作流。

  验证通过后,就返回一个非空的AuthenticationInfo 实例来代表来自于该数据源的Subject 帐户信息。

 

Shiro默认的Realms的认证实现

n所有Shiro 立即可用的Realm 的实现默认使用SimpleCredentialsMatcher。SimpleCredentialsMatcher 执行一个普通的直接相等性的检查,也就是在存储的帐户credentials 与在AuthenticationToken 所提交的之间的检查。

n使用Hashing Credentials

  如果要使用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

 

 

n新的实现配置示例:

[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时传的密码就应该是加密后的字符串了

 

n你还可以在密码加密的时候,加点salt,使密码更安全。这种方式目前默认的iniRealm没有支持,只能是在自己扩展的Realm里面使用

n新的配置文件示例:

[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

 

n获得加盐后的密码字符串

String ss = new Sha256Hash("cc","javass",10).toBase64();

 

n在自定义的realm中,返回的SimpleAuthenticationInfo需要修改一下,要加入salt的信息,当然,密码也需要是加密后的字符串,修改为:

SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,"kExd2f52W1M/wXidIRjOfMDj76DVo6e2md+7Rn4ubmY=" .toCharArray(),ByteSource.Util.bytes("javass".getBytes()),getName()
);

 要保证你的Realm 实现必须返回一个SaltedAuthenticationInfo 实例而不是一个普通的AuthenticationInfo 实例。

使用默认的JdbcRealm

n这个需要在数据库中建立相应的表,然后配置相应的数据库连接,然后才能使用,这里以spring中的bean定义来说明一下,示例如下:



自定义Realm

    n自定义Reald非常简单,通常是继承AuthorizingRealm 抽象类,这个类实现了常用的authentication 及authorization 工作流来节省你的时间和精力。n然后覆盖doGetAuthenticationInfo,在这个方法里面实现获取用户信息

n基本的示例如下:

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;
}

n然后覆盖doGetAuthorizationInfo,在这个方法里面实现获取用户权限的信息,基本的示例如下:

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {String userName = (String) getAvailablePrincipal(principals);//通过用户名去获得用户的所有资源,并把资源存入info中//当然这里通常会操作数据库去获取SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();Set s = new HashSet();s.add("p1");s.add("p2");info.setStringPermissions(s);Set r = new HashSet();r.add("r1");r.add("r2");info.setRoles(r);return info;
}

 

在spring的配置文件中使用

n在Spring的配置文件中,配置的内容和ini文件是一样,只不过转换成xml的风格,用bean的方式来配置

n基本的示例如下:




 而且,由于把我们的Realm配置成bean了,自然就可以使用依赖注入等功能了。

私塾在线   原创,转载请注明   http://sishuok.com/forum/blogPost/list/0/7456.html



转载于:https://my.oschina.net/boonya/blog/348146


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部