实际开发中存在这样一种场景,同一个密码可能在MqSQL中存储,也可能在Oracle中存储,有可能MqSQL中使用的是MD5加密算法,而Oracle使用SHA1加密算法。这就需要有多个Realm以及认证策略的问题。
在上篇教程的基础上我们来完成此案例shiro教程5(整合SSM项目-认证)
public static void main(String[] args) {
// 算法引入的是 org.apache.shiro.crypto.hash.Sha1Hash
// shiro中提供的jar包
// SHA1算法 原始密码 盐值 加密次数
Sha1Hash sha1= new Sha1Hash("123456", "aaa", 2);
System.out.println(sha1);
// 输出结果是:61c47830088c7355a7a2ac0106137632f2476bea
}
@Test
public void Md5Test() {
// 对单个信息加密
Md5Hash md5 = new Md5Hash("123456");
System.out.println(md5.toString());
// 加密添加盐值 增大解密难度
md5 = new Md5Hash("123456","aaa");
System.out.println(md5.toString());
// 加密添加盐值 增大解密难度 2迭代两次
md5 = new Md5Hash("123456","aaa",2);
System.out.println(md5);
// 输出结果:a7cf41c6537065fe724cc9980f8b5635
}
users表中存储的数据是MD5加密的数据
users1表中存储的数据是SHA1加密的数据
public interface UsersMapper {
/**
* 查询users表
* @param userName
* @return
*/
public Users queryUserByName(String userName);
/**
* 查询users1表
* @param userName
* @return
*/
public Users queryUserByNameSHA1(String userName);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dpb.mapper.UsersMapper">
<select id="queryUserByName" resultType="users">
select * from users
where username = #{param1}
</select>
<select id="queryUserByNameSHA1" resultType="users">
select * from users1
where username = #{param1}
</select>
</mapper>
public interface IUsersService {
public Users login(String userName);
public Users loginSha1(String userName);
}
@Service
public class UsersServiceImpl implements IUsersService {
@Resource
private UsersMapper mapper;
@Override
public Users login(String userName) {
// TODO Auto-generated method stub
return mapper.queryUserByName(userName);
}
@Override
public Users loginSha1(String userName) {
// TODO Auto-generated method stub
return mapper.queryUserByNameSHA1(userName);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 注册自定义Realm -->
<bean class="com.dpb.realm.MyRealm" id="myRealm">
<!-- 配置凭证匹配器 -->
<property name="credentialsMatcher">
<!-- 定义凭证匹配器 -->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 配置散列算法 -->
<property name="hashAlgorithmName" value="MD5" />
<!-- 配置散列次数 -->
<property name="hashIterations" value="2" />
</bean>
</property>
</bean>
<!-- 注册第二个Realm -->
<bean class="com.dpb.realm.MyRealmSha1" id="myRealmSha1">
<!-- 配置凭证匹配器 -->
<property name="credentialsMatcher">
<!-- 定义凭证匹配器 -->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 配置散列算法 -->
<property name="hashAlgorithmName" value="SHA1"/>
<!-- 配置散列次数 -->
<property name="hashIterations" value="2" />
</bean>
</property>
</bean>
<!-- 注册SecurityManager -->
<bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"
id="securityManager">
<property name="authenticator">
<bean class="org.apache.shiro.authc.pam.ModularRealmAuthenticator" >
<property name="authenticationStrategy" >
<!-- 至少有一个Realm认证通过 -->
<bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"/>
</property>
</bean>
</property>
<!-- 配置自定义Realm -->
<property name="realms" >
<list>
<ref bean="myRealm"/>
<ref bean="myRealmSha1"/>
</list>
</property>
</bean>
<!-- 注册ShiroFilterFactoryBean 注意id必须和web.xml中注册的targetBeanName的值一致 -->
<bean class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"
id="shiro">
<!-- 注册SecurityManager -->
<property name="securityManager" ref="securityManager" />
<!-- 登录地址 如果用户请求的的地址是 login.do 那么会对该地址认证 -->
<property name="loginUrl" value="/login.do" />
<!-- 登录成功的跳转地址 -->
<property name="successUrl" value="/jsp/success.jsp" />
<!-- 访问未授权的页面跳转的地址 -->
<property name="unauthorizedUrl" value="/jsp/refuse.jsp" />
<!-- 设置 过滤器链 -->
<property name="filterChainDefinitions">
<value>
/login.do=authc
/**=anon
</value>
</property>
</bean>
</beans>