集成 Jersey、Spring、Shiro

在上面的 Jersey web 项目的基础上,添加 Spring、Shiro 的支持,实现初级的权限安全框架。

增加 Spring、Shiro 的版本依赖

pom.xml 做如下修改:

<properties>中增加 Spring、Shiro 的版本

  1. <spring.version>4.1.4.RELEASE</spring.version>
  2. <shiro.version>1.2.3</shiro.version>

<dependencies>中增加 Spring、Shiro 的版本依赖

  1. <dependency>
  2. <groupId>org.glassfish.jersey.ext</groupId>
  3. <artifactId>jersey-spring3</artifactId>
  4. <version>${jersey.version}</version>
  5. <exclusions>
  6. <exclusion>
  7. <artifactId>jersey-server</artifactId>
  8. <groupId>org.glassfish.jersey.core</groupId>
  9. </exclusion>
  10. <exclusion>
  11. <artifactId>
  12. jersey-container-servlet-core
  13. </artifactId>
  14. <groupId>org.glassfish.jersey.containers</groupId>
  15. </exclusion>
  16. <exclusion>
  17. <artifactId>hk2</artifactId>
  18. <groupId>org.glassfish.hk2</groupId>
  19. </exclusion>
  20. <exclusion>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-core</artifactId>
  23. </exclusion>
  24. <exclusion>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-context</artifactId>
  27. </exclusion>
  28. <exclusion>
  29. <groupId>org.springframework</groupId>
  30. <artifactId>spring-web</artifactId>
  31. </exclusion>
  32. <exclusion>
  33. <groupId>org.springframework</groupId>
  34. <artifactId>spring-beans</artifactId>
  35. </exclusion>
  36. <exclusion>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-orm</artifactId>
  39. </exclusion>
  40. <exclusion>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-aop</artifactId>
  43. </exclusion>
  44. <exclusion>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-aspects</artifactId>
  47. </exclusion>
  48. <exclusion>
  49. <groupId>org.springframework</groupId>
  50. <artifactId>spring-test</artifactId>
  51. </exclusion>
  52. </exclusions>
  53. </dependency>
  54. <!-- jersey end -->
  55. <!-- spring start -->
  56. <dependency>
  57. <groupId>org.springframework</groupId>
  58. <artifactId>spring-core</artifactId>
  59. <version>${spring.version}</version>
  60. </dependency>
  61. <dependency>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring-context</artifactId>
  64. <version>${spring.version}</version>
  65. </dependency>
  66. <dependency>
  67. <groupId>org.springframework</groupId>
  68. <artifactId>spring-orm</artifactId>
  69. <version>${spring.version}</version>
  70. </dependency>
  71. <dependency>
  72. <groupId>org.springframework</groupId>
  73. <artifactId>spring-web</artifactId>
  74. <version>${spring.version}</version>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.springframework</groupId>
  78. <artifactId>spring-aop</artifactId>
  79. <version>${spring.version}</version>
  80. </dependency>
  81. <dependency>
  82. <groupId>org.springframework</groupId>
  83. <artifactId>spring-beans</artifactId>
  84. <version>${spring.version}</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>org.springframework</groupId>
  88. <artifactId>spring-aspects</artifactId>
  89. <version>${spring.version}</version>
  90. </dependency>
  91. <dependency>
  92. <groupId>org.springframework</groupId>
  93. <artifactId>spring-test</artifactId>
  94. <version>${spring.version}</version>
  95. <scope>test</scope>
  96. </dependency>
  97. <!-- spring end -->
  98. <!-- shiro start -->
  99. <dependency>
  100. <groupId>org.apache.shiro</groupId>
  101. <artifactId>shiro-core</artifactId>
  102. <version>${shiro.version}</version>
  103. </dependency>
  104. <dependency>
  105. <groupId>org.apache.shiro</groupId>
  106. <artifactId>shiro-spring</artifactId>
  107. <version>${shiro.version}</version>
  108. </dependency>
  109. <dependency>
  110. <groupId>org.apache.shiro</groupId>
  111. <artifactId>shiro-web</artifactId>
  112. <version>${shiro.version}</version>
  113. </dependency>
  114. <!-- shiro end -->

由于 Jersey 官方 只有 jersey-spring3 库是用 来对 Spring 进行集成支持的,所以我们在用 Spring 4.x 的依赖时,要将 jersey-spring3 中的 Spring 3 依赖剔除。

配置 Spring

在 resource 目录下 增加一个 applicationContext.xml,这个就不详述了,搞过 Spring 的都知道,而后在 web.xml 中添加如下:

  1. <!-- spring start -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:applicationContext.xml</param-value>
  5. </context-param>
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>
  9. <filter>
  10. <description>处理编码的过滤器</description>
  11. <filter-name>encodingFilter</filter-name>
  12. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13. <init-param>
  14. <param-name>encoding</param-name>
  15. <param-value>UTF-8</param-value>
  16. </init-param>
  17. <init-param>
  18. <param-name>forceEncoding</param-name>
  19. <param-value>true</param-value>
  20. </init-param>
  21. </filter>
  22. <filter-mapping>
  23. <filter-name>encodingFilter</filter-name>
  24. <url-pattern>/*</url-pattern>
  25. </filter-mapping>
  26. <!-- spring end -->

解释:classpath:applicationContext.xml,就是 Spring 配置文件的所在位置。encodingFilter 用来处理 编码的,这里是 UTF-8 来支持中文。

没错 ,Spring 的配置就是这么简单。

问题

web.xml 可能会出现下面的问题:

  1. The content of element type "web-app" must match "(icon?,display-
  2. name?,description?,distributable?,context-param*,filter*,filter-mapping*,listener*,servlet*,servlet-
  3. mapping*,session-config?,mime-mapping*,welcome-file-list?,error-page*,taglib*,resource-env-
  4. ref*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*,ejb-local-ref*)".

意思是 这个 web.xml 的解析文档太旧了,解决方法是 将原来的

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>

改为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">

Shiro 在 Spring 的集成

为了方便管理,我们将 Shiro 的配置和 Spring 的常用配置分来处理。在相同路径下,复制一份 applicationContext.xml 更名为 applicationContext-shiro.xml。

修改 applicationContext.xml ,引用 Shiro 的配置

  1. <!-- 引用各模块的spring配置文件 -->
  2. <import resource="applicationContext-shiro.xml"></import>

修改 applicationContext-spring.xml:

在 web.xml 中定义下面的过滤器及过滤器映射:

  1. <!-- filter-name对应applicationContext.xml中定义的名字为“shiroFilter”的bean -->
  2. <filter>
  3. <filter-name>shiroFilter</filter-name>
  4. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  5. <init-param>
  6. <param-name>targetFilterLifecycle</param-name>
  7. <param-value>true</param-value>
  8. </init-param>
  9. </filter>
  10. <!-- 使用“/*”匹配所有请求,保证所有的可控请求都经过Shiro的过滤。通常这个filter-mapping
  11. 放置到最前面(其他filter-mapping前面),保证它是过滤器链中第一个起作用的 -->
  12. <filter-mapping>
  13. <filter-name>shiroFilter</filter-name>
  14. <url-pattern>/*</url-pattern>
  15. </filter-mapping>