Shiro 整合 SpringMVC 配置文件详解篇

首先是web.xml这里面主要配置mvc以及加载shiro

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:config/applicationContext.xml
            classpath:config/cache-context.xml
            classpath:config/shiro-context.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

<!-- shiroFilter -->
    <!--shiro主过滤器的配置,后面会用到  --> 
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--springMVC-->
    <servlet>
        <servlet-name>MyMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:config/SpringMVC/servlet-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyMvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MyMvc</servlet-name>
        <url-pattern>*.jhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MyMvc</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>MyMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

------------------------------------分割线--------------------------------------------------

applicationContext.xml

这个大家都熟悉不多说,看注释就行

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd "
    default-lazy-init="true"
    >

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
<!--             classpath:config/ -->
                <value>classpath:config/jdbc.properties</value>
            </list>
        </property>
    </bean>

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="autoCommitOnClose" value="true"/>
        <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>
        <property name="initialPoolSize" value="${cpool.minPoolSize}"/>
        <property name="minPoolSize" value="${cpool.minPoolSize}"/>
        <property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
        <property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
        <property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
        <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
    </bean>
    
<!--     <jee:jndi-lookup id="dataSource1" jndi-name="jdbc/LiferayPool2" resource-ref="true"/> -->
<!--     <jee:jndi-lookup id="dataSource1" jndi-name="jdbc/LiferayPool" resource-ref="true"/> -->
    
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>
    
    
<!--     org.springframework.orm.hibernate3.LocalSessionFactoryBean -->
<!--采用注解好些-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>

        <property name="hibernateProperties">
            <value>
            hibernate.dialect=${hibernate.dialect}
            hibernate.show_sql=true
            hibernate.format_sql=true
            hibernate.query.substitutions=true 1, false 0
            hibernate.jdbc.batch_size=20
<!--             hibernate.cache.use_query_cache=true -->
            </value>
        </property>

        <property name="cacheProvider">
            <ref local="cacheProvider"/>
        </property>
        <property name="lobHandler">
            <ref bean="lobHandler" />
        </property>
        <property name="packagesToScan">
            <list>
                <value>org.pag.sys.pojo</value>
            </list>
        </property>
    </bean>
    <bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/>
    
    <bean id="cacheProvider" class="org.core.ehcache.SpringEhCacheProvider">
        <property name="configLocation">
            <value>classpath:config/ehcache/ehcache-hibernate.xml</value>
        </property>
        <property name="diskStoreLocation">
            <value>/WEB-INF/cache/hibernate</value>
        </property>
    </bean>
    
<!--     <bean id="treeInterceptor" class="com.jeecms.common.hibernate3.TreeIntercptor"/> -->
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <tx:advice id="txadvice" transaction-manager="transactionManager" >
    <tx:attributes>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>    
    <!-- 定义一个切面 -->
    <aop:config>
    <!-- 定义一个切如点 -->
    <aop:pointcut expression="execution(* org.pag.*.service.impl.*.*(..))" id="mypointcut"/>
    <aop:advisor advice-ref="txadvice" pointcut-ref="mypointcut"/>
    </aop:config>
    
    <!-- 是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。 即解决了@Controller标识的类的bean的注入和使用。 -->
    
    <!-- 进行扫描,以完成Controller创建和自动依赖注入的功能 -->
    <context:component-scan base-package="org.pag" >
        <!-- 扫描Controller注解Bean 强烈建议Controller层扫描和其他注解扫描分开 其他类型注解(@Controller
            @Service , @Configuration, etc.)扫描 由applicationContext-common.xml中配置 -->
        <!-- include-filter 包含 exclude-filter 排除 -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Service"  />
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Repository" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

------------------------------------分割线--------------------------------------------------

shiro 登场 shiro-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
    default-lazy-init="true">
    
    <!--shiro主过滤器的配置,这里的名字和web中的要对应  -->  
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- 集成上安全管理器 -->  
        <property name="securityManager" ref="securityManager" />
        <!-- 全局默认的未登录跳转 验证路径 -->
        <property name="loginUrl" value="/login.jhtml" />
        <property name="successUrl" value="/" />
        <property name="filters">
            <util:map>
                <!-- 基于表单认证的过滤器 -->
                <entry key="authc" value-ref="authcFilter" />
                <!-- shiro内置过滤器 表示必须存在用户,当登入操作时不做检查 -->
                <entry key="user" value-ref="userFilter" />
                <!-- shiro内置过滤器 用户退出 -->
                <entry key="logout" value-ref="logoutFilter" />
            </util:map>
        </property>
        <!-- 过滤器链,对URL配置过滤规则 -->  
        <!--anon匿名 authc登录认证  user用户已登录 logout退出filter-->
        <property name="filterChainDefinitions">
            <value>
                *.jsp = anon
                /login.jhtml = authc
                /logout.jhtml = logout
                /admin/login.do = authc
                /admin/logout.do = logout
                /*.htm = user
                /*/**.htm = user
                /*/*/**.htm = user
            </value>
        </property>
    </bean>
    <!-- Shiro Filter -->    
    <bean id="adminUrlBean" class="org.core.security.CmsAdminUrl">
    <!-- 自定义字段 登录地址  用在CmsAuthenticationFilter 158行 判断 是不是请求的一个登录地址 -->
        <property name="adminLogin" value="/admin/login.do"/>
        <!-- 自定字段  用在 CmsAuthenticationFilter 72行 判断是 不是后台管理的请求-->
        <property name="adminPrefix" value="/admin/"/>
    </bean>
    <bean id="authcFilter" class="org.core.security.CmsAuthenticationFilter" parent="adminUrlBean">
        <!-- 自定义字段   用在 CmsAuthenticationFilter 142行  替换默认的 successUrl 认证后的url跳转 -->
        <property name="adminIndex" value="/admin/index.do"/>
        <property name="adminIndexI" value="/"/>
        <property name="adminIndexII" value="/xx/xxx/"/>
    </bean>
    <bean id="userFilter" class="org.core.security.CmsUserFilter" parent="adminUrlBean"/>
    <bean id="logoutFilter" class="org.core.security.CmsLogoutFilter" parent="adminUrlBean"/>
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="authorizingRealm" />
        <property name="cacheManager" ref="shiroEhcacheManager"/>
    </bean>
    <bean id="authorizingRealm" class="org.core.security.CmsAuthorizingRealm">
        <property name="credentialsMatcher">
           <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!--密码加盐 md5-->
               <property name="hashAlgorithmName" value="MD5"/>
             <!--   true means hex encoded, false means base64 encoded -->
               <property name="storedCredentialsHexEncoded" value="true"/>
               <!-- 迭代次数 -->
               <property name="hashIterations" value="1" />
           </bean>
        </property> 
        <!-- <property name="userSvc" ref="userSvc" /> -->
    </bean>
<!--缓存-->
    <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile">
                <value>classpath:config/ehcache/ehcache-shiro.xml</value>
        </property>
    </bean>
    
    <!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->
    <!-- the lifecycleBeanProcessor has run: -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>

最后是mvc了

------------------------------------分割线--------------------------------------------------

servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
    default-lazy-init="true">
    
    
    
    

    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> -->
    <!-- <property name="interceptors"> -->
    <!-- <list> -->
    <!-- <ref bean="testttt"/> -->
    <!-- </list> -->
    <!-- </property> -->
    <!-- </bean> -->
    <!-- 前台页面拦截器 -->
    <!-- <bean id="testttt" class="com.jeecms.test.TestContextInterceptor"/> -->

    <!-- 是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。 即解决了@Controller标识的类的bean的注入和使用。 -->
    <context:annotation-config/>
    <!-- 进行扫描,以完成Controller创建和自动依赖注入的功能 -->
    <context:component-scan base-package="org.pag" >
        <!-- 扫描Controller注解Bean 强烈建议Controller层扫描和其他注解扫描分开 其他类型注解(@Controller, 
            @Service , @Configuration, etc.)扫描 由applicationContext-common.xml中配置 -->
        <!-- include-filter 包含 exclude-filter 排除 -->
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service"  />
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Repository" />
    </context:component-scan>

    <!-- 相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean, 
        配置一些messageconverter。即解决了@Controller注解的使用前提配置。 -->
    <bean
        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    <!-- Configure the multipart resolver -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
    <!-- Enables the Spring MVC @Controller programming model -->

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
        <property name="suffix" value=".jsp" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
            <property name="order" value="0" /> 
    </bean>
<mvc:annotation-driven >
    <!-- 消息转换器 -->
    <mvc:message-converters register-defaults="true">
      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>
    <!-- <mvc:default-servlet-handler/> -->
    <mvc:resources mapping="/dhtmlx/**" location="/WEB-INF/dhtmlx/**" />
    <mvc:resources mapping="/css/**" location="/WEB-INF/sys/css/" />
    <mvc:resources mapping="/js/**" location="/WEB-INF/sys/js/" />
    <mvc:resources mapping="/fonts/**" location="/WEB-INF/sys/fonts/" />
    <mvc:resources mapping="/images/**" location="/WEB-INF/sys/images/" />
<!-- <mvc:resources mapping="/error/**" location="/WEB-INF/error/" /> -->
    
    <!-- 增加 AOP 注解支持 必须写在此处,否则 SpringMVC框架中的AOP无效  -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!--看到shiro配置文件最后的那个了吧 lifecycleBeanPostProcessor -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<!--这个要看你报不报错 不报错就不需要 当时这个搞了好久 坑-->
        <property name="proxyTargetClass" value="true"></property>
    </bean>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
    
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
     <!-- 定义默认的异常处理页面 -->  
<!--     <property name="defaultErrorView" value="error"/>   -->
    <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.web.bind.MissingServletRequestParameterException">error/requiredParameter</prop>
                <prop key="org.springframework.beans.TypeMismatchException">error/mismatchParameter</prop>
                <prop key="org.springframework.web.bind.ServletRequestBindingException">error/bindException</prop>
                <prop key="org.apache.shiro.authz.AuthorizationException">error/authrizationException</prop>
            </props>
        </property>
    </bean>
    
    <!-- 注解驱动开启,对注解方式的事物进行支持 -->
    <tx:annotation-driven  mode="aspectj"/>
 
 
 
 
</beans>

基本上整个配置我都是按照顺序来的,按照这个顺序配置就好,目前正在计划出一个开源的小权限管理开发框架,后面后时间再说咯。

转载于:https://my.oschina.net/58685474/blog/719362

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值