shiro【多Realm认证】

为什么要使用多realm认证?

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

实现多realm认证

ssm-shiro的基础上

首先MD5和SHA1加密简单实现

sha1算法

public static void main(String[] args) {
	// 算法引入的是 org.apache.shiro.crypto.hash.Sha1Hash
	// shiro中提供的jar包
	// SHA1算法      			原始密码   盐值   加密次数 
	Sha1Hash sha1= new Sha1Hash("123456", "aaa", 1024);
	System.out.println(sha1);
}

在这里插入图片描述

MD5算法

public void Md5Test() {
	// 对单个信息加密
	Md5Hash md5 = new Md5Hash("123456","aaa",1024);
	System.out.println(md5);
}

在这里插入图片描述

项目实现多Realm认证

mapper接口

package com.sxt.mapper;

import java.util.List;
import com.sxt.pojo.User;

public interface UserMapper {

	public List<User> query(String username);
	
	public List<User> queryForSha1(String username);
}

mapper映射文件

<?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.sxt.mapper.UserMapper">
	<select id="query" resultType="user">
		select * from users where username=#{param1}
	</select>
	
	<select id="queryForSha1" resultType="user">
		select * from users1 where username=#{param1}
	</select>
</mapper>

service处理

在这里插入图片描述
在这里插入图片描述

添加一个自定义Realm文件

在这里插入图片描述
在这里插入图片描述

添加多Realm配置

applicationContext.xml

<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" xmlns:tx="http://www.springframework.org/schema/tx"
	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
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	
	<!-- 配置自定义的Realm MD5 -->
	<bean class="com.sxt.realm.SecurityRealm" id="securityRealmMD5">
		<!-- 配置对应的匹配器 -->
		<property name="credentialsMatcher" >
			<!-- 配置凭证匹配器 -->
			<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
				<!-- 指定散列算法和迭代次数 -->
				<property name="hashAlgorithmName" value="md5" />
				<property name="hashIterations" value="1024" />
			</bean>
		</property>
	</bean>
	
	<!-- 配置自定义的Realm Sha1 -->
	<bean class="com.sxt.realm.SecurityForSha1Realm" id="securityRealmSha1">
		<!-- 配置对应的匹配器 -->
		<property name="credentialsMatcher" >
			<!-- 配置凭证匹配器 -->
			<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
				<!-- 指定散列算法和迭代次数 -->
				<property name="hashAlgorithmName" value="sha1" />
				<property name="hashIterations" value="1024" />
			</bean>
		</property>
	</bean>
	
	<!-- 配置SecurityManager -->
	<bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager">
		<!-- 配置多Realm的认证策略 -->
		<property name="authenticator">
			<bean class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
				<property name="authenticationStrategy"> 
					<!-- 至少有一个Realm认证通过 -->
					<!-- <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"/> -->
					<!-- 全部Realm认证通过 -->
					<bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"/>
				</property>
			</bean>
		</property>
 		<!-- 关联配置多个自定义Realm -->
 		<property name="realms">
 			<list>
 				<ref bean="securityRealmMD5"/>
 				<ref bean="securityRealmSha1"/>
 			</list>
 		</property>
 	</bean>
 	
 	<!-- 注册ShiroFilterFactoryBean id必须要和web.xml文件中的targetName一致 -->
 	<bean id="shiro" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
 		<!-- 注册SecurityManager -->
 		<property name="securityManager" ref="securityManager"/>
 		<!-- 登录地址 如果用户请求的的地址是 login.do 那么会对该地址认证-->
 		<property name="loginUrl" value="/login.do"/>
 		<!-- 登录成功的跳转地址 -->
 		<property name="successUrl" value="/success.jsp"/>
 		<!-- 访问未授权的页面跳转的地址 -->
 		<property name="unauthorizedUrl" value="/refuse.jsp"/>
 		<!-- 设置 过滤器链 -->
 		<property name="filterChainDefinitions">
 			<value>
 				<!--加载顺序从上往下。
 					authc需要认证
 					anon可以匿名访问的资源
 				 -->
 				/login.do=authc
				/login.jsp=anon
 				/**=authc
 			</value>
 		</property>
 	</bean>

</beans>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值