1.前言

上个Shiro Demo基础搭建是基于官方的快速入门版本,没有集成其他框架,只是简单的通过Main方法来执行Shiro工作流程,并测试一下比较核心的函数;但在企业开发中一般都会集成Spring,因为被Spring管理后很多事情都交给了Spring框架进行了管理,而且Spring框架提供了丰富的支持类,不仅方便我们开发人员进行扩展,也利于维护,通过Spring管理我们能把更多的细节放在业务上,提高我们的开发效率。

搭建环境(eclipse+jdk1.7+tomcat7.0)

  1.       首先是 新建一个web工程,引入Spring和Shiro相关jar包,如图

2.          新建一个applicationContext.xml文件,用来配置Shiro

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5.  
  6. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  7. <property name="cacheManager" ref="cacheManager"></property>
  8. <property name="realm" ref="jdbcRealm"></property>
  9. </bean>
  10.  
  11. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  12. <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
  13. </bean>
  14.  
  15. <bean id="jdbcRealm" class="com.zdd.shiro.ShiroRealm"></bean>
  16.  
  17. <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor">
  18. </bean>
  19.  
  20. <!-- 5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用. -->
  21.  
  22. <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
  23. depends-on="lifecycleBeanPostProcessor"/>
  24. <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
  25. <property name="securityManager" ref="securityManager"/>
  26. </bean>
  27.  
  28. <!--
  29. 6. 配置 ShiroFilter.
  30. 6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
  31. 若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
  32. -->
  33. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  34. <property name="securityManager" ref="securityManager"/>
  35. <property name="loginUrl" value="/login.jsp"/>
  36. <property name="successUrl" value="/list.jsp"/>
  37. <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
  38.  
  39. <property name="filterChainDefinitions">
  40. <value>
  41. /login.jsp = anon
  42. # everything else requires authentication:
  43. /** = authc
  44. </value>
  45. </property>
  46.  
  47. </bean>
  48.  
  49. </beans>

3.    配置ehcache.xml

  1. <ehcache>
  2.  
  3. <!-- Sets the path to the directory where cache .data files are created.
  4.  
  5. If the path is a Java System Property it is replaced by
  6. its value in the running VM.
  7.  
  8. The following properties are translated:
  9. user.home - User's home directory
  10. user.dir - User's current working directory
  11. java.io.tmpdir - Default temp file path -->
  12. <diskStore path="java.io.tmpdir"/>
  13.  
  14. <cache name="authorizationCache"
  15. eternal="false"
  16. timeToIdleSeconds="3600"
  17. timeToLiveSeconds="0"
  18. overflowToDisk="false"
  19. statistics="true">
  20. </cache>
  21.  
  22. <cache name="authenticationCache"
  23. eternal="false"
  24. timeToIdleSeconds="3600"
  25. timeToLiveSeconds="0"
  26. overflowToDisk="false"
  27. statistics="true">
  28. </cache>
  29.  
  30. <cache name="shiro-activeSessionCache"
  31. eternal="false"
  32. timeToIdleSeconds="3600"
  33. timeToLiveSeconds="0"
  34. overflowToDisk="false"
  35. statistics="true">
  36. </cache>
  37.  
  38. <!--Default Cache configuration. These will applied to caches programmatically created through
  39. the CacheManager.
  40.  
  41. The following attributes are required for defaultCache:
  42.  
  43. maxInMemory - Sets the maximum number of objects that will be created in memory
  44. eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
  45. is never expired.
  46. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
  47. if the element is not eternal. Idle time is now - last accessed time
  48. timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
  49. if the element is not eternal. TTL is now - creation time
  50. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
  51. has reached the maxInMemory limit.
  52.  
  53. -->
  54. <defaultCache
  55. maxElementsInMemory="10000"
  56. eternal="false"
  57. timeToIdleSeconds="120"
  58. timeToLiveSeconds="120"
  59. overflowToDisk="true"
  60. />
  61.  
  62. <!--Predefined caches. Add your cache configuration settings here.
  63. If you do not have a configuration for your cache a WARNING will be issued when the
  64. CacheManager starts
  65.  
  66. The following attributes are required for defaultCache:
  67.  
  68. name - Sets the name of the cache. This is used to identify the cache. It must be unique.
  69. maxInMemory - Sets the maximum number of objects that will be created in memory
  70. eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
  71. is never expired.
  72. timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
  73. if the element is not eternal. Idle time is now - last accessed time
  74. timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
  75. if the element is not eternal. TTL is now - creation time
  76. overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
  77. has reached the maxInMemory limit.
  78.  
  79. -->
  80.  
  81. <!-- Sample cache named sampleCache1
  82. This cache contains a maximum in memory of 10000 elements, and will expire
  83. an element if it is idle for more than 5 minutes and lives for more than
  84. 10 minutes.
  85.  
  86. If there are more than 10000 elements it will overflow to the
  87. disk cache, which in this configuration will go to wherever java.io.tmp is
  88. defined on your system. On a standard Linux system this will be /tmp"
  89. -->
  90. <cache name="sampleCache1"
  91. maxElementsInMemory="10000"
  92. eternal="false"
  93. timeToIdleSeconds="300"
  94. timeToLiveSeconds="600"
  95. overflowToDisk="true"
  96. />
  97.  
  98. <!-- Sample cache named sampleCache2
  99. This cache contains 1000 elements. Elements will always be held in memory.
  100. They are not expired. -->
  101. <cache name="sampleCache2"
  102. maxElementsInMemory="1000"
  103. eternal="true"
  104. timeToIdleSeconds="0"
  105. timeToLiveSeconds="0"
  106. overflowToDisk="false"
  107. /> -->
  108.  
  109. <!-- Place configuration for your caches following -->
  110.  
  111. </ehcache>
  112. 4. 新建一个spring-servlet.xml,用来配置Spring MVC,因为项目用到的Spring mvc
  113.  
  114. <?xml version="1.0" encoding="UTF-8"?>
  115. <beans xmlns="http://www.springframework.org/schema/beans"
  116. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  117. xmlns:mvc="http://www.springframework.org/schema/mvc"
  118. xmlns:context="http://www.springframework.org/schema/context"
  119. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  120. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  121. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  122.  
  123. <context:component-scan base-package="com.zdd.shiro"></context:component-scan>
  124.  
  125. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  126. <property name="prefix" value="/"></property>
  127. <property name="suffix" value=".jsp"></property>
  128. </bean>
  129.  
  130. <mvc:annotation-driven></mvc:annotation-driven>
  131. <mvc:default-servlet-handler/>
  132.  
  133. </beans>

5.  配置web.xml,注意的是需要配置Shiro拦截器,因为集成Spring后每次请求都需要被Shiro拦截

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6.  
  7. <!-- needed for ContextLoaderListener -->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:applicationContext.xml</param-value>
  11. </context-param>
  12.  
  13. <!-- Bootstraps the root web application context before servlet initialization -->
  14. <listener>
  15. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16. </listener>
  17.  
  18. <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
  19. <servlet>
  20. <servlet-name>spring</servlet-name>
  21. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24.  
  25. <!-- Map all requests to the DispatcherServlet for handling -->
  26. <servlet-mapping>
  27. <servlet-name>spring</servlet-name>
  28. <url-pattern>/</url-pattern>
  29. </servlet-mapping>
  30.  
  31. <!-- Shiro Filter is defined in the spring application context: -->
  32. <!--
  33. 1. 配置 Shiro 的 shiroFilter.
  34. 2. DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和
  35. <filter-name> 对应的 filter bean. 也可以通过 targetBeanName 的初始化参数来配置 filter bean 的 id.
  36. -->
  37. <filter>
  38. <filter-name>shiroFilter</filter-name>
  39. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  40. <init-param>
  41. <param-name>targetFilterLifecycle</param-name>
  42. <param-value>true</param-value>
  43. </init-param>
  44. </filter>
  45.  
  46. <filter-mapping>
  47. <filter-name>shiroFilter</filter-name>
  48. <url-pattern>/*</url-pattern>
  49. </filter-mapping>
  50.  
  51. </web-app>

6. 在applicationContext,xml中我配置的是 只有login页面可以匿名访问,其他的都需要认证能访问,所以新建一个login.jsp和一个user.jsp,测试下效果


  以上就是一个基础的Shiro和Spring集成的一个demo,其中的大部分代码和配置文件也是官网提供。


最后运行项目后默认是跳转到了login.jsp页面(因为在上面代码里配置了只有认证后才能访问其他页面)

【原】Spring整合Shiro基础搭建[3]的更多相关文章

  1. spring整合shiro框架

    上一篇文章已经对shiro框架做了一定的介绍,这篇文章讲述使用spring整合shiro框架,实现用户认证已经权限控制 1.搭建环境 这里不在赘述spring环境的搭建,可以简单的搭建一个ssm框架, ...

  2. Spring整合Shiro做权限控制模块详细案例分析

    1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...

  3. Spring整合Shiro并扩展使用EL表达式

    Shiro是一个轻量级的权限控制框架,应用非常广泛.本文的重点是介绍Spring整合Shiro,并通过扩展使用Spring的EL表达式,使@RequiresRoles等支持动态的参数.对Shiro的介 ...

  4. Spring 整合 Shiro

    一.引入依赖 <!-- spring start --> <dependency> <groupId>org.springframework</groupId ...

  5. Spring整合Shiro 权限 角色 用户关系分析

    Spring整合Shiro 权限 角色 用户关系分析 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 前置内容 之前我们学习了,使用注解的方式去完成权限的控制,当然,也是静态的,也就 ...

  6. 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面

    这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...

  7. Spring整合Shiro

    apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式.其提供的 native-session(即把用户认证后的授权信息保存 ...

  8. spring整合shiro配置BUG,Tomcat启动不了:Error during artifact deployment. See server log for details

    现象 spring配置shiro权限控制之后,项目无法启动 [2019-08-09 09:00:35,800] Artifact export_web_manager:war exploded: Er ...

  9. 7. 整合shiro,搭建粗粒度权限管理

    shiro是一个易用的权限管理框架,只需提供一个Realm即可在项目中使用,本文就将结合上一篇中搭建的权限模块.角色模块和用户模块来搭建一个粗粒度的权限管理系统,具体如下:1. 添加shiro依赖和与 ...

随机推荐

  1. web请求的状态码

    摘录于  https://www.cnblogs.com/lovychen/p/6256343.html 1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行 ...

  2. day 81 天 ORM 操作复习总结

    # ###############基于对象查询(子查询)############## 一.对多查询  正向查询 from django.shortcuts import render,HttpResp ...

  3. JQuery - 动态添加Html后,如何使CSS生效,JS代码可用?

    今天在开发JQuery Mobile程序时候,需要从服务器取得数据,随后显示在页面上的Listview控件中,数据完整获取到了,也动态添加到Listview控件中,但是数据对应的CSS没有任何效果了, ...

  4. “全栈2019”Java多线程第二十章:同步方法产生死锁的例子

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. Django(图书管理系统2)

    day64 内容回顾     1. ORM外键操作         图书表和出版社表  多对一 的关系              # 书     class Book(models.Model):   ...

  6. 【GDKOI2016】 魔卡少女 线段树

    题目大意:给你一个长度为n的序列${a_1....a_n}$,有$m$次操作 每次操作有两种情况:修改$a_i$的值,询问$[l,r]$中所有子区间的异或和. 数据范围:$n,m≤10^5$,$a_i ...

  7. Maven国内阿里镜像(Maven下载慢的解决方法)

    Maven是当前流行的项目管理工具,但官方的库在国外经常连不上,连上也下载速度很慢.国内oschina的maven服务器很早之前就关了.今天发现阿里云的一个中央仓库,亲测可用. <mirror& ...

  8. System.Thread.TImer控件——http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml

    http://www.360doc.com/content/11/0812/11/1039473_139824496.shtml

  9. jq01--概述

    jq:jQuery,是一个JavaScript函数库,为了简化js开发与编码而封装的js库,是一个“写得更少,做得更多”的js函数库,为事件处理而特别设计的.现在我们来学习一下它. 1.jq库:为事件 ...

  10. (转)Python 字符串

    原文:http://www.runoob.com/python/python-strings.html