最近跟着视频学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 ( ) {
Factory< SecurityManager> factory = new IniSecurityManagerFactory (
"classpath:shiro-first.ini" ) ;
SecurityManager securityManager = factory. getInstance ( ) ;
SecurityUtils. setSecurityManager ( securityManager) ;
Subject subject = SecurityUtils. getSubject ( ) ;
UsernamePasswordToken token = new UsernamePasswordToken ( "zhangsan" , "111111" ) ;
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 ( ) ) ;
}
}
然后我们来看一下文件目录:
刚开始接触idea不久,可以说是完全看不出哪里有问题吧,后来一个大牛告诉我是config文件夹没有设置成资源文件夹,接下来看一下在idea里如何设置成资源文件夹吧
右键config文件夹,找到Mark Directory as 单击Resources Root设置成资源文件夹,之后你的.ini配置文件就可以被IniSecurityManagerFactory访问到,最后看一下设置完成资源文件夹的控制台输出信息吧。