当前访客身份:游客 [ 登录 | 加入开源中国 ]
当前访客身份: 游客 [ 登录 | 加入开源中国 ]
Carlos_Tse Carlos_Tse
java工程师(垂直检索、爬虫及信息聚合领域)开源项目heyDr发起人
最新评论
访客统计
  • 0
  • 20
  • 32
  • 189
  • 4761

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

发表于3 年 前(2012-02-06 00:00)   阅读(1982) | 评论(7 0人收藏此文章,
1

shiro配置:

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

<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设置:

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

<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 配置:

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

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

分享到: 1
声明:OSCHINA 博客文章版权属于作者,受法律保护。未经作者同意不得转载。

评论

Carlos_Tse

插入: 表情 开源软件

关闭插入表情