ssh框架配置过程

1.pom.xml配置依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cjw</groupId>
    <artifactId>sshTest</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.1.1.RELEASE</spring.version>
        <hibernate.version>3.6.5.Final</hibernate.version>
        <struts2.version>2.3.1</struts2.version>
        <!-- 给出Shiro的版本 -->
        <shiro.version>1.2.3</shiro.version>
    </properties>

    <dependencies>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.1_3</version>
        </dependency>


        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- struts2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts2.version}</version>
        </dependency>
        <!--config-browser-plugin插件,使用了这个插件之后,就可以很方便的浏览项目中的所有action及其与 jsp view的映射 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-junit-plugin</artifactId>
            <version>${struts2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts2.version}</version>
        </dependency>

        <!-- 添加对数据库的支持 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.5</version>
        </dependency>

        <!-- 添加对数据源的支持 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- shiro -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-quartz</artifactId>
            <version>${shiro.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src\main\webapp</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
        <finalName>com</finalName>
    </build>
</project>

2.包的创建

3.resources资源文件配置

1.applicationContext.xml配置

  1.1创建过程:(1)数据库配置文件导入(2)开启注解(3)注解纳入管理(4)配置数据源(5)和hibernate整合,需要数据源dataSource,配置hibernate的属性(6)包扫描加载注解(7)注解管理事务

  1.2代码:

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- classpath 是指什么呢 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 使用annotation -->
    <context:annotation-config/>
    <!-- 使用annotation自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
    <context:component-scan base-package="com.demo.ssh.action"/>
    <context:component-scan base-package="com.demo.ssh.service"/>
    <context:component-scan base-package="com.demo.ssh.dao" />

    <!-- data connection setting -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${database.driverClassName}"></property>
        <property name="jdbcUrl" value="${database.url}"></property>
        <property name="user" value="${database.username}"></property>
        <property name="password" value="${database.password}"></property>
        <!-- 设置数据库连接池的最大连接数 -->
        <property name="maxPoolSize">
            <value>50</value>
        </property>
        <!-- 设置数据库连接池的最小连接数 -->
        <property name="minPoolSize">
            <value>5</value>
        </property>
        <!-- 设置数据库连接池的初始化连接数 -->
        <property name="initialPoolSize">
            <value>5</value>
        </property>
        <!-- 设置数据库连接池的连接最大空闲时间 -->
        <property name="maxIdleTime">
            <value>20</value>
        </property>
        <!-- c3p0缓存Statement的数量数 -->
        <property name="maxStatements">
            <value>50</value>
        </property>
        <!-- 当连接池里面的连接用完的时候,C3P0一下获取新的连接数 -->
        <property name="acquireIncrement">
            <value>20</value>
        </property>

    </bean>

    <!-- hibernate 管理-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <!-- 引用上面设置的数据源 -->
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.autoReconnect">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <!-- 解决session关闭问题 -->
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
                <prop key="current_session_context_class">thread</prop>

                <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/test</prop>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            </props>

        </property>

        <!-- 包扫描的方式加载注解类 -->
        <property name="packagesToScan">
            <list>
                <value>com.demo.ssh.model</value>
            </list>
        </property>
    </bean>

    <!-- 用注解来实现事物管理 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

2.db.properties文件

database.database=mysql
database.driverClassName=com.mysql.jdbc.Driver
database.username=root
database.password=root
database.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

3.struts.xml配置

  1.1创建过程:(1)strtus模式配置(2)开启spring管理strtus2(3)配置每一个action

  1.2代码:

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

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <constant name="struts.devMode" value="false"/>
    <constant name="struts.objectFactory" value="spring" />

    <package name="user" namespace="/" extends="struts-default">

        <action name="user_*" class="userAction" method="{1}">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>

    </package>
    <package name="login" namespace="/" extends="struts-default">
        <!-- 登陆提交的地址,和applicationContext-shiro.xml中配置的loginurl一致 -->
        <action name="login_*" class="com.cjw.ssh.action.LoginAction" method="{1}">
            <result name="login">/WEB-INF/jsp/login.jsp</result>
        </action>
    </package>
</struts>

4.applicationContext-shiro.xml配置

  1.1创建过程:(1)shiro过滤器配置,需要安全管理器,登录页面,成功页面,权限不足页面,权限配置(2)安全管理器,引用自定义的realm(3)自定义的realm

  1.2代码:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- web.xml中shiro的filter对应的bean -->
    <!-- Shiro 的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- 指定安全管理器 -->
        <property name="securityManager" ref="securityManager" />

        <!-- 如果没有认证将要跳转的登陆地址,http可访问的url,如果不在表单认证过虑器FormAuthenticationFilter中指定此地址就为身份认证地址 -->
        <!-- loginUrl认证提交地址,如果没有通过认证将会请求此地址进行认证,请求地址酱油formAuthenticationFilter进行表单认证 -->
        <property name="loginUrl" value="/login_loginPage.action" />

        <!-- 认证通过后,指定如果没有权限跳转的地址 -->
        <property name="unauthorizedUrl" value="/refuse.jsp" />

      <!--  shiro拦截器配置-->
       <!-- <property name="filters">
            <map>
                <entry key="authc" value-ref="formAuthenticationFilter" />
            </map>
        </property>-->

        <!-- 真正的filter,也是过滤链,从上向下顺序执行,一般都将/**放在最下边 -->
        <property name="filterChainDefinitions">
            <value>
                <!-- 所有的URL都可以匿名访问 -->
                /** = authc
            </value>
        </property>
    </bean>


    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- 注入realm -->
        <property name="realm" ref="userRealm" />
    </bean>


    <!-- 自定义 realm -->
    <bean id="userRealm" class="com.cjw.ssh.shiro.UserRealm"></bean>
</beans>

1.5 web.xml配置

  1.1创建过程:(1)spring容器配置的地址(2)spring容器监听器(3)shiro安全过滤器(4)strtus2过滤器

  1.2代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!-- spring容器配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext.xml
            classpath*:applicationContext-*.xml
        </param-value>
    </context-param>
    <!-- spring容器监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 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>
    <!-- struts2容器控制器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>


    <!-- 欢迎页面 -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

posted @ 2018-01-24 16:25  生根  阅读(492)  评论(0编辑  收藏  举报