IDEA完成shiro认证报错:org.apache.shiro.config.ConfigurationException: java.io.IOException: Resource
最近跟着视频学 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 {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用户登陆和退出</span>
@Test
public void testLoginAndLogout() {</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建securityManager工厂,通过ini配置文件创建securityManager工厂 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.创建一个安全管理器的工厂</span> Factory<SecurityManager> factory = <span style="color: rgba(0, 0, 255, 1)">new</span> IniSecurityManagerFactory("classpath:shiro-first.ini"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2.在工厂中获取安全管理器</span> SecurityManager securityManager =<span style="color: rgba(0, 0, 0, 1)"> factory.getInstance(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3.将securityManager绑定到运行环境</span>
SecurityUtils.setSecurityManager(securityManager);
//4. 获取 Subject 对象 (将要登录的用户)
Subject subject = SecurityUtils.getSubject();
//5. 获取要登录用户的 token, 客户端传递过来的用户名和密码
String username = "zhangsan",password="123456";
UsernamePasswordToken token = new UsernamePasswordToken(username,password);subject.logout(); System.out.println("执行推出操作"); System.out.println("是否认证通过:"+subject.isAuthenticated());</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ subject.login(token); }</span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (AuthenticationException e){ e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">boolean</span> isAuthenticated =<span style="color: rgba(0, 0, 0, 1)"> subject.isAuthenticated(); System.out.println(</span>"是否认证通过:"+<span style="color: rgba(0, 0, 0, 1)">isAuthenticated); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">执行退出操作</span>
}
}
文件目录
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><span style="color: rgba(0, 0, 255, 1)"></</span><span style="color: rgba(128, 0, 0, 1)">resources</span><span style="color: rgba(0, 0, 255, 1)">></span></pre>
现在知道了吧: 添加了 ini 文件后, 重启 IDE 即可 (这样就可以资源目录文件编译的时候复制到 classpath 了)
<include>**/*.ini</include>
转 : https://blog.csdn.net/a1106103430/article/details/86519355