shiro整合springMVC,Hibernate,mybaits等出现问题

小白白哈哈 发布于 2017/04/06 21:17
阅读 263
收藏 0

以前用的Spring的Security在进行权限管理,现在换成shiro时,在自定义的realm中@autowired service类时会导致,service中被调用的方法所用的dao为空,加载不进来。这是什么原因,怎么解决,求大神解答!

加载中
0
MnameHZJ
MnameHZJ
realm有在spring的xml配置文件里定义吗?或者realm类上有写spring的容器注解(比如@Service ,@Component)吗?
MnameHZJ
MnameHZJ
realm类有在spring扫描包下吗
小白白哈哈
小白白哈哈
已经在Spring的xml配置文件里定义了,而且写了spring的@service注解还是dao为空,
0
风翔飞
风翔飞

dao为空那就是mybatis的问题,你确定初始化shiro的时候dao扫描了吗?

小白白哈哈
小白白哈哈
@风翔飞 回复@风翔飞 : <description>shiro配置</description> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="...
风翔飞
风翔飞
回复 @小白白哈哈 : 是在shiro初始化的文件里扫描的吗?这个bug可能和初始化顺序有关,最好贴下代码
小白白哈哈
小白白哈哈
dao包扫描了
0
小白白哈哈
小白白哈哈
<description>shiro配置</description>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/login"/>
    <property name="successUrl" value="/index"/>
    <property name="unauthorizedUrl" value="redirect:/login"/>
    <property name="filterChainDefinitions">
        <value>
            /login = anon
            /index= anon
            /** =anon
        </value>
    </property>
</bean>
<!--shiro核心-securityManager-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myrealm"/>
    <property name="rememberMeManager" ref="rememberMeManager" />
</bean>
<!--单个realm,如果是多个换成realms-->
<bean id="myrealm" class="com.dsdl.eidea.base.web.shiro.UserRealm"></bean>
<!-- 后置处理程序自动调用init()和destroy()方法 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- Shiro支持对方法级别的权限控制 -->
<bean
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
    <property name="proxyTargetClass" value="true" />
</bean>

<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager" />
</bean>

<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
    <property name="cipherKey"
              value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}" />

    <!-- Shiro自身提供Cookie的实现有以下2种.任选其一. 大多数采用rememberMeCookie的实现。 -->
    <property name="cookie" ref="rememberMeCookie" />
    <!-- <property name="cookie" ref="sessionIdCookie"/> -->
</bean>

<!-- 会话Cookie模板-->
<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
    <constructor-arg value="sid" />
    <property name="httpOnly" value="true" />
    <property name="maxAge" value="-1" />
</bean>
<!-- 即记住我的Cookie,保存时长1小时 -->
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
    <constructor-arg value="rememberMe" />
    <property name="httpOnly" value="true" />
    <property name="maxAge" value="3600" /><!-- 1小时,秒为单位 -->
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager" />
    <property name="arguments" ref="securityManager" />
</bean>

spring中扫描

<context:component-scan base-package="com.dsdl.eidea.core.spring.processor"/>
<context:component-scan base-package="com.dsdl.eidea.**.dao"/>
<context:component-scan base-package="com.dsdl.eidea.core.init"/>
<context:component-scan base-package="com.dsdl.eidea.**.service"/>
<context:component-scan base-package="com.dsdl.eidea.core.web.converter"/>
风翔飞
风翔飞
在哪加载的spring.xml?web.xml也贴下
0
小白白哈哈
小白白哈哈
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:application-web.xml
        classpath:spring-mvc.xml
        classpath:spring-shiro.xml
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
</listener>
<listener>
    <listener-class>com.dsdl.eidea.base.web.listener.OnLineUserListener</listener-class>
</listener>
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!--shiro监听器开始-->
<filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!--shiro监听器结束-->
风翔飞
风翔飞
回复 @小白白哈哈 : 那么其他service的dao也是null?
小白白哈哈
小白白哈哈
回复 @风翔飞 : 持久层是以Hibernate为主的
风翔飞
风翔飞
回复 @小白白哈哈 : 这个有配置吗?
风翔飞
风翔飞
回复 @小白白哈哈 : 基本没问题,不过扫描dao指的是mybatis的扫描<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="xxx.xxx.dao"/><property name="sqlSessionFactory"/></be
返回顶部
顶部