SSH的简单教程(整合)

使用框架Sping+SpringMVC+Hibernate+Shiro
一、简介
1.spring管理bean
2.SpringMVC分发HTTP请求
3.Hibernate数据库连接
4.shiro登录及授权
二、配置
1.web.xml
首先配置全站字符集,否则中文乱码

<filter>
    <!-- 配置字符集 -->
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

配置Spring

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <!-- 加载所有的配置文件-->
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:/spring.xml
        classpath*:/spring-hibernate.xml
    </param-value>
</context-param>

配置SpringMVC

<servlet>
    <!-- 配置SpringMVC -->
    <servlet-name>springServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

配置默认请求

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.txt</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
</servlet-mapping>

三、Spring配置
配置Spring.xml

<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties"/>

<!-- autowired对应的bean -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.*">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation"
                            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

四、Hibernate配置
1.引入配置文件

<context:property-placeholder location="classpath*:/config.properties"/>

2.配置数据源

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

3.配置session

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

4.配置事务

<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

 

<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">

 

<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">

五、SpringMVC
配置spring-mvc.xml

<aop:aspectj-autoproxy proxy-target-class="true"/>
<aop:config proxy-target-class="true"></aop:config>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean>
<context:component-scan base-package="com.*">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:include-filter type="annotation"
                            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        </bean>
        <!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="prettyPrint" value="true"/>
        </bean>

    </mvc:message-converters>
</mvc:annotation-driven>
<!-- 文件上传 -->
<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 设置上传文件的最大尺寸为10MB -->
    <property name="maxUploadSize" value="10000000"/>
    <property name="defaultEncoding" value="utf-8"/>
</bean>

六、shiro
shiro.xml

<!-- Shiro Filter 拦截器相关配置 -->
<!--anon 表示匿名访问(不需要认证与授权),authc 表示需要认证filters自定义过滤器,-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="/"/>
    <!--<property name="successUrl" value="/login" />-->
    <property name="unauthorizedUrl" value="/api/v1/auth/unauthorize"/>
    <property name="filters">
        <map>

            <entry key="authcStateless" value-ref="statelessAuthcFilter"/>
            <entry key="skip" value-ref="statelessSkipFilter"/>
        </map>
    </property>
    <property name="filterChainDefinitions">
        <value>
            /test/** = anon
            /api/v1/file/{id}/view/** = authcStateless
            /api/v1/file/** = skip
            /api/v1/auth/** = skip

            /api/v1/** = authcStateless

            /api/v1/repositories/mostUsed = perms[repository:view]
            <!--/** = authcStateless&lt;!&ndash;表示需要自定义的过滤器过滤&ndash;&gt;-->

        </value>
    </property>
</bean>

 

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="org.apache.shiro.authz.UnauthorizedException">/api/v1/auth/unauthorize</prop>
        </props>
    </property>
</bean>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页