Spring+SpringMVC+SpringDataJPA+shiro框架集成

Spring+SpringMVC+SpringDataJPA+shiro框架集成

一.导包

二.准备db.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=数据库
jdbc.username=用户名
jdbc.password=密码

三. 准备spring配置文件

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

<!--扫描Spring的注解-->
<context:component-scan base-package="cn.pj.aisell.service"/>
<!--读取properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!--配置数据源-->
<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}"/>


</bean>



<!--配置entityManagerFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--扫描JPA的注解,比如@Entity-->
    <property name="packagesToScan" value="cn.pj.aisell.domain"/>
    <!--配置适配器,因为jpa是规范,实现太多了,配置适配器指定使用的是哪个实现-->
    <property name="jpaVendorAdapter">
        <!--Hibernate实现-->
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <!--数据库方言-->
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
            <!--建表策略,值只有true和false,true表示update,false表示什么都不做-->
            <property name="generateDdl" value="false"/>
            <property name="showSql" value="true"/>

        </bean>
    </property>

</bean>

<!--配置事务-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>

</bean>

<!--配置支持事务的注解,transaction-manager="transactionManager"名字要与上面的一样,默认找transactionManager这个名字,这里不写也可以-->
<tx:annotation-driven transaction-manager="transactionManager"/>

<jpa:repositories base-package="cn.pj.aisell.repository" entity-manager-factory-ref="entityManagerFactory"
                  transaction-manager-ref="transactionManager" factory-class="cn.pj.aisell.repository.BaseRepositoryFactoryBean"/>

<!--导入shiro配置文件-->
<import resource="classpath:applicationContext-shiro.xml"/>

四. 准备SpringMVC配置文件

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

<!--扫描controller-->
<context:component-scan base-package="cn.pj.aisell.web"/>
<!--开启静态资源方法-->
<mvc:default-servlet-handler/>
<!--扫描Springmvc注解支持-->
<!-- Spring MVC 配置 -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes">
                <list>
                    <value>application/json; charset=UTF-8</value>
                    <value>application/x-www-form-urlencoded; charset=UTF-8</value>
                </list>
            </property>
            <!-- No serializer:配置 objectMapper 为我们自定义扩展后的 CustomMapper,解决了返回对象有关系对象的报错问题 -->
            <property name="objectMapper">
                <bean class="cn.pj.aisell.common.CustomMapper"></bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>


<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<!--文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <!--上传文件最大尺寸-->
    <property name="maxUploadSize" value="1048576"/>
</bean>

五. 准备shiro配置文件(shiro与spring集成)

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

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

<bean id="aisellRealm" class="cn.pj.aisell.shiro.AisellRealm">
    <property name="credentialsMatcher">
        <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
            <property name="hashAlgorithmName" value="MD5"/>
            <property name="hashIterations" value="10"/>
        </bean>
    </property>
</bean>

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<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>

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/login"/>
    <property name="successUrl" value="/main"/>
    <property name="unauthorizedUrl" value="/s/unauthorized.jsp"/>
    <property name="filterChainDefinitionMap" ref="myfilterChainDefinitionMap"/>
    <!--<property name="filterChainDefinitions">

        <value>
            /login = anon
            /images = anon
            /dept/index = perms[dept:index]
            /** = authc
        </value>
    </property>-->
</bean>
<bean id="myfilterChainDefinitionMap" factory-bean="filterChainDefinitionMapBuilder" factory-method="createFilterChainDefinitionMap"/>
<bean id="filterChainDefinitionMapBuilder" class="cn.pj.aisell.shiro.FilterChainDefinitionMapBuilder"/>

六. 配置web.xml

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

dispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:applicationContext-mvc.xml 1 dispatcherServlet / shiroFilter org.springframework.web.filter.DelegatingFilterProxy targetFilterLifecycle true shiroFilter /* org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true characterEncodingFilter /* openEntityManagerInViewFilter org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter openEntityManagerInViewFilter /*
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值