集成 Jersey、Spring、Shiro

    pom.xml 做如下修改:

    中增加 Spring、Shiro 的版本

    <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. <artifactId>spring-aspects</artifactId>
    46. </exclusion>
    47. <groupId>org.springframework</groupId>
    48. <artifactId>spring-test</artifactId>
    49. </exclusion>
    50. </exclusions>
    51. </dependency>
    52. <!-- jersey end -->
    53. <!-- spring start -->
    54. <dependency>
    55. <groupId>org.springframework</groupId>
    56. <artifactId>spring-core</artifactId>
    57. <version>${spring.version}</version>
    58. </dependency>
    59. <dependency>
    60. <groupId>org.springframework</groupId>
    61. <artifactId>spring-context</artifactId>
    62. <version>${spring.version}</version>
    63. </dependency>
    64. <dependency>
    65. <groupId>org.springframework</groupId>
    66. <artifactId>spring-orm</artifactId>
    67. <version>${spring.version}</version>
    68. </dependency>
    69. <dependency>
    70. <groupId>org.springframework</groupId>
    71. <artifactId>spring-web</artifactId>
    72. <version>${spring.version}</version>
    73. </dependency>
    74. <dependency>
    75. <groupId>org.springframework</groupId>
    76. <artifactId>spring-aop</artifactId>
    77. <version>${spring.version}</version>
    78. </dependency>
    79. <dependency>
    80. <groupId>org.springframework</groupId>
    81. <artifactId>spring-beans</artifactId>
    82. <version>${spring.version}</version>
    83. </dependency>
    84. <dependency>
    85. <groupId>org.springframework</groupId>
    86. <artifactId>spring-aspects</artifactId>
    87. <version>${spring.version}</version>
    88. </dependency>
    89. <dependency>
    90. <artifactId>spring-test</artifactId>
    91. <version>${spring.version}</version>
    92. </dependency>
    93. <!-- spring end -->
    94. <!-- shiro start -->
    95. <dependency>
    96. <groupId>org.apache.shiro</groupId>
    97. <artifactId>shiro-core</artifactId>
    98. <version>${shiro.version}</version>
    99. </dependency>
    100. <dependency>
    101. <groupId>org.apache.shiro</groupId>
    102. <artifactId>shiro-spring</artifactId>
    103. <version>${shiro.version}</version>
    104. </dependency>
    105. <dependency>
    106. <groupId>org.apache.shiro</groupId>
    107. <artifactId>shiro-web</artifactId>
    108. <version>${shiro.version}</version>
    109. </dependency>
    110. <!-- shiro end -->

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

    解释: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. <?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 的常用配置分来处理。在相同路径下,复制一份 applicationContext.xml 更名为 applicationContext-shiro.xml。

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

    修改 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>