IDEA完成shiro认证报错:org.apache.shiro.config.ConfigurationException: java.io.IOException: Resource
2023-09-11 14:19:38 时间
最近跟着视频学shiro,作为一个小白,刚开始就遇到了一个折腾半天都没解决的报错: org.apache.shiro.config.ConfigurationException: java.io.IOException: Resource [classpath:shiro-first.ini] could not be found.
大意上就是ini配置文件没有找到,这是代码:
package cn.itcast.shiro.authentication; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.junit.Test; public class AuthenticationTest { // 用户登陆和退出 @Test public void testLoginAndLogout() { // 创建securityManager工厂,通过ini配置文件创建securityManager工厂 //1.创建一个安全管理器的工厂 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini"); //2.在工厂中获取安全管理器 SecurityManager securityManager = factory.getInstance(); //3.将securityManager绑定到运行环境 SecurityUtils.setSecurityManager(securityManager); //4.获取Subject对象(将要登录的用户) Subject subject = SecurityUtils.getSubject(); //5.获取要登录用户的token,客户端传递过来的用户名和密码 String username = "zhangsan",password="123456"; UsernamePasswordToken token = new UsernamePasswordToken(username,password); try{ subject.login(token); }catch (AuthenticationException e){ e.printStackTrace(); } boolean isAuthenticated = subject.isAuthenticated(); System.out.println("是否认证通过:"+isAuthenticated); //执行退出操作 subject.logout(); System.out.println("执行推出操作"); System.out.println("是否认证通过:"+subject.isAuthenticated()); } }
文件目录
config文件夹没有设置成资源文件夹,接下来看一下在idea里如何设置成资源文件夹吧
右键config文件夹,找到Mark Directory as 单击Resources Root设置成资源文件夹,之后你的.ini配置文件就可以被IniSecurityManagerFactory访问到,最后看一下设置完成资源文件夹的控制台输出信息吧。
另一个因素(本人遇到的原因):
因为我的项目是maven项目,pom.xml有这样的设置:
<resources> <!--编译之后包含xml--> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.org</include> <include>**/*.txt</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.org</include> <include>**/*.txt</include> </includes> <filtering>true</filtering> </resource> </resources>
现在知道了吧: 添加了ini文件后,重启IDE即可 (这样就可以资源目录文件编译的时候复制到classpath了)
<include>**/*.ini</include>
转 : https://blog.csdn.net/a1106103430/article/details/86519355
相关文章
- Solr 报错:java.lang.NoClassDefFoundError: org/apache/http/entity/mime/content/ContentBody
- Java 小记 — RabbitMQ 的实践与思考
- 【JAVA】Eclipse中开启java和xml智能提示功能(图文,已解决!)
- Java Date Time 教程-java.sql.Date
- java.lang.OutOfMemoryError:GC overhead limit exceeded填坑心得
- Ubuntu安装java的几种方式以及多个JAVA版本的切换
- 【Oracle】ORACLE SQL Developer不支持JAVA版本
- Java-struts2的问题 java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
- 《Java EE 7精粹》—— 3.9 导航规则
- Apache Curator与Zookeeper版本兼容性异常:org.apache.zookeeper.KeeperException$UnimplementedException: KeeperErrorCode =Unimplemented
- Java面试通关要点汇总集之核心篇参考答案
- intellij idea 下用java Apache axis 创建WebService 服务端 过程
- elasticsearch之JAVA环境变量报错:could not find java; set JAVA_HOME or ensure java is in PATH
- Java之Logger日志(Java8特性)
- 【JAVA】选择排序
- Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/ma ven/cli/Maven
- Java异常处理错误
- 简单的JAVA MVC框架模式--Java-servlet-JavaBean
- java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
- eclipse.exe打开时提示:An error has occurred. See the log file E:Java eclipse Work metadatalog.
- 【Apache Flume】Apache Flume之Java API集成
- Java经纬读坐标的距离计算
- Java //PP2.1 修改第1章的程序Lincoln,使程序在名言外面显示引号
- Java 深入理解Java异常以及异常处理机制