Maven配置Spring+Hibernate Shiro權限控制項目

     前言:在Eclipse中安裝好Maven插件,而後建立一個Sample項目。在Eclipse中檢出Shiro的官方演示樣例。地址http://svn.apache.org/repos/asf/shiro/trunk/samples/。參考spring-hibernate項目配置。html

由於以前對Maven的認識不足因此今天專門配置來了解其工做機制,如下是我依照SVN上配置的演示樣例。java

1.建立Maven項目並複製配置文件

如圖所看到的:web


注:文件的位置與官網演示樣例一致。spring

2.配置pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<!-- <parent> <groupId>org.apache.shiro.samples</groupId> <artifactId>shiro-samples</artifactId> 
		<version>1.2.3</version> <relativePath>../pom.xml</relativePath> </parent> -->

	<modelVersion>4.0.0</modelVersion>
	<groupId>com.boonya.shiro</groupId>
	<artifactId>boonyashiro-springhibernate</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<!-- Java代碼依賴環境包 -->
	<dependencies>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.6.0</version>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.transaction</groupId>
			<artifactId>jta</artifactId>
			<version>1.1</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache-core</artifactId>
			<version>2.4.3</version>
		</dependency>

		<dependency>
			<groupId>org.hsqldb</groupId>
			<artifactId>hsqldb</artifactId>
			<version>2.3.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-all</artifactId>
			<version>${org.apache.shiro.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate</artifactId>
			<version>${org.hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${org.hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>${org.hibernate.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-annotations</artifactId>
			<version>3.4.0.GA</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-c3p0</artifactId>
			<version>${org.hibernate.version}</version>
		</dependency>

		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${org.hibernate.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-portlet</artifactId>
			<version>${org.springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

	<!-- 項目編譯 -->

	<build>
		<!-- 項目資源路徑引入 -->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>

		<!-- 項目打包插件 -->
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compile-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surfire-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>

	</build>

	<!-- 配置可變版本 (Hibernate、spring、shiro) -->
	<properties>
		<javax.servlet.version>3.1.0</javax.servlet.version>
		<org.apache.shiro.version>1.2.3</org.apache.shiro.version>
		<org.hibernate.version>3.6.10.Final</org.hibernate.version>
		<org.springframework.version>3.0.2.RELEASE</org.springframework.version>
		<developer.organization><![CDATA[scengine]]></developer.organization>
	</properties>


</project>

注:${*.version}此處實現了Hibernate和Spring版本號統一管理。曾經看到別人用過。不知道在哪裏實現版本號控制,今天網上查閱了很多資料最終明確怎麼作了。

3.改動web.xml文件

由於演示樣例中的*-servlet.xml,此處改成了boonyashiro-servlet.xml。此中內容例如如下:sql

<?

xml version="1.0" encoding="UTF-8"?express

> <!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more contributor license agreements. See the NOTICE file ~ distributed with this work for additional information ~ regarding copyright ownership. The ASF licenses this file ~ to you under the Apache License, Version 2.0 (the ~ "License"); you may not use this file except in compliance ~ with the License. You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, ~ software distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the License for the ~ specific language governing permissions and limitations ~ under the License. --> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" metadata-complete="false" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>boonyashiro-springhibernate.webapp.root</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/s/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/s/*</url-pattern> </filter-mapping> <span style="color:#ff0000;"><strong><servlet> <servlet-name>boonyashiro</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>boonyashiro</servlet-name> <url-pattern>/s/*</url-pattern> </servlet-mapping></strong></span> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <error-page> <error-code>401</error-code> <location>/unauthorized.jsp</location> </error-page> </web-app>apache


注:此處關鍵處是org.springframework.web.servlet.DispatcherServlet的配置,它的servlet-name與*-servlet.xml是一致的,*表明servlet的名字。

5.Maven依賴不成功處理方法

一、在google 中搜尋 XX Maven repository。api

二、下載jar失敗:刪除.m2/repository/下相應文件夾的*.pom.lastUpdate(這樣的文件一旦存在則Maven將不會再更新下載jar),可以手動下載相應版本號扔到改相應路徑下。mvc

注:刪除的時候假設刪除不掉需要關閉IDE刪除後又一次啓動就可以了。app

6.Shiro配置成果展現

一、登陸


二、主頁


三、控制檯輸出


僅僅要有足夠的信心,要作成一件事是很是easy的。

7.安裝打包

        很久沒用Maven了,有個小小的疑惑,爲何個人項目右鍵Run as沒有 mvn package命令了呢?個人Maven是org.apache.maven 3.2.2,多是新版本號將命令mvn package囊括在了mvn intall上了。我用mvn install打出了程序包,效果跟以前用的mvn package 一致。下圖是個人打包項目結構截圖:


用過Maven的都知道target文件夾是項目編譯打包後的項目文件存放的地方。

相關文章
相關標籤/搜索