shiro教程6(多realm认证)

为什么要使用多realm认证?

实际开发中存在这样一种场景,同一个密码可能在MqSQL中存储,也可能在Oracle中存储,有可能MqSQL中使用的是MD5加密算法,而Oracle使用SHA1加密算法。这就需要有多个Realm以及认证策略的问题。

实现多realm认证

在上篇教程的基础上我们来完成此案例shiro教程5(整合SSM项目-认证)

首先MD5和SHA1加密简单实现

SHA1算法

public static void main(String[] args) {
\t// 算法引入的是 org.apache.shiro.crypto.hash.Sha1Hash
\t// shiro中提供的jar包
\t// SHA1算法 原始密码 盐值 加密次数
\tSha1Hash sha1= new Sha1Hash("123456", "aaa", 2);
\tSystem.out.println(sha1);
\t// 输出结果是:61c47830088c7355a7a2ac0106137632f2476bea
}

MD5算法

@Test
public void Md5Test() {
\t// 对单个信息加密
\tMd5Hash md5 = new Md5Hash("123456");
\tSystem.out.println(md5.toString());
\t// 加密添加盐值 增大解密难度
\tmd5 = new Md5Hash("123456","aaa");
\tSystem.out.println(md5.toString());
\t// 加密添加盐值 增大解密难度 2迭代两次
\tmd5 = new Md5Hash("123456","aaa",2);
\tSystem.out.println(md5);
\t// 输出结果:a7cf41c6537065fe724cc9980f8b5635
}

模拟数据

users表中存储的数据是MD5加密的数据

shiro教程6(多realm认证)

users1表中存储的数据是SHA1加密的数据

shiro教程6(多realm认证)

项目实现多Realm认证

mapper接口

public interface UsersMapper {

\t/**
\t * 查询users表
\t * @param userName
\t * @return
\t */
\tpublic Users queryUserByName(String userName);
\t
\t/**
\t * 查询users1表
\t * @param userName
\t * @return
\t */
\tpublic Users queryUserByNameSHA1(String userName);
}

mapper映射文件



<mapper>

\t<select>
\t\tselect * from users
\t\twhere username = #{param1}
\t/<select>

\t<select>
\t\tselect * from users1
\t\twhere username = #{param1}
\t/<select>
/<mapper>

service处理

public interface IUsersService {

\tpublic Users login(String userName);
\t
\tpublic Users loginSha1(String userName);

}
@Service
public class UsersServiceImpl implements IUsersService {
\t
\t@Resource
\tprivate UsersMapper mapper;

\t@Override
\tpublic Users login(String userName) {
\t\t// TODO Auto-generated method stub
\t\treturn mapper.queryUserByName(userName);
\t}

\t@Override
\tpublic Users loginSha1(String userName) {
\t\t// TODO Auto-generated method stub
\t\treturn mapper.queryUserByNameSHA1(userName);
\t}
}

添加一个自定义Realm文件

shiro教程6(多realm认证)

添加多Realm配置


<beans>\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
\txmlns:aop="http://www.springframework.org/schema/aop"
\txsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
\t\thttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
\t\thttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

\t
\t<bean>
\t\t
\t\t<property>
\t\t\t
\t\t\t<bean>
\t\t\t\t
\t\t\t\t<property>
\t\t\t\t
\t\t\t\t<property>
\t\t\t/<bean>
\t\t/<property>
\t/<bean>
\t
\t<bean>
\t\t
\t\t<property>
\t\t\t
\t\t\t<bean>
\t\t\t\t
\t\t\t\t<property>
\t\t\t\t

\t\t\t\t<property>
\t\t\t/<bean>
\t\t/<property>
\t/<bean>

\t
\t<bean>\t\tid="securityManager">
\t\t<property>
\t\t\t<bean>
\t\t\t\t<property>
\t\t\t\t\t
\t\t\t\t\t<bean>
\t\t\t\t/<property>
\t\t\t/<bean>
\t\t/<property>
\t\t
\t\t<property>
\t\t\t<list>
\t\t\t\t
\t\t\t\t
\t\t\t/<list>
\t\t/<property>
\t/<bean>

\t
\t<bean>\t\tid="shiro">
\t\t
\t\t<property>
\t\t
\t\t<property>
\t\t
\t\t<property>
\t\t

\t\t<property>
\t\t
\t\t<property>
\t\t\t<value>
\t\t\t\t/login.do=authc
\t\t\t\t/**=anon
\t\t\t/<value>
\t\t/<property>
\t/<bean>
/<beans>
shiro教程6(多realm认证)

shiro教程6(多realm认证)

shiro教程6(多realm认证)

shiro教程6(多realm认证)

shiro教程6(多realm认证)

测试

shiro教程6(多realm认证)



分享到:


相關文章: