Shiro与Spring结合时报Configuration error: No realms have been configured! One or more realms must be……

本文主要是介绍Shiro与Spring结合时报Configuration error: No realms have been configured! One or more realms must be……,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
最近在跟着网上的教学视频学习Shiro安全框架,使用Shiro可以方便的做验证、授权等,其中在谈到使用多realm做验证,授权时,报了个缺少realm配置的错误。
Configuration error: No realms have been configured! One or more realms must be present to execute an authorization operation.] with root cause

为什么我会特别写出来这个问题呢?

其实这个问题我当时就解决了,更换了写realms属性的地方,但问题是在学习多realm身份验证时这两个地方都可以写realms属性,偏偏在经过了身份验证,再进行授权时却出现No realms have been configured!。
同时,我也看了下其他的博客,相同问题解决的办法就是securityManager里面authenticator的属性要放在realms属性前面,我尝试了在securityManager里面authenticator的属性放在了realms属性后面,身份验证失败,但是并没有报错。我就更加疑惑了……
没学过Shiro框架的人需要对realm概念先有个大致了解:
realm:就目前我的认识来简单描述下,realm就是一个验证器或者叫关卡,你输入username和password,生成一个UsernamePassswordToken,然后这个token给了某个realm,经过它的比对,通过就验证成功,不通过就验证失败。
多realm验证就可以理解成想要完成身份验证,需要通过多个realm的验证,可能每个realm都有自己的验证策略,最终是否验证成功的判断默认的有三种,比如通过全部的realm验证才算真正通过身份验证,或者通过任意一个realm也算是通过身份验证。

我先把主要的代码陈列下,做几个对比分析下:

applicationConetxt.xml配置文件

1.realms属性直接配置在securityManager里面

1).realms属性是在authenticator属性后面,身份验证、授权完全没有问题
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><property name="authenticator" ref="authenticator"/><property name="realms"><list><ref bean="jdbcRealm"></ref><ref bean="secondRealm"></ref></list></property></bean><!--多Realm认证--><bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"><!--认证策略--><property name="authenticationStrategy"><bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean></property></bean>
2).realms属性是在authenticator属性前面,经测试,身份验证不通过,但不会报错
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><property name="realms"><list><ref bean="jdbcRealm"></ref><ref bean="secondRealm"></ref></list></property><property name="authenticator" ref="authenticator"/></bean><!--多Realm认证--><bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"><!--认证策略--><property name="authenticationStrategy"><bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean></property></bean>

2.realms属性不配置在securityManager里面,而配置在authenticator里面

经过测试,身份验证时没有问题

 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="cacheManager" ref="cacheManager"/><property name="authenticator" ref="authenticator"/></bean><!--多Realm认证--><bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator"><property name="realms"><list><ref bean="jdbcRealm"></ref><ref bean="secondRealm"></ref></list></property><!--认证策略--><property name="authenticationStrategy"><bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean></property></bean>

但在授权时就出现了报错:java.lang.IllegalStateException: Configuration error: No realms have been configured! One or more realms must be present to execute an authorization operation.

翻看了源代码发现:
报错的类路径为:

org.apache.shiro.authc.pam.ModularRealmAuthenticator#assertRealmsConfigured

在这里插入图片描述
我最大的疑惑来了……在做多realm身份验证时通过了,就证明配置在authenticator的多个realm确实被加载了,否则在身份验证时就应该报错!
既然能加载多realm,授权时为何又报错?

目前还没找到问题所在,先记在这里,害怕之后就忘了……

这篇关于Shiro与Spring结合时报Configuration error: No realms have been configured! One or more realms must be……的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/162808

相关文章

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过

Springboot的配置文件及其优先级说明

《Springboot的配置文件及其优先级说明》文章介绍了SpringBoot的配置文件,包括application.properties和application.yml的使用,以及它们的优先级,还讨... 目录配置文件内置配置文件yml与properties的比较优先级比较外置配置文件springboot

自定义注解SpringBoot防重复提交AOP方法详解

《自定义注解SpringBoot防重复提交AOP方法详解》该文章描述了一个防止重复提交的流程,通过HttpServletRequest对象获取请求信息,生成唯一标识,使用Redis分布式锁判断请求是否... 目录防重复提交流程引入依赖properties配置自定义注解切面Redis工具类controller