freemarker+shiro+spring mvc+spring+mybatis整合

shiro配置:css

<?xml version="1.0" encoding="UTF-8"?>html

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">web

<context:component-scan base-package="security" />
<!-- 配置數據源 -->
<bean id="securityDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driverClassName}"/>
	<property name="url" value="${jdbc.security.url}"/>
	<property name="username" value="${jdbc.security.username}"/>
	<property name="password" value="${jdbc.security.password}"/>
	<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
	<property name="minEvictableIdleTimeMillis" value="18000000"/>
	<property name="testWhileIdle" value="true"/>
	<property name="testOnBorrow" value="true"/>
	<property name="validationQuery" value="select 1"/>
</bean>

<!-- 配置myBatis -->
<bean id="securitySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="securityDataSource"/>
	<property name="configLocation" value="classpath:mybatis-config.xml"/>
	<property name="mapperLocations">
		<list>
			<value>classpath:security/shiro/mapper/ShiroMapper.xml</value>
		</list>
	</property>
	<!--<property name="mapperLocations" value="classpath:security/shiro/mapper/ShiroMapper.xml"/>-->
</bean>

<!-- 配置SqlSession -->
<bean id="securitySqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg index="0" ref="securitySqlSessionFactory"/>
</bean>
   
<!-- shiro配置 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
	<property name="securityManager" ref="securityManager" />
	<property name="loginUrl" value="/exit" />
	<property name="successUrl" value="/center" />
	<property name="unauthorizedUrl" value="/exit" />
	<property name="filters">
		<map>
			<entry key="authc" value-ref="formAuthenticationFilter" />
		</map>
	</property>
	<property name="filterChainDefinitions">
		<value>
			/login*=anon
			/logout*=anon
			/user/*=authc
			<!-- # 權限配置示例 /security/account/view.do=authc,perms[SECURITY_ACCOUNT_VIEW] 
				/** = authc -->
		</value>
	</property>
</bean>

<bean id="myShiroRealm" class="security.shiro.logic.MyShiroRealm">
	<!-- businessManager 用來實現用戶名密碼的查詢 -->
	<!--shiro緩存設置-->
	<property name="cacheManager" ref="shiroCacheManager" /> 
</bean>

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
	<property name="realm" ref="myShiroRealm" />
</bean>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

<bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
</bean>

<bean id="formAuthenticationFilter"	class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter" />

</beans>spring

spring mvc設置:sql

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

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">apache

<context:component-scan base-package="./*" />

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/> 
    <property name="suffix" value=".html"/>
    <property name="contentType" value="text/html;charset=utf-8"/>
</bean>
<!-- freemarker config -->
<bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
	<property name="location" value="classpath:freemarker.properties"/>
</bean>
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
	<property name="freemarkerSettings" ref="freemarkerConfiguration"/>
	<property name="templateLoaderPath" value="/templates/"/>
</bean>
<!-- freemarker視圖設置 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
	<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
	<property name="order" value="1"/>
	<property name="suffix" value=".ftl"/>
	<property name="contentType" value="text/html;charset=utf-8"/>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
		<list>
       		<ref bean="mappingJacksonHttpMessageConverter"/>
		</list>
    </property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
    <property name="supportedMediaTypes">
        <list>
            <value>text/html;charset=utf-8</value>
        </list>
    </property>
</bean>

<!-- FileUpload Support -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="1000000" />
</bean>

<!-- Support static resource -->
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />

<mvc:default-servlet-handler/>

<!-- Support json ResponseBody 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	<property name="messageConverters">
		<list>
			<ref bean="mappingJacksonHttpMessageConverter" />
		</list>
	</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
-->


<!-- 錯誤攔截器,統一處理 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
	<property name="exceptionMappings">
		<props> 
	    	<prop key="org.apache.shiro.authc.IncorrectCredentialsException">redirect:index.html</prop>
		</props>
	</property>
	<property name="defaultErrorView">
		<value>error</value>
	</property>
</bean>


<!-- shiro securityManager,必需要在spring mvc配置文件,若是在其餘地方將不生效 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" />
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
	<property name="securityManager" ref="securityManager" />
</bean>

</beans>json

spring 配置:spring-mvc

<?xml version="1.0" encoding="UTF-8"?>緩存

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

<!-- 註解掃描區域@Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="com.pms" />
<!-- 啓用註解enable autowiring annotation -->
<context:annotation-config/>

<!-- properties文件讀取 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 事務的傳播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="add*" propagation="REQUIRED"/>
		<tx:method name="del*" propagation="REQUIRED" />
		<tx:method name="update*" propagation="REQUIRED"/>
		<tx:method name="*" read-only="true"/>
	</tx:attributes>
</tx:advice>

<!-- 哪些類哪些方法使用事務 -->
<aop:config>
	<aop:pointcut id="allManagerMethod" expression="execution(* com.pms.*.*.*(..))"/>
	<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>

<!-- 配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driverClassName}"/>
	<property name="url" value="${jdbc.url}"/>
	<property name="username" value="${jdbc.username}"/>
	<property name="password" value="${jdbc.password}"/>
	<property name="timeBetweenEvictionRunsMillis" value="3600000"/>
	<property name="minEvictableIdleTimeMillis" value="18000000"/>
	<property name="testWhileIdle" value="true"/>
	<property name="testOnBorrow" value="true"/>
	<property name="validationQuery" value="select 1"/>
</bean>

<!-- 配置myBatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"/>
	<property name="configLocation" value="classpath:mybatis-config.xml"/>
	<property name="mapperLocations" value="classpath:com/pms/model/mapper/*Mapper.xml"/>
</bean>

<!-- 配置SqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
	<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

<!-- 引入shiro配置 -->
<import resource="classpath*:security/shiro.xml" />

</beans>

web.xml配置: 在此輸入圖片描述

相關文章
相關標籤/搜索