spring-shiro-springmvc-mybatis简单配置项目

这是不包含缓存的配置:
spring-shiro.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:webflow="http://www.springframework.org/schema/webflow-config"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">

    <!-- 加载shiro-cas的 .properties 文件 -->
    <context:property-placeholder location="classpath:shiro.properties" ignore-unresolvable="true"/>   



    <!-- shiro普通realm -->
    <bean id="myRealm" class="com.hky.utils.shiro.MyRealm"></bean>





    <!-- securityManager -->  
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
        <property name="rememberMeManager">  
            <bean class="org.apache.shiro.web.mgt.CookieRememberMeManager">  
                <property name="cookie">  
                    <bean class="org.apache.shiro.web.servlet.SimpleCookie">  
                        <constructor-arg name="name" value="RememberMe" />  
                        <property name="maxAge" value="604800" />  
                    </bean>  
                </property>  
            </bean>  
        </property>  
        <!-- <property name="realm" ref="iniRealm" /> -->  
        <property name="realm" ref="myRealm" />          
    </bean>  


    <!-- shiroFilter -->  
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
        <property name="securityManager" ref="securityManager" />  

        <property name="loginUrl" value="/login/index.shtml" />

        <property name="successUrl" value="/home/index.shtml" />

        <!-- 未登录用户url -->
        <property name="unauthorizedUrl" value="/login/index.shtml" />
        <property name="filters">
            <map>
                <!--退出过滤器-->
                <entry key="logout" value-ref="systemLogoutFilter" />

            </map>
        </property>  
        <property name="filterChainDefinitions">  
            <value>

                <!-- anon表示此地址不需要任何权限即可访问 -->    
                /login/** = anon  

                <!-- 登出过滤器 -->
                /user/logout**=logout


                <!--所有的请求(除去配置的静态资源请求或请求地址为anon的请求)都要通过登录验证,如果未登录则跳到/login-->   
                /** = authc


               <!--  /static/**=anon  -->   
                <!-- perms[user:query]表示访问此连接需要权限为user:query的用户 -->    
               <!--  /user=perms[user:query]   -->  
                <!-- roles[manager]表示访问此连接需要用户的角色为manager -->    
               <!--  /user/add=roles[manager]   -->  
               <!--  /user/del/**=roles[admin]   -->  
               <!--  /user/edit/**=roles[manager]   -->  

               <!--   -->   
            </value>  
        </property>  
    </bean>  
    <!-- 自定义登出过滤,对应地址/user/logout**=logout -->
    <bean id="systemLogoutFilter" class="com.hky.utils.shiro.SystemLogoutFilter"></bean>

    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />


</beans>   

其中的登出过滤实现SystemLogoutFilter代码:

package com.hky.utils.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

/**
 * 登出过滤器
 * @author mac
 *
 */
public class SystemLogoutFilter extends LogoutFilter{

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        //在这里执行退出系统前需要清空的数据
        Subject subject = getSubject(request, response);

        //获取重定向url
        String redirectUrl = getRedirectUrl(request, response, subject);

        try {
            //登出
            subject.logout();

        } catch (SessionException ise) {

           ise.printStackTrace();

        }
        //返回false表示不执行后续的过滤器,直接返回跳转到登录页面
        issueRedirect(request, response, redirectUrl);

        return false;

    }
}

web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    id="WebApp_ID" version="3.0">  

  <display-name>Archetype Created Web Application</display-name>

    <!-- 指定配置文件所在目录,加载文件 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name> 
        <param-value>classpath:spring-mvc.xml, classpath:spring-mybatis.xml, classpath:spring-shiro.xml</param-value> 
    </context-param> 




    <!-- Shiro配置(放在最上面) -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   


    <!-- 编码过滤器 -->
    <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <async-supported>true</async-supported>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>


    <!-- 配置springmvc -->
    <servlet>  
        <servlet-name>SpringMVC</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <!-- 可以自定义servlet.xml配置文件的位置和名称   -->
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring-mvc.xml</param-value>  
        </init-param>
        <!-- 表示启动容器时初始化该Servlet -->
        <load-on-startup>1</load-on-startup>  
        <async-supported>true</async-supported>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>SpringMVC</servlet-name>  
        <!-- 这个斜杠,表示拦截所有的url -->  
        <url-pattern>*.shtml</url-pattern>  
        <!-- <url-pattern>*.do</url-pattern>   -->
    </servlet-mapping>


    <!-- Spring配置 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  


    <jsp-config>  
        <jsp-property-group>  
            <url-pattern>*.jsp</url-pattern>  
            <page-encoding>UTF-8</page-encoding>  
            <scripting-invalid>false</scripting-invalid>  
        </jsp-property-group>  
    </jsp-config>  

    <!-- 
    <session-config>  
        <cookie-config>  
            <name>_sid</name>  
        </cookie-config>  
        <tracking-mode>COOKIE</tracking-mode>  
    </session-config> -->










    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>


</web-app>

spring-mvc.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 
    如果没有<mvc:annotation-driven/>,那么所有的Controller可能就没有解析,所以当有请求时候都没有匹配的处理请求类,就都去
    <mvc:default-servlet-handler/>即default servlet处理了。
    添加上<mvc:annotation-driven/>后,相应的do请求被Controller处理,
    而静态资源因为没有相应的Controller就会被default servlet处理。总之没有相应的Controller就会被default servlet处理就ok了。

    <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。
    即解决了@Controller注解的使用前提配置。
    <context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。
     -->
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
    <context:component-scan base-package="com.hky.controller"/>
    <mvc:annotation-driven/>

    <!-- 手动注入相当与配置: <mvc:annotation-driven/>
    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>    
     -->


    <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->    
    <bean id="multipartResolver"      
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      
        <!-- 默认编码 -->    
        <property name="defaultEncoding" value="utf-8" />      
        <!-- 文件大小最大值 -->    
        <property name="maxUploadSize" value="10485760000" />      
        <!-- 内存中的最大值 -->    
        <property name="maxInMemorySize" value="40960" />      
    </bean>


    <!-- 配置路径 -->
    <!-- 定义跳转的文件的前后缀 ,视图模式配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/WEB-INF/test/" />
        <property name="suffix" value=".jsp" />
    </bean> 

    <!-- 拦截器部分 -->
    <!--设置不拦截的资源 -->  
    <mvc:resources mapping="/js/**" location="/js/"/>    
    <mvc:resources mapping="/css/**" location="/css/"/>    
    <mvc:resources mapping="/pack/**" location="/pack/"/>  
    <mvc:resources mapping="/img/**" location="/img/"/>  


</beans>

spring-mybatis.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <!-- 自动扫描 -->
    <context:component-scan base-package="com.hky" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${usename}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:com/hky/sss/mapper/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hky.sss.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

jdbc.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/han
usename=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
#最大等待时间
maxWait=60000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值