所有源代码能够訪问我的GitHub
有空没空的稻谷了几天,最终前后台跑通了,提供一套可用的配置文件。
(因为与extjs整合,spring security的登录须要重写原handler。会在后面补上)
首先是pom。主要有junit。spring(mvc + security)。log4j以及jdbc(mysql)依赖。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.paris</groupId>
<artifactId>fast-contacts</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>fast-contacts Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jstl.version>1.2</jstl.version>
<junit.version>4.11</junit.version>
<spring.version>4.0.0.RELEASE</spring.version>
<servlet.version>3.0.1</servlet.version>
<jsp.version>2.2</jsp.version>
<tomcat.plugin.version>2.2</tomcat.plugin.version>
<slf4j.version>1.5.6</slf4j.version>
<spring.security.version>3.2.4.RELEASE</spring.security.version>
<lombok.version>1.12.6</lombok.version>
<log4j.version>1.2.16</log4j.version>
<org.json.version>20090211</org.json.version>
<common.dbcp.verison>1.4</common.dbcp.verison>
<commmons.pool.version>1.6</commmons.pool.version>
<mysql.connector.verison>5.1.6</mysql.connector.verison>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring webmvc相关jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring security configuration -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${common.dbcp.verison}</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>${commmons.pool.version}</version>
</dependency> <!-- spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency> <!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency> <!-- org json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${org.json.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.verison}</version>
</dependency>
</dependencies> <build>
<finalName>fast-contacts</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.plugin.version}</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<url>http://localhost:8080/manager/html</url>
<server>tomcat7</server>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml 主要是spring和spring security。
我用的html。假设喜欢jsp的,能够去掉html的filterMapping
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/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_3_0.xsd"
version="3.0" metadata-complete="false">
<display-name>Fast contacts</display-name> <!--
- Location of the XML file that defines the root application context
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-security.xml
classpath:spring-servlet.xml
</param-value>
</context-param> <!-- Nothing below here needs to be modified --> <context-param>
<param-name>webAppRootKey</param-name>
<param-value>servletapi.root</param-value>
</context-param> <filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter> <filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<!--bak <url-pattern>/*</url-pattern> -->
<url-pattern>/*</url-pattern>
</filter-mapping> <!--
- Loads the root application context of this web app at startup.
- The application context is then available via
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
spring-security.xml 不全,可是可用。
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd"> <!-- no need to filter login page -->
<security:http pattern="/login" security="none"/>
<!-- 不要过滤图片等静态资源,当中**代表能够跨越文件夹。*不能够跨越文件夹。 -->
<security:http pattern="**.jpg" security="none" />
<security:http pattern="**.png" security="none" />
<security:http pattern="**.gif" security="none" />
<security:http pattern="**.css" security="none" />
<security:http pattern="**.js" security="none" />
<security:http auto-config="true">
<!--
<security:intercept-url pattern="/auth/login" access="permitAll"/>
<security:intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/> -->
<security:intercept-url pattern="/login.html" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/*" access="ROLE_USER"/>
<security:form-login
login-page="/login.html"
/>
<security:logout delete-cookies="JSESSIONID"/>
<security:remember-me key="fast-contacts"/>
<security:session-management invalid-session-url="/invalidSession.html"></security:session-management>
</security:http> <security:authentication-manager>
<security:authentication-provider user-service-ref="fastUserDetailsService">
<security:password-encoder ref="passwdEcoder"/>
<!--
<security:user-service>
<security:user name="test" password="test" authorities="ROLE_USER"/>
<security:user name="admin" password="admin" authorities="ROLE_USER,ROLE_ADMIN"/>
</security:user-service>-->
</security:authentication-provider>
</security:authentication-manager> <b:bean id="fastUserDetailsService"
class="com.chinadreamer.contacts.filter.FastUserDetailsService">
</b:bean>
<b:bean id="passwdEcoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>
</b:beans>
spring-servlet.xml db连接是mysql driver + jdbc
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- Scans the classpath of this application for @Components to deploy as
beans -->
<context:component-scan base-package="com.chinadreamer.contacts.*,demo.*">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<mvc:annotation-driven /> <!-- 静态资源映射 -->
<mvc:resources mapping="/comp/**" location="/WEB-INF/comp/"/>
<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/"/>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<mvc:resources mapping="/view/**" location="/WEB-INF/view/"/> <!-- 对模型视图名称的解析。在请求时模型视图名称加入前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/"
p:suffix=".html" /> <!-- 配置数据源 -->
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
id = "dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/fastcontacts"></property>
<property name="username" value="root"/>
<property name="password" value="Password2"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="10"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="60000"/>
<!-- 最大空暇值.当经过一个高峰时间后。连接池能够慢慢将已经用不到的连接慢慢释放一部分,一直降低到maxIdle为止 -->
<property name="maxIdle" value="20"/>
<!-- 最小空暇值.当空暇的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="5"/>
</bean>
<!-- transaction -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务拦截器 -->
<!-- 配置事务拦截器 -->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="request*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<property name="transactionInterceptor" ref="transactionInterceptor"/>
</bean> <!-- jdbc template -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean> </beans>