zoukankan      html  css  js  c++  java
  • 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 {
    
        // 用户登陆和退出
        @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

  • 相关阅读:
    mysql环境搭建
    php基础:查看上次插入的主键和影响的行数及关闭mysql连接
    php基础:文件包含与引用 require和include的区别
    php基础:echo和print_r和var_dump的区别
    php基础:变量检测
    php基础:动态变量名
    php基础:代码的短路特性和运算符优先级
    php基础:三元运算符及比较3个数的大小
    php基础:字符串基本函数
    php基础:数组的定义和遍历
  • 原文地址:https://www.cnblogs.com/fps2tao/p/13555132.html
Copyright © 2011-2022 走看看