SpringBoot 与Shiro 整合系列(三)多Realm验证和认证策略

本文介绍了SpringBoot与Shiro整合实现多Realm验证的场景和配置过程,包括添加SecondRealm,修改ShiroConfig配置。同时,详细探讨了认证策略的概念,包括默认的AtLeastOneSuccessfulStrategy和如何切换到AllSuccessfulStrategy,以及它们在多Realm认证中的应用和效果。
摘要由CSDN通过智能技术生成

系列(三)讲述 SpringBoot整合Shiro 实现多Realm验证以及认证策略


shiro是一个很好的登陆以及权限管理框架,但是默认是单realm单数据表,但是很多业务需求单realm是很难实现登陆以及权限管理的功能(比如业务中用户分布在不同的数据表),这个时候就需要使用多个Realm。

一、使用场景

存在这样一种场景,我们可能会把安全数据放到不同的数据库,比方说 mysql里有,oracle里也有,mysql里面的加密算法是MD5,oracle里面的加密算法是SHA1。这个时候我们进行用户认证时,就需要同时访问这两个数据库,就需要多个Realm。如果有多个Realm的话,还需要涉及到认证策略的问题。

二、多Realm验证

多重认证,主要的类是ModularRealmAuthenticator他有两个需要配置的属性,一个是Collection(用于存储Realm),另一个是AuthenticationStrategy(用于存储验证的策略 )

通过查看源码可以看到 ModularRealmAuthenticator.class 中的 doAuthenticate方法:

    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
   
        assertRealmsConfigured();
        Collection<Realm> realms = getRealms();
        if (realms.size() == 1) {
   //一个realm
            return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
        } else {
   //多个realm
            return doMultiRealmAuthentication(realms, authenticationToken);
        }
    }

从上面doAuthenticate方法中可以看到:

如果有一个Realm 使用的是:

  • doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);

如果有多个Realm 使用的是:

  • doMultiRealmAuthentication(realms, authenticationToken);

所以我们可以配置多个Realm 给到 ModularRealmAuthenticator 这个bean,将ModularRealmAuthenticator 单独配置为一个bean,将这个bean 配置给SecurityManager。

配置流程:

1.在原有代码的基础上,添加第二个Realm SecondRealm.java

SecondRealm.java 中的 加密算法 为 SHA1

package com.koncord.shiro;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.koncord.model.User;
import com.koncord.service.UserService;

/**
 * 自定义 Realm
 * @author Administrator
 *
 */
public class SecondRealm extends AuthorizingRealm{
   

	@Autowired
	private UserService userService;
	
	/**
	 * 执行授权逻辑
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
   
		System.out.println("SecondRealm 执行授权逻辑");
		//给资源进行授权
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		//添加资源的授权字符串
		//info.addStringPermission("user:add");
		
		//导数据库查询当前登录用户的授权字符串
		//获取当前登录用户
		Subject subject=SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal(); 
		
		User sbUser=userService.findUserByName(user.getName());
		
		info.addStringPermission(sbUser.getPerms());
		return info;
	}

	/**
	 * 执行认证逻辑
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
   
		System.out.println("SecondRealm 执行认证逻辑")
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值