按着《SpringMVC整合Shiro》这篇博文来做即可。但是,在启动时发现Spring的配置被载入两次,很明显SpringMVC 和 Shiro 所使用的不是同一个 Spring Application Context。在参考《ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别》后,web.xml 配置改为:
......
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>
org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatch
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
......
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatch-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
......
来源:oschina
链接:https://my.oschina.net/u/587242/blog/208289