freemarker+shiro+spring mvc+spring+mybatis整合
freemarker+shiro+spring mvc+spring+mybatis整合
Carlos_Tse 发表于6年前
freemarker+shiro+spring mvc+spring+mybatis整合
  • 发表于 6年前
  • 阅读 6670
  • 收藏 24
  • 点赞 2
  • 评论 7

shiro配置:

<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" />

spring mvc设置:

<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>

spring 配置:

<!-- 注解扫描区域@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" />

web.xml配置: 在此输入图片描述

共有 人打赏支持
Carlos_Tse
粉丝 25
博文 32
码字总数 8085
作品 1
评论 (7)
隔山打牛
有开源项目吗?能发一份给我吗?chenlisong1021@163.com
老码农008
有开源项目吗?能发一份给我吗?liujf0401@qq.com
凌山雪峰
同求啊1478489533@qq.com
豆豆熊
同求
edd1225
同求
edd1225
123567549@qq.com
guankai
请问有工程代码么,能否发一份。谢谢 dijkstra@qq.com
×
Carlos_Tse
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: