maven springmvc hibernate shiro 集成(转)

代码格式很不错,转载过来借鉴下。
原文链接:http://blog.csdn.net/jokes000/article/details/10998109
以下为原文:
最近项目需要进行高要求的Web权限管理,在比较Spring security和Shiro之后由于Shiro更大的灵活度和更强的配置性选择了Shiro。
不过官方的文档写的对于集成spring不太清楚,尤其是Realm怎样实现。
因此,在查阅一系列资料之后,将目前集成的步骤总结出来。
特别推荐博文:http://www.cnblogs.com/xql4j/archive/2013/03/30/2990841.html,此文对作者帮助极大,在此表示感谢!

具体步骤抽象:



修改配置文件:
首先是pom.xml
[html] view plain copy
<!-- Version of jars --> 
    <properties> 
        <spring.version>3.2.3.RELEASE</spring.version> 
        <hibernate.version>4.2.3.Final</hibernate.version> 
        <slf4j.version>1.7.5</slf4j.version> 
        <log4j.version>1.2.17</log4j.version> 
        <junit.version>4.11</junit.version> 
        <cglib.version>3.0</cglib.version> 
        <velocity.version>1.7</velocity.version> 
        <tiles.extras.version>3.0.1</tiles.extras.version> 
        <jsonlib.version>2.4</jsonlib.version> 
        <shiro.version>1.2.2</shiro.version> 
    </properties> 
 
    <dependencies> 
 
        <!-- Spring jars --> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-core</artifactId> 
            <version>${spring.version}</version> 
            <exclusions> 
                <exclusion> 
                    <groupId>commons-logging</groupId> 
                    <artifactId>commons-logging</artifactId> 
                </exclusion> 
            </exclusions> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-beans</artifactId> 
            <version>${spring.version}</version> 
            <exclusions> 
                <exclusion> 
                    <groupId>commons-logging</groupId> 
                    <artifactId>commons-logging</artifactId> 
                </exclusion> 
            </exclusions> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context</artifactId> 
            <version>${spring.version}</version> 
            <exclusions> 
                <exclusion> 
                    <groupId>commons-logging</groupId> 
                    <artifactId>commons-logging</artifactId> 
                </exclusion> 
            </exclusions> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-web</artifactId> 
            <version>${spring.version}</version> 
            <exclusions> 
                <exclusion> 
                    <groupId>commons-logging</groupId> 
                    <artifactId>commons-logging</artifactId> 
                </exclusion> 
            </exclusions> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-test</artifactId> 
            <version>${spring.version}</version> 
            <scope>test</scope> 
            <exclusions> 
                <exclusion> 
                    <groupId>commons-logging</groupId> 
                    <artifactId>commons-logging</artifactId> 
                </exclusion> 
            </exclusions> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-aop</artifactId> 
            <version>${spring.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-webmvc</artifactId> 
            <version>${spring.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-context-support</artifactId> 
            <version>${spring.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-orm</artifactId> 
            <version>${spring.version}</version> 
            <type>jar</type> 
            <scope>compile</scope> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework</groupId> 
            <artifactId>spring-jdbc</artifactId> 
            <version>${spring.version}</version> 
            <type>jar</type> 
            <scope>runtime</scope> 
        </dependency> 
        <dependency> 
            <groupId>taglibs</groupId> 
            <artifactId>standard</artifactId> 
            <version>1.1.2</version> 
        </dependency> 
        <dependency> 
            <groupId>javax.servlet</groupId> 
            <artifactId>jstl</artifactId> 
            <version>1.2</version> 
        </dependency> 
        <dependency> 
            <groupId>commons-io</groupId> 
            <artifactId>commons-io</artifactId> 
            <version>2.1</version> 
        </dependency> 
        <!-- end of Spring jars --> 
 
        <!-- Shiro security --> 
        <dependency> 
            <groupId>org.apache.shiro</groupId> 
            <artifactId>shiro-core</artifactId> 
            <version>${shiro.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.shiro</groupId> 
            <artifactId>shiro-web</artifactId> 
            <version>${shiro.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.shiro</groupId> 
            <artifactId>shiro-cas</artifactId> 
            <version>${shiro.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.shiro</groupId> 
            <artifactId>shiro-spring</artifactId> 
            <version>${shiro.version}</version> 
        </dependency> 
        <!-- end of Shiro security --> 
 
        <!-- Json --> 
        <dependency> 
            <groupId>org.codehaus.jackson</groupId> 
            <artifactId>jackson-core-lgpl</artifactId> 
            <version>1.8.1</version> 
        </dependency> 
        <dependency> 
            <groupId>org.codehaus.jackson</groupId> 
            <artifactId>jackson-mapper-lgpl</artifactId> 
            <version>1.8.1</version> 
        </dependency> 
        <dependency> 
            <groupId>net.sf.json-lib</groupId> 
            <artifactId>json-lib</artifactId> 
            <version>${jsonlib.version}</version> 
            <classifier>jdk15</classifier> 
        </dependency> 
        <!-- end of Json --> 
 
        <!-- Hibernate4 --> 
        <dependency> 
            <groupId>org.hibernate</groupId> 
            <artifactId>hibernate-core</artifactId> 
            <version>${hibernate.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.hibernate</groupId> 
            <artifactId>hibernate-entitymanager</artifactId> 
            <version>${hibernate.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>javax.xml.bind</groupId> 
            <artifactId>jaxb-api</artifactId> 
            <version>2.1</version> 
            <exclusions> 
                <exclusion> 
                    <groupId>javax.xml.stream</groupId> 
                    <artifactId>stax-api</artifactId> 
                </exclusion> 
            </exclusions> 
        </dependency> 
        <dependency> 
            <groupId>com.sun.xml.bind</groupId> 
            <artifactId>jaxb-impl</artifactId> 
            <version>2.1.10</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.openejb</groupId> 
            <artifactId>javaee-api</artifactId> 
            <version>5.0-1</version> 
            <!--<scope>provided</scope> --> 
        </dependency> 
        <dependency> 
            <groupId>commons-dbcp</groupId> 
            <artifactId>commons-dbcp</artifactId> 
            <version>1.4</version> 
            <!--<scope>provided</scope> --> 
        </dependency> 
        <!-- end of Hibernate4 --> 
 
        <!-- logging --> 
        <dependency> 
            <groupId>org.slf4j</groupId> 
            <artifactId>slf4j-api</artifactId> 
            <version>${slf4j.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.slf4j</groupId> 
            <artifactId>jcl-over-slf4j</artifactId> 
            <version>${slf4j.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.slf4j</groupId> 
            <artifactId>slf4j-log4j12</artifactId> 
            <version>${slf4j.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>log4j</groupId> 
            <artifactId>log4j</artifactId> 
            <version>${log4j.version}</version> 
            <!--<scope>provided</scope> --> 
        </dependency> 
        <!-- end of logging --> 
 
        <!-- provided --> 
        <dependency> 
            <groupId>javax.servlet</groupId> 
            <artifactId>servlet-api</artifactId> 
            <version>2.5</version> 
            <scope>compile</scope> 
        </dependency> 
        <dependency> 
            <groupId>cglib</groupId> 
            <artifactId>cglib</artifactId> 
            <version>${cglib.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>javax.servlet.jsp</groupId> 
            <artifactId>jsp-api</artifactId> 
            <version>2.1</version> 
        </dependency> 
        <!-- end of provided --> 
 
        <!-- Test --> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>${junit.version}</version> 
            <scope>test</scope> 
        </dependency> 
        <!-- end of Test --> 
 
        <!-- Database --> 
        <dependency> 
            <groupId>com.mchange</groupId> 
            <artifactId>c3p0</artifactId> 
            <version>0.9.5-pre3</version> 
            <type>jar</type> 
            <scope>compile</scope> 
        </dependency> 
        <dependency> 
            <groupId>mysql</groupId> 
            <artifactId>mysql-connector-java</artifactId> 
            <version>5.1.21</version> 
            <type>jar</type> 
            <scope>compile</scope> 
        </dependency> 
        <!-- end of Database --> 
 
        <!-- velocity --> 
        <dependency> 
            <groupId>org.apache.velocity</groupId> 
            <artifactId>velocity</artifactId> 
            <version>${velocity.version}</version> 
        </dependency> 
        <!-- end of velocity --> 
 
        <!-- fileUpload --> 
        <dependency> 
            <groupId>commons-fileupload</groupId> 
            <artifactId>commons-fileupload</artifactId> 
            <version>1.2.2</version> 
        </dependency> 
        <!--io --> 
        <dependency> 
            <groupId>commons-io</groupId> 
            <artifactId>commons-io</artifactId> 
            <version>2.4</version> 
        </dependency> 
        <!-- end of fileUpload --> 
 
        <!-- tiles --> 
        <dependency> 
            <groupId>org.apache.tiles</groupId> 
            <artifactId>tiles-extras</artifactId> 
            <version>${tiles.extras.version}</version> 
            <!-- it will include the rest --> 
        </dependency> 
        <!-- end of tiles --> 
    </dependencies> 
 
    <!-- Custom repositories --> 
    <repositories> 
        <repository> 
            <id>springsource-milestones</id> 
            <name>SpringSource Milestones Proxy</name> 
            <url>https://oss.sonatype.org/content/repositories/springsource-milestones</url> 
        </repository> 
        <repository> 
            <id>CentralM1</id> 
            <name>sonatype maven repository</name> 
            <url>https://repository.sonatype.org/content/shadows/centralm1/</url> 
        </repository> 
    </repositories> 
    <!-- end of Custom repositories --> 
 
    <!-- Build support --> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.mortbay.jetty</groupId> 
                <artifactId>maven-jetty-plugin</artifactId> 
                <version>7.0.0.pre5</version> 
                <configuration> 
                    <encoding>UTF-8</encoding> 
                </configuration> 
            </plugin> 
        </plugins> 
    </build> 
    <!-- end of Build support --> 
</project> 
接下来:web.xml
[html] view plain copy
<?xml version="1.0" encoding="UTF-8" ?> 
<web-app xmlns="http://java.sun.xml/ns/javaee" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                                     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <display-name>haiercloud-webapp</display-name> 
    <!-- context-params --> 
    <!-- webAppRootKey --> 
    <context-param> 
        <param-name>webAppRootKey</param-name> 
        <param-value>haiercloud-webapp</param-value> 
    </context-param> 
    <context-param> 
        <param-name>log4jConfigLocation</param-name> 
        <param-value>classpath:log4j.properties</param-value> 
    </context-param> 
    <!-- Spring configuration file location --> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
            /WEB-INF/classes/applicationContext.xml, 
            classpath*:applicationContext.xml 
        </param-value> 
    </context-param> 
    <!-- end of context-params --> 
     
    <!-- Spring configuration --> 
    <!-- filters --> 
    <!-- Spring encoding filter --> 
    <filter> 
        <filter-name>encodingFilter</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>UTF-8</param-value> 
        </init-param> 
    </filter> 
    <!-- Shiro security filter --> 
    <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.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> 
    </filter> 
    <!-- Make sure any request you want accessible to Shiro is filtered. /* catches all --> 
    <!-- requests. Usually this filter mapping is defined first (before all others) to       --> 
    <!-- ensure that Shiro works in subsequent filters in the filter chain: --> 
    
    <!-- Encoding URL mapping --> 
    <filter-mapping> 
        <filter-name>encodingFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
     <filter-mapping> 
        <filter-name>shiroFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <!-- end of filters --> 
     
    <!-- Servlet configuration --> 
    <servlet> 
        <servlet-name>SpringDispatcher</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <init-param> 
            <param-name>contextConfigLocation</param-name> 
            <param-value>/WEB-INF/dispathcer-servlet.xml</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
     
    <servlet-mapping> 
        <servlet-name>default</servlet-name> 
        <url-pattern>*.png</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
        <servlet-name>SpringDispatcher</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
     
    <session-config> 
        <session-timeout>30</session-timeout> 
    </session-config> 
    <!-- end of Servlet configuration --> 
     
    <!-- Listeners --> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
        <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class> 
    </listener> 
    <listener> 
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
    <!-- end of Listeners --> 
     
    <!-- jsp configuration --> 
    <jsp-config> 
        <jsp-property-group> 
            <display-name>JSPConfiguration</display-name> 
            <url-pattern>*.jsp</url-pattern> 
            <el-ignored>false</el-ignored> 
            <page-encoding>utf-8</page-encoding> 
        </jsp-property-group> 
    </jsp-config> 
    <!-- end of jsp configuration --> 
     
    <!-- Error page definition --> 
    <!--  
    <error-page> 
        <exception-type>java.lang.Throwable</exception-type> 
        <location>/static_pages/error_pages/500.jsp</location> 
    </error-page> 
    <error-page> 
        <error-code>500</error-code> 
        <location>/static_pages/error_pages/500.jsp</location> 
    </error-page> 
    <error-page> 
        <error-code>404</error-code> 
        <location>/static_pages/error_pages/404.jsp</location> 
    </error-page> 
    --> 
    <!-- end of Error page definition --> 
     
    <!-- Welcome page definition --> 
    <!--   
    <welcome-file-list> 
        <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    --> 
    <!-- end of Welcome page definition --> 
</web-app>

猜你喜欢

转载自obeyheartchen.iteye.com/blog/2288604