shiro_spring.xml

本文介绍如何使用Spring框架整合MyBatis实现数据访问层,并结合Shiro进行权限管理和认证,包括配置数据源、事务管理、权限过滤等关键步骤。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    <!--引入配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--进行包扫描-->
    <context:component-scan base-package="com.service.*">

    </context:component-scan>
    <!--数据库连接池的配置-->
    <bean id="druiddataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--创建SQLsessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="druiddataSource"></property>
        <!--扫描mapper-->
        <property name="mapperLocations" value="classpath:com/dao/impl/*Mapper.xml"></property>
        <property name="typeAliasesPackage" value="classpath:com.entity"></property>
    </bean>
    <!--dao-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.dao"></property>

    </bean>
    <!--创建事务管理逻辑-->
    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" value="druiddataSource"></property>
    </bean>
    <!--创建事务-->
    <tx:annotation-driven></tx:annotation-driven>

    <!--创建shiro的安全管理器-->


    <!-- 创建凭证器 -->
    <bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
        <property name="hashAlgorithmName" value="MD5"></property>
        <property name="hashIterations" value="2615"></property>
    </bean>

    <!-- 配置自定义relam -->
    <bean id="nameRealm" class="com.shiro.CustomRealm">
        <property name="credentialsMatcher" ref="credentialsMatcher"></property>
    </bean>

    <!-- 会话管理对象 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    </bean>

    <!-- web环境下的securityManager 安全管理器-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--
            会话管理属性  非web环境 shiro session
                        web环境 HttpSession
        -->
        <property name="sessionManager" ref="sessionManager" ></property>
        <property name="realm" ref="nameRealm">
        </property>
        <!--<property name="realms">
            <list>
                <ref local="nameRealm"/>

            </list>
        </property>-->
    </bean>
    <!--在Spring的集成文件中 创建ShiroFilter 对资源做到安全控制。-->
    <bean id="shiroFilterFactoryBean" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--shiro核心安全管理器-->
        <property name="securityManager" ref="securityManager"></property>
        <!--登录的url 默认是/login.jsp-->
        <property name="loginUrl" value="/login.jsp"></property>
        <property name="successUrl" value="/index.jsp"></property>
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                /regist.jsp=anon
                /css/** = anon
                /js/** = anon
                /vcode=anon
                /adm/** = anon
                /logout = logout
                /** = authc

            </value>
        </property>
    </bean>
</beans>
<think>好的,我现在要解决用户提到的Spring Boot中Shiro配置导致的BeanCreationException异常问题。首先,我需要回忆一下相关的知识,并查看用户提供的引用内容,看看是否有相关的线索。 用户提供的引用中,引用[1]提到了BeanCreationException,具体是在创建AnnotationMethodHandlerAdapter时失败,可能和类加载有关。引用[3]指出版本不一致的问题,比如Jackson和Spring版本不兼容。引用[4]提到ClassNotFoundException,特别是javax.servlet.ServletContext缺失,这可能和依赖项未正确引入有关。 现在,用户的问题是关于ShiroShiroFilterFactoryBean导致的BeanCreationException,并且涉及到javax.servlet.Filter的错误。首先,我应该考虑常见的Shiro配置问题。比如,Shiro的过滤器需要在Spring Boot中正确配置,可能涉及到依赖冲突或版本不兼容。 首先,检查依赖版本是否兼容。ShiroSpring Boot的版本需要匹配。例如,如果使用较新的Spring Boot版本,可能需要对应的Shiro版本。同时,Shiro可能依赖于Servlet API,所以需要确保相关的Servlet依赖存在,特别是在Spring Boot中可能需要添加spring-boot-starter-web,因为它会提供Servlet相关的依赖。 引用[4]提到javax.servlet.ServletContext找不到,这通常是因为缺少相关的依赖。在Spring Boot中,通常通过spring-boot-starter-web来引入Servlet相关的类。如果用户的项目中没有这个依赖,或者版本不对,可能导致这个问题。所以需要检查pom.xml或build.gradle文件中是否包含了spring-boot-starter-web。 另外,引用[3]提到版本不一致的问题,比如Jackson和Spring版本。类似地,Shiro的依赖可能和Spring Boot的版本有冲突。需要确认Shiro的版本是否兼容当前使用的Spring Boot版本。例如,如果用户使用的是Spring Boot 2.x,可能需要Shiro 1.7或更高版本。 接下来,ShiroFilterFactoryBean的配置是否正确。在Spring Boot中,配置Shiro的过滤器通常需要创建一个ShiroFilterFactoryBean的Bean,并定义相应的过滤规则。如果配置不当,比如URL路径不正确,或者过滤器链顺序有问题,可能导致Bean创建失败。 另外,可能涉及到Shiro的依赖是否正确引入。例如,可能需要添加shiro-spring依赖来与Spring整合。如果依赖缺失,或者版本不正确,可能导致Bean无法创建。 另外,考虑到用户提到的异常涉及javax.servlet.Filter,可能需要检查是否Shiro的过滤器类正确继承了Servlet的Filter接口,并且相关的类在类路径中可用。如果Shiro的依赖中存在Servlet API的版本冲突,或者没有正确引入,可能导致ClassNotFound或NoClassDefFoundError。 还有可能是在配置Shiro时,某些Bean的依赖没有正确注入。例如,如果ShiroFilterFactoryBean需要依赖于SecurityManager,而SecurityManager没有正确配置,也可能导致Bean创建异常。 总结可能的解决步骤: 1. 检查依赖版本:确保ShiroSpring Boot及相关库(如Servlet API、Jackson)的版本兼容。 2. 添加必要的依赖:如spring-boot-starter-web、shiro-spring等。 3. 检查Shiro配置类:确保ShiroFilterFactoryBean和SecurityManager等Bean正确配置。 4. 检查过滤器链配置:确保URL规则和过滤器顺序正确。 5. 查看完整堆栈信息:确定具体的错误位置,比如是否有缺失的类或初始化失败的具体原因。 可能需要参考用户提供的引用[3]中的解决方案,处理版本兼容性问题,或者引用[4]中提到的添加Servlet依赖。此外,还需查看具体的异常堆栈,确定是哪个类找不到,或者哪个Bean初始化失败,从而更精准地解决问题。</think>### Spring Boot中Shiro配置导致BeanCreationException的解决方案 #### 1. **检查依赖版本兼容性** - **ShiroSpring Boot版本匹配**:确保使用的Shiro版本与Spring Boot兼容。例如: - Spring Boot 2.x建议使用Shiro 1.7+。 - Spring Boot 1.5.x可使用Shiro 1.4.x。 - 若版本不匹配,需升级或降级依赖[^3]。 - **添加必要依赖**: - 引入`shiro-spring`整合包: ```xml <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> ``` - 确保包含`spring-boot-starter-web`以提供Servlet支持[^4]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` #### 2. **配置ShiroFilterFactoryBean** - **定义SecurityManager**:需先配置`SecurityManager` Bean: ```java @Bean public SecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(yourRealmBean); // 注入自定义Realm return securityManager; } ``` - **配置过滤器链**: ```java @Bean public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean(); factoryBean.setSecurityManager(securityManager); factoryBean.setLoginUrl("/login"); factoryBean.setUnauthorizedUrl("/403"); Map<String, String> filterChainMap = new LinkedHashMap<>(); filterChainMap.put("/static/**", "anon"); filterChainMap.put("/login", "anon"); filterChainMap.put("/**", "authc"); factoryBean.setFilterChainDefinitionMap(filterChainMap); return factoryBean; } ``` - **注意**:若未正确关联`SecurityManager`或URL规则冲突,会导致`BeanCreationException`。 #### 3. **解决Servlet相关依赖缺失** - **添加Servlet API依赖**:若异常包含`ClassNotFoundException: javax.servlet.Filter`,需显式引入Servlet API: ```xml <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> ``` - **检查依赖冲突**:使用`mvn dependency:tree`排查重复或冲突的Servlet API包[^4]。 #### 4. **排查其他兼容性问题** - **Jackson与Spring版本**:如引用所述,若存在JSON序列化异常,需确保Jackson版本与Spring Boot兼容。例如: - Spring Boot 2.5.x对应Jackson 2.12.x。 - **启用调试日志**:在`application.properties`中添加: ```properties logging.level.org.springframework=DEBUG logging.level.apache.shiro=DEBUG ``` 通过日志定位初始化失败的具体位置。 #### 5. **示例配置修正** ```java @Configuration public class ShiroConfig { @Bean public Realm realm() { return new CustomRealm(); // 自定义Realm实现 } @Bean public SecurityManager securityManager(Realm realm) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(realm); return securityManager; } @Bean public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean(); factoryBean.setSecurityManager(securityManager); // 其他配置... return factoryBean; } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值