当前位置: 代码迷 >> 综合 >> SSM整合Shiro框架时自定义Realm中(@Autowired、@Resource)注入service失败问题
  详细解决方案

SSM整合Shiro框架时自定义Realm中(@Autowired、@Resource)注入service失败问题

热度:57   发布时间:2023-10-24 12:47:46.0

        在SSM框架中整合Shiro时,在自定义的Realm类中通过@Autowired、@Resource注解注入service时,有的人会碰到如下错误:

public class MyRealm extends AuthorizingRealm {
    @AutowiredUserService userService;// 授权@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    ...return null;}// 认证@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)throws AuthenticationException {
    ...}}

SSM整合Shiro框架时自定义Realm中(@Autowired、@Resource)注入service失败问题
        这说明IOC容器中不存在UserService这个类的对象,导致初始化MyRealm时注入UserService失败。这是什么原因?
        有以下两点说明:

  1. spring初始化时bean注入容器的顺序是Listener、filter、servlet
  2. 自定义realm的认证阶段属于filter
    那出现上述错误有可能是把service层包的扫描放到了springMVC的配置文件中。

spring-servlet.xml:

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="com.lr.shiro.controller"></context:component-scan><context:component-scan base-package="com.lr.shiro.service"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><mvc:annotation-driven /><mvc:default-servlet-handler/></beans>

        这样将导致初始化MyRealm时UserService并未注入到IOC容器中,因为MyRealm是在初始化Filter阶段,而service在初始化servlet阶段。
        所以,将service层包扫描配置到spring的配置文件,SpringMVC只负责controller层。
applicationContext.xml:

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="com.lr.shiro.service"></context:component-scan><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><!--...--></bean>
</beans>

spring-servlet.xml:

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!--springmvc 只负责扫描controller--><context:component-scan base-package="com.lr.shiro.controller"></context:component-scan><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean><mvc:annotation-driven /><mvc:default-servlet-handler/></beans>

这样即可正常启动。

  相关解决方案