shiro多realm抛出异常问题

简介: 使用Shiro作为安全框架时,为了方便我们可以把异常提出为公共模块,当使用多Realm时,需要特殊处理下,不然,异常时只会抛出一种。最近写项目时,就遇到了该问题,本身项目有一个登录系统,需要多加一个OAuth2的单点登录,服务端已经OK,需要在项目中对接一下该服务端;特此记录下
  • 既然是多Realm,我们需要在每个Realm中重写Support方法
//UserRealm 自身验证
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }
//SSO 单点登录验证
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof OAuth2SsoAuthenticationToken;
    }
  • 自定义一个类继承 ModularRealmAuthenticator,并重写doMultiRealmAuthentication方法
/**
 * @author yafengliang@yeah.net
 * @Description
 * @date 2022-08-18 9:18
 */
public class MultiRealmAuthenticator extends ModularRealmAuthenticator {

    private static final Logger log = LoggerFactory.getLogger(ModularRealmAuthenticator.class);


    @Override
    protected AuthenticationInfo doMultiRealmAuthentication(Collection<Realm> realms, AuthenticationToken token) {

        AuthenticationStrategy strategy = getAuthenticationStrategy();
        AuthenticationInfo authentication = strategy.beforeAllAttempts(realms, token);
        if (log.isTraceEnabled()) {
            log.trace("Iterating through {} realms for PAM authentication", realms.size());
        }
        AuthenticationException authenticationException = null;
        for (Realm realm : realms) {
            authentication = strategy.beforeAttempt(realm, token, authentication);
            if (realm.supports(token)) {
                log.trace("Attempting to authenticate token [{}] using realm [{}]", token, realm);
                AuthenticationInfo info = null;
                try {
                    info = realm.getAuthenticationInfo(token);
                   //因为默认使用的是AtLeastOneSuccessfulStrategy(),有一个Realm成功就可以
                  authenticationException = null;
                } catch (AuthenticationException e) {
                    authenticationException = e;
                    if (log.isDebugEnabled()) {
                        String msg = "Realm [" + realm + "] threw an exception during a multi-realm authentication attempt:";
                        log.debug(msg, e);
                    }
                }
                authentication = strategy.afterAttempt(realm, token, info, authentication, authenticationException);
            } else {
                log.debug("Realm [{}] does not support token {}.  Skipping realm.", realm, token);
            }
        }
        if (authenticationException != null) {
            throw authenticationException;
        }
        authentication = strategy.afterAllAttempts(token, authentication);
        return authentication;

    }
}
  • 在shiro的配置类中注入自定义类
@Bean
    public AbstractAuthenticator authenticator(UserRealm userRealm, OAuth2Realm oAuth2Realm){
        ModularRealmAuthenticator authenticator = new MultiRealmAuthenticator();
        authenticator.setRealms(Arrays.asList(userRealm,oAuth2Realm));
        authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        return authenticator;
    }
  • 在securityManager中加入多realm
    /**
     * 安全管理器
     */
    @Bean
    public SecurityManager securityManager(UserRealm userRealm, OAuth2Realm oAuth2Realm,AbstractAuthenticator authenticator) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 设置realm.
        // securityManager.setRealm(userRealm);
        securityManager.setRealms(Arrays.asList(userRealm, oAuth2Realm));
        // 注入缓存管理器;
        securityManager.setCacheManager(getEhCacheManager());
        // session管理器
        securityManager.setSessionManager(sessionManager());
        //解决多realm的异常问题重点在此
        securityManager.setAuthenticator(authenticator);

        return securityManager;
    }
  • 再次运行解决

感谢 https://blog.csdn.net/qq_46416934/article/details/124021822
感谢 https://blog.csdn.net/m0_67403188/article/details/124019651

目录
相关文章
|
网络安全 API 对象存储
SSL证书过期替换之踩坑总结
本文简要列出了阿里云ssl证书更新涉及到的服务及修改截图,供大家参考、补充和完善。
8366 0
|
6月前
|
JSON 算法 开发工具
HarmonyOS NEXT实战:通过QQ分享内容
本教程介绍如何在HarmonyOS Next项目中接入QQ SDK实现分享功能,包含依赖配置、签名生成及分享逻辑代码,适用于教育类应用的内容分享场景。
306 0
|
12月前
|
存储 小程序 Python
农历节日倒计时:基于Python的公历与农历日期转换及节日查询小程序
### 农历节日倒计时:基于Python的公历与农历日期转换及节日查询小程序 该程序通过`lunardate`库实现公历与农历的日期转换,支持闰月和跨年处理,用户输入农历节日名称后,可准确计算距离该节日还有多少天。功能包括农历节日查询、倒计时计算等。欢迎使用! (239字符)
762 86
|
9月前
|
SQL 安全 网络安全
网络安全防御矩阵:从云防火墙流量清洗到WAF语义分析的立体化防护
在数字化浪潮中,网络安全日益重要。云防火墙依托云计算技术,提供灵活高效的网络防护,适用于公有云和私有云环境;Web应用防火墙专注于HTTP/HTTPS流量,防范SQL注入、XSS等攻击,保护Web应用安全。两者结合使用可实现优势互补,构建更强大的网络安全防线,满足不同场景下的安全需求。
406 1
|
JSON 小程序 JavaScript
|
12月前
|
存储 开发者 容器
|
11月前
|
数据采集 存储 Serverless
5 分钟复刻你的声音,一键实现 GPT-Sovits 模型部署
想象一下,只需简单几步操作,就能生成逼真的语音效果,无论是为客户服务还是为游戏角色配音,都能轻松实现。GPT-Sovits 模型,其高效的语音生成能力为实现自然、流畅的语音交互提供了强有力的技术支持。本文将详细介绍如何利用函数计算平台部署 GPT-Sovits 模型,以构建一个高效、可扩展的 AI 语音交互系统。通过这一部署方案,开发者和企业能够快速集成语音合成功能,实现从文本到语音的无缝转换,进而推动智能语音应用的创新和发展。
2033 11
|
数据库 数据安全/隐私保护
Shiro【自定义Realm 、多Realm认证 、多Realm认证策略、异常处理】(二)-全面详解(学习总结---从入门到深化)
Shiro【自定义Realm 、多Realm认证 、多Realm认证策略、异常处理】(二)-全面详解(学习总结---从入门到深化)
890 0
|
小程序
Flutter CustomScrollView 效果-顶栏透明与标签栏吸顶
Flutter CustomScrollView 效果-顶栏透明与标签栏吸顶
Shiro用户鉴权框架 子线程获取不到用户信息问题解决
Shiro用户鉴权框架 子线程获取不到用户信息问题解决