`

springMvc和shiro整合,shiro的realm不能自动注入的问题

阅读更多

最近研究shiro,一开头就遇到了大困难,调试了3小时。
问题描述如下:shiro和spring mvc整合,shiro自定义了realm。
其中自定义的realm里面居然不能使用@Autowired注解标签注入相关的用户service。
百思不得其解,一项项跟踪,发现原来shiro 自定义realm的认证阶段属于filter,当时的spring bean还没有读取进来。

最后通过配置web.xml文件,把spring mvc的xml提高一点优先级,才最终解决了这个问题。

 

	<!-- 配置spring容器监听器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/classes/applicationContext-shiro.xml,
			/WEB-INF/classes/spring-mvc.xml
		</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

<!-- spring web程序的第一层控制器, 负责处理程序请求 -->
	<servlet>
		<servlet-name>springDispatcherServlet</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>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

 

注意红色这一项,我把springmvc的配置文件提上去,放到contextConfigLocation中去加载。

这样,就能在filter阶段注入其它已经注册了的bean。

 

具体参考了这篇文章:

http://blog.csdn.net/godha/article/details/13025099

 

分享到:
评论

相关推荐

    springMVC+mybatis+shiro+redis 项目整合demo

    springMVC+mybatis+shiro+redis 项目整合demo。

    简单配置 shiro + spring +springMVC+hibernate简单框架

    SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能。 配置applicationContext-shiro.xml 1. 配置authorizingRealm ...

    吴天雄--shiro个人总结笔记.doc

    shiro标签,第六讲 自定义Realm(加入整合spring和MyBatis), 第七讲 加密(加密算法、加密过程、加密实现代码),第八讲 记住我,第九讲 缓存管理(代码实现),第十讲 会话管理(session的监听和检测、环境配置)...

    单点登录sso-shiro-cas-maven

    # sso-shiro-cas spring下使用shiro+cas配置单点登录,多个系统之间的访问,每次只需要登录一次 ## 系统模块说明 1. cas: 单点登录模块,这里直接拿的是cas的项目改了点样式而已 2. doc: 文档目录,里面有数据库...

    shiro注解版

    -- Support Shiro Annotation 必须放在springMVC配置文件中 --&gt; &lt;!-- 异常处理,权限注解会抛出异常,根据异常返回相应页面 --&gt; class="org.springframework.web.servlet.handler....

    Shiro入门框架

    这是一个shiro的入门Demo.. 使用了Spring MVC,mybaits等技术.. 数据库设计 : User : name--password Role : id--userid--roleName Function : id--userid--url tinys普通用户只能访问index.jsp admin用户通过添加...

    SpringShiro分布式缓存版

    -- Support Shiro Annotation 必须放在springMVC配置文件中 --&gt; &lt;!-- 异常处理,权限注解会抛出异常,根据异常返回相应页面 --&gt; class="org.springframework.web.servlet.handler....

    SpringMVC+mybatis-framework-bootstrap

    实际使用shiro的时候大部分都是和spring等框架结合使用,主要就是配置web.xml将shiro的filter和spring容器bean的filter关联起来,生命周期由servlet容器来控制,然后配置shiro的spring的xml文件,其中主要配置shiro...

    CRM客户关系管理系统(配套数据库需另行下载).zip

    前端:vue+element框架 后台:spring+springMVC+Mybatis框架 开发软件:idea 数据库:MySQL 1.shiro身份验证,权限管理。 主要是授权,在项目启动的时候会加载所有的权限资源,登录然后进行权限判断,通过后台代码...

    4S店管理系统.zip

    前端:vue+element框架 后台:spring+springMVC+Mybatis框架 开发软件:idea 数据库:MySQL 1.shiro身份验证,权限管理。 主要是授权,在项目启动的时候会加载所有的权限资源,登录然后进行权限判断,通过后台代码...

Global site tag (gtag.js) - Google Analytics