maven springmvc hibernate shiro 集成

18 篇文章 0 订阅
8 篇文章 0 订阅

最近项目需要进行高要求的Web权限管理,在比较Spring security和Shiro之后由于Shiro更大的灵活度和更强的配置性选择了Shiro。

不过官方的文档写的对于集成spring不太清楚,尤其是Realm怎样实现。

因此,在查阅一系列资料之后,将目前集成的步骤总结出来。

特别推荐博文:http://www.cnblogs.com/xql4j/archive/2013/03/30/2990841.html,此文对作者帮助极大,在此表示感谢!


具体步骤抽象:




修改配置文件:

首先是pom.xml
  1. <!-- Version of jars -->  
  2.     <properties>  
  3.         <spring.version>3.2.3.RELEASE</spring.version>  
  4.         <hibernate.version>4.2.3.Final</hibernate.version>  
  5.         <slf4j.version>1.7.5</slf4j.version>  
  6.         <log4j.version>1.2.17</log4j.version>  
  7.         <junit.version>4.11</junit.version>  
  8.         <cglib.version>3.0</cglib.version>  
  9.         <velocity.version>1.7</velocity.version>  
  10.         <tiles.extras.version>3.0.1</tiles.extras.version>  
  11.         <jsonlib.version>2.4</jsonlib.version>  
  12.         <shiro.version>1.2.2</shiro.version>  
  13.     </properties>  
  14.   
  15.     <dependencies>  
  16.   
  17.         <!-- Spring jars -->  
  18.         <dependency>  
  19.             <groupId>org.springframework</groupId>  
  20.             <artifactId>spring-core</artifactId>  
  21.             <version>${spring.version}</version>  
  22.             <exclusions>  
  23.                 <exclusion>  
  24.                     <groupId>commons-logging</groupId>  
  25.                     <artifactId>commons-logging</artifactId>  
  26.                 </exclusion>  
  27.             </exclusions>  
  28.         </dependency>  
  29.         <dependency>  
  30.             <groupId>org.springframework</groupId>  
  31.             <artifactId>spring-beans</artifactId>  
  32.             <version>${spring.version}</version>  
  33.             <exclusions>  
  34.                 <exclusion>  
  35.                     <groupId>commons-logging</groupId>  
  36.                     <artifactId>commons-logging</artifactId>  
  37.                 </exclusion>  
  38.             </exclusions>  
  39.         </dependency>  
  40.         <dependency>  
  41.             <groupId>org.springframework</groupId>  
  42.             <artifactId>spring-context</artifactId>  
  43.             <version>${spring.version}</version>  
  44.             <exclusions>  
  45.                 <exclusion>  
  46.                     <groupId>commons-logging</groupId>  
  47.                     <artifactId>commons-logging</artifactId>  
  48.                 </exclusion>  
  49.             </exclusions>  
  50.         </dependency>  
  51.         <dependency>  
  52.             <groupId>org.springframework</groupId>  
  53.             <artifactId>spring-web</artifactId>  
  54.             <version>${spring.version}</version>  
  55.             <exclusions>  
  56.                 <exclusion>  
  57.                     <groupId>commons-logging</groupId>  
  58.                     <artifactId>commons-logging</artifactId>  
  59.                 </exclusion>  
  60.             </exclusions>  
  61.         </dependency>  
  62.         <dependency>  
  63.             <groupId>org.springframework</groupId>  
  64.             <artifactId>spring-test</artifactId>  
  65.             <version>${spring.version}</version>  
  66.             <scope>test</scope>  
  67.             <exclusions>  
  68.                 <exclusion>  
  69.                     <groupId>commons-logging</groupId>  
  70.                     <artifactId>commons-logging</artifactId>  
  71.                 </exclusion>  
  72.             </exclusions>  
  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-webmvc</artifactId>  
  82.             <version>${spring.version}</version>  
  83.         </dependency>  
  84.         <dependency>  
  85.             <groupId>org.springframework</groupId>  
  86.             <artifactId>spring-context-support</artifactId>  
  87.             <version>${spring.version}</version>  
  88.         </dependency>  
  89.         <dependency>  
  90.             <groupId>org.springframework</groupId>  
  91.             <artifactId>spring-orm</artifactId>  
  92.             <version>${spring.version}</version>  
  93.             <type>jar</type>  
  94.             <scope>compile</scope>  
  95.         </dependency>  
  96.         <dependency>  
  97.             <groupId>org.springframework</groupId>  
  98.             <artifactId>spring-jdbc</artifactId>  
  99.             <version>${spring.version}</version>  
  100.             <type>jar</type>  
  101.             <scope>runtime</scope>  
  102.         </dependency>  
  103.         <dependency>  
  104.             <groupId>taglibs</groupId>  
  105.             <artifactId>standard</artifactId>  
  106.             <version>1.1.2</version>  
  107.         </dependency>  
  108.         <dependency>  
  109.             <groupId>javax.servlet</groupId>  
  110.             <artifactId>jstl</artifactId>  
  111.             <version>1.2</version>  
  112.         </dependency>  
  113.         <dependency>  
  114.             <groupId>commons-io</groupId>  
  115.             <artifactId>commons-io</artifactId>  
  116.             <version>2.1</version>  
  117.         </dependency>  
  118.         <!-- end of Spring jars -->  
  119.   
  120.         <!-- Shiro security -->  
  121.         <dependency>  
  122.             <groupId>org.apache.shiro</groupId>  
  123.             <artifactId>shiro-core</artifactId>  
  124.             <version>${shiro.version}</version>  
  125.         </dependency>  
  126.         <dependency>  
  127.             <groupId>org.apache.shiro</groupId>  
  128.             <artifactId>shiro-web</artifactId>  
  129.             <version>${shiro.version}</version>  
  130.         </dependency>  
  131.         <dependency>  
  132.             <groupId>org.apache.shiro</groupId>  
  133.             <artifactId>shiro-cas</artifactId>  
  134.             <version>${shiro.version}</version>  
  135.         </dependency>  
  136.         <dependency>  
  137.             <groupId>org.apache.shiro</groupId>  
  138.             <artifactId>shiro-spring</artifactId>  
  139.             <version>${shiro.version}</version>  
  140.         </dependency>  
  141.         <!-- end of Shiro security -->  
  142.   
  143.         <!-- Json -->  
  144.         <dependency>  
  145.             <groupId>org.codehaus.jackson</groupId>  
  146.             <artifactId>jackson-core-lgpl</artifactId>  
  147.             <version>1.8.1</version>  
  148.         </dependency>  
  149.         <dependency>  
  150.             <groupId>org.codehaus.jackson</groupId>  
  151.             <artifactId>jackson-mapper-lgpl</artifactId>  
  152.             <version>1.8.1</version>  
  153.         </dependency>  
  154.         <dependency>  
  155.             <groupId>net.sf.json-lib</groupId>  
  156.             <artifactId>json-lib</artifactId>  
  157.             <version>${jsonlib.version}</version>  
  158.             <classifier>jdk15</classifier>  
  159.         </dependency>  
  160.         <!-- end of Json -->  
  161.   
  162.         <!-- Hibernate4 -->  
  163.         <dependency>  
  164.             <groupId>org.hibernate</groupId>  
  165.             <artifactId>hibernate-core</artifactId>  
  166.             <version>${hibernate.version}</version>  
  167.         </dependency>  
  168.         <dependency>  
  169.             <groupId>org.hibernate</groupId>  
  170.             <artifactId>hibernate-entitymanager</artifactId>  
  171.             <version>${hibernate.version}</version>  
  172.         </dependency>  
  173.         <dependency>  
  174.             <groupId>javax.xml.bind</groupId>  
  175.             <artifactId>jaxb-api</artifactId>  
  176.             <version>2.1</version>  
  177.             <exclusions>  
  178.                 <exclusion>  
  179.                     <groupId>javax.xml.stream</groupId>  
  180.                     <artifactId>stax-api</artifactId>  
  181.                 </exclusion>  
  182.             </exclusions>  
  183.         </dependency>  
  184.         <dependency>  
  185.             <groupId>com.sun.xml.bind</groupId>  
  186.             <artifactId>jaxb-impl</artifactId>  
  187.             <version>2.1.10</version>  
  188.         </dependency>  
  189.         <dependency>  
  190.             <groupId>org.apache.openejb</groupId>  
  191.             <artifactId>javaee-api</artifactId>  
  192.             <version>5.0-1</version>  
  193.             <!--<scope>provided</scope> -->  
  194.         </dependency>  
  195.         <dependency>  
  196.             <groupId>commons-dbcp</groupId>  
  197.             <artifactId>commons-dbcp</artifactId>  
  198.             <version>1.4</version>  
  199.             <!--<scope>provided</scope> -->  
  200.         </dependency>  
  201.         <!-- end of Hibernate4 -->  
  202.   
  203.         <!-- logging -->  
  204.         <dependency>  
  205.             <groupId>org.slf4j</groupId>  
  206.             <artifactId>slf4j-api</artifactId>  
  207.             <version>${slf4j.version}</version>  
  208.         </dependency>  
  209.         <dependency>  
  210.             <groupId>org.slf4j</groupId>  
  211.             <artifactId>jcl-over-slf4j</artifactId>  
  212.             <version>${slf4j.version}</version>  
  213.         </dependency>  
  214.         <dependency>  
  215.             <groupId>org.slf4j</groupId>  
  216.             <artifactId>slf4j-log4j12</artifactId>  
  217.             <version>${slf4j.version}</version>  
  218.         </dependency>  
  219.         <dependency>  
  220.             <groupId>log4j</groupId>  
  221.             <artifactId>log4j</artifactId>  
  222.             <version>${log4j.version}</version>  
  223.             <!--<scope>provided</scope> -->  
  224.         </dependency>  
  225.         <!-- end of logging -->  
  226.   
  227.         <!-- provided -->  
  228.         <dependency>  
  229.             <groupId>javax.servlet</groupId>  
  230.             <artifactId>servlet-api</artifactId>  
  231.             <version>2.5</version>  
  232.             <scope>compile</scope>  
  233.         </dependency>  
  234.         <dependency>  
  235.             <groupId>cglib</groupId>  
  236.             <artifactId>cglib</artifactId>  
  237.             <version>${cglib.version}</version>  
  238.         </dependency>  
  239.         <dependency>  
  240.             <groupId>javax.servlet.jsp</groupId>  
  241.             <artifactId>jsp-api</artifactId>  
  242.             <version>2.1</version>  
  243.         </dependency>  
  244.         <!-- end of provided -->  
  245.   
  246.         <!-- Test -->  
  247.         <dependency>  
  248.             <groupId>junit</groupId>  
  249.             <artifactId>junit</artifactId>  
  250.             <version>${junit.version}</version>  
  251.             <scope>test</scope>  
  252.         </dependency>  
  253.         <!-- end of Test -->  
  254.   
  255.         <!-- Database -->  
  256.         <dependency>  
  257.             <groupId>com.mchange</groupId>  
  258.             <artifactId>c3p0</artifactId>  
  259.             <version>0.9.5-pre3</version>  
  260.             <type>jar</type>  
  261.             <scope>compile</scope>  
  262.         </dependency>  
  263.         <dependency>  
  264.             <groupId>mysql</groupId>  
  265.             <artifactId>mysql-connector-java</artifactId>  
  266.             <version>5.1.21</version>  
  267.             <type>jar</type>  
  268.             <scope>compile</scope>  
  269.         </dependency>  
  270.         <!-- end of Database -->  
  271.   
  272.         <!-- velocity -->  
  273.         <dependency>  
  274.             <groupId>org.apache.velocity</groupId>  
  275.             <artifactId>velocity</artifactId>  
  276.             <version>${velocity.version}</version>  
  277.         </dependency>  
  278.         <!-- end of velocity -->  
  279.   
  280.         <!-- fileUpload -->  
  281.         <dependency>  
  282.             <groupId>commons-fileupload</groupId>  
  283.             <artifactId>commons-fileupload</artifactId>  
  284.             <version>1.2.2</version>  
  285.         </dependency>  
  286.         <!--io -->  
  287.         <dependency>  
  288.             <groupId>commons-io</groupId>  
  289.             <artifactId>commons-io</artifactId>  
  290.             <version>2.4</version>  
  291.         </dependency>  
  292.         <!-- end of fileUpload -->  
  293.   
  294.         <!-- tiles -->  
  295.         <dependency>  
  296.             <groupId>org.apache.tiles</groupId>  
  297.             <artifactId>tiles-extras</artifactId>  
  298.             <version>${tiles.extras.version}</version>  
  299.             <!-- it will include the rest -->  
  300.         </dependency>  
  301.         <!-- end of tiles -->  
  302.     </dependencies>  
  303.   
  304.     <!-- Custom repositories -->  
  305.     <repositories>  
  306.         <repository>  
  307.             <id>springsource-milestones</id>  
  308.             <name>SpringSource Milestones Proxy</name>  
  309.             <url>https://oss.sonatype.org/content/repositories/springsource-milestones</url>  
  310.         </repository>  
  311.         <repository>  
  312.             <id>CentralM1</id>  
  313.             <name>sonatype maven repository</name>  
  314.             <url>https://repository.sonatype.org/content/shadows/centralm1/</url>  
  315.         </repository>  
  316.     </repositories>  
  317.     <!-- end of Custom repositories -->  
  318.   
  319.     <!-- Build support -->  
  320.     <build>  
  321.         <plugins>  
  322.             <plugin>  
  323.                 <groupId>org.mortbay.jetty</groupId>  
  324.                 <artifactId>maven-jetty-plugin</artifactId>  
  325.                 <version>7.0.0.pre5</version>  
  326.                 <configuration>  
  327.                     <encoding>UTF-8</encoding>  
  328.                 </configuration>  
  329.             </plugin>  
  330.         </plugins>  
  331.     </build>  
  332.     <!-- end of Build support -->  
  333. </project>  
接下来:web.xml
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <web-app xmlns="http://java.sun.xml/ns/javaee"  
  3.                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  5.                                                      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <display-name>haiercloud-webapp</display-name>  
  7.     <!-- context-params -->  
  8.     <!-- webAppRootKey -->  
  9.     <context-param>  
  10.         <param-name>webAppRootKey</param-name>  
  11.         <param-value>haiercloud-webapp</param-value>  
  12.     </context-param>  
  13.     <context-param>  
  14.         <param-name>log4jConfigLocation</param-name>  
  15.         <param-value>classpath:log4j.properties</param-value>  
  16.     </context-param>  
  17.     <!-- Spring configuration file location -->  
  18.     <context-param>  
  19.         <param-name>contextConfigLocation</param-name>  
  20.         <param-value>  
  21.             /WEB-INF/classes/applicationContext.xml,  
  22.             classpath*:applicationContext.xml  
  23.         </param-value>  
  24.     </context-param>  
  25.     <!-- end of context-params -->  
  26.       
  27.     <!-- Spring configuration -->  
  28.     <!-- filters -->  
  29.     <!-- Spring encoding filter -->  
  30.     <filter>  
  31.         <filter-name>encodingFilter</filter-name>  
  32.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  33.         <init-param>  
  34.             <param-name>encoding</param-name>  
  35.             <param-value>UTF-8</param-value>  
  36.         </init-param>  
  37.     </filter>  
  38.     <!-- Shiro security filter -->  
  39.     <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->  
  40.     <filter>  
  41.         <filter-name>shiroFilter</filter-name>  
  42.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  43.         <init-param>  
  44.             <param-name>targetFilterLifecycle</param-name>  
  45.             <param-value>true</param-value>  
  46.         </init-param>  
  47.     </filter>  
  48.     <!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->  
  49.     <!-- requests. Usually this filter mapping is defined first (before all others) to       -->  
  50.     <!-- ensure that Shiro works in subsequent filters in the filter chain: -->  
  51.      
  52.     <!-- Encoding URL mapping -->  
  53.     <filter-mapping>  
  54.         <filter-name>encodingFilter</filter-name>  
  55.         <url-pattern>/*</url-pattern>  
  56.     </filter-mapping>  
  57.      <filter-mapping>  
  58.         <filter-name>shiroFilter</filter-name>  
  59.         <url-pattern>/*</url-pattern>  
  60.     </filter-mapping>  
  61.     <!-- end of filters -->  
  62.       
  63.     <!-- Servlet configuration -->  
  64.     <servlet>  
  65.         <servlet-name>SpringDispatcher</servlet-name>  
  66.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  67.         <init-param>  
  68.             <param-name>contextConfigLocation</param-name>  
  69.             <param-value>/WEB-INF/dispathcer-servlet.xml</param-value>  
  70.         </init-param>  
  71.         <load-on-startup>1</load-on-startup>  
  72.     </servlet>  
  73.       
  74.     <servlet-mapping>  
  75.         <servlet-name>default</servlet-name>  
  76.         <url-pattern>*.png</url-pattern>  
  77.     </servlet-mapping>  
  78.     <servlet-mapping>  
  79.         <servlet-name>SpringDispatcher</servlet-name>  
  80.         <url-pattern>/</url-pattern>  
  81.     </servlet-mapping>  
  82.       
  83.     <session-config>  
  84.         <session-timeout>30</session-timeout>  
  85.     </session-config>  
  86.     <!-- end of Servlet configuration -->  
  87.       
  88.     <!-- Listeners -->  
  89.     <listener>  
  90.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  91.     </listener>  
  92.     <listener>  
  93.         <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>  
  94.     </listener>  
  95.     <listener>  
  96.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  97.     </listener>  
  98.     <!-- end of Listeners -->  
  99.       
  100.     <!-- jsp configuration -->  
  101.     <jsp-config>  
  102.         <jsp-property-group>  
  103.             <display-name>JSPConfiguration</display-name>  
  104.             <url-pattern>*.jsp</url-pattern>  
  105.             <el-ignored>false</el-ignored>  
  106.             <page-encoding>utf-8</page-encoding>  
  107.         </jsp-property-group>  
  108.     </jsp-config>  
  109.     <!-- end of jsp configuration -->  
  110.       
  111.     <!-- Error page definition -->  
  112.     <!--   
  113.     <error-page>  
  114.         <exception-type>java.lang.Throwable</exception-type>  
  115.         <location>/static_pages/error_pages/500.jsp</location>  
  116.     </error-page>  
  117.     <error-page>  
  118.         <error-code>500</error-code>  
  119.         <location>/static_pages/error_pages/500.jsp</location>  
  120.     </error-page>  
  121.     <error-page>  
  122.         <error-code>404</error-code>  
  123.         <location>/static_pages/error_pages/404.jsp</location>  
  124.     </error-page>  
  125.     -->  
  126.     <!-- end of Error page definition -->  
  127.       
  128.     <!-- Welcome page definition -->  
  129.     <!--    
  130.     <welcome-file-list>  
  131.         <welcome-file>index.jsp</welcome-file>  
  132.     </welcome-file-list>  
  133.     -->  
  134.     <!-- end of Welcome page definition -->  
  135. </web-app>  



原文地址:http://blog.csdn.net/jokes000/article/details/10998109


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值