传统web项目(SSM)搭建流程-集成springSecurity安全框架

传统web项目(SSM)搭建流程-集成springSecurity安全框架

第一步(框架搭建):
1.使用Idea的或者Eclipse等集成开发环境中集成的maven骨架搭建出一个最基本的代码结构:
在这里插入图片描述 注意:
(1) 作者使用的是Idea;
(2) 模板选择一定要使用maven提供的;
(3) 创建项目时需要联网;

2.目录结构完善:
创建好的目录结构应该是这样:
在这里插入图片描述注意:
(1) 其中红色框标注的两个目录需要自行创建(最新版的Idea创建时会有提示,如果没有的话创建好需要标记一下);
(2) 注意联网;
(3) 如果有文件夹缺失的可以自行创建并标记即可;

第二步(编写配置文件):
1.在webapp/WEB-INF/web.xml文件中编写web项目的核心配置文件:

<?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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

<!--xml2.3版本文件头(不推荐: 会有严格的排序制度)-->
<!--<!DOCTYPE web-app PUBLIC-->
<!-- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"-->
<!-- "http://java.sun.com/dtd/web-app_2_3.dtd" >-->
<!--<web-app>-->

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

  <!--配置服务器context对象启动时加载的数据-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <!--扫描整个项目下spring/目录下指定格式得配置文件-->
    <param-value>classpath*:spring/applicationContext_*.xml,classpath:spring_security.xml</param-value>
  </context-param>
  <!--监听器:创建spring IOC容器的创建-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 配置监听器,监听request域对象的创建和销毁==当请求来的时候,创建request对象放进spring容器中 -->
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <!--编码过滤器: 解决post请求乱码-->
  <filter>
    <filter-name>CharacterEncodingFilter</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>
  </filter>
  <!--配置编码过滤器的范围-->
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--安全框架-配置委派代理过滤器: filter-name属性必须是:springSecurityFilterChain  -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <!--配置安全框架的拦截范围-->
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- 前端控制器: 引入springmvc的配置文件-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
  </servlet>
  <!--前端控制器的servlet映射-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

2.控制层的配置文件(web层):

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:security="http://www.springframework.org/schema/security"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!--1.注解扫描的包路径-->
    <context:component-scan base-package="com.itheima.controller"/>
    <context:component-scan base-package="com.itheima.log"/>

    <!--2.注解驱动-->
    <mvc:annotation-driven conversion-service="conversionService"/>

    <!--3.视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--4.静态资源放行-->
    <mvc:default-servlet-handler/>

    <!--5.拦截器-->

    <!--6.自定义日期类型转换器-->
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.itheima.converter.StringToDateConverter"/>
            </set>
        </property>
    </bean>
    
    <!--==============安全框架使用:服务器端的角色拦截配置===============-->
    <!--sop的自动代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <!--配置开启security的注解支持===允许使用 安全框架的注解,控制服务端-->
    <security:global-method-security secured-annotations="enabled"/>
</beans>

3.springSecurity的配置文件(web层):

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

    <!--配置放放行界面(包括登录界面等)-->
    <!--1.pattern: 指定路径-->
    <!--2.security: 值为none表示不拦截-->
    <security:http pattern="/css/**" security="none"/>
    <security:http pattern="/img/**" security="none"></security:http>
    <security:http pattern="/plugins/**" security="none"></security:http>
    <security:http pattern="/favicon.ico" security="none"/>
    <security:http pattern="/login.jsp" security="none"/>
    <security:http pattern="/failer.jsp" security="none"/>
    
    <!--配置需要拦截的请求-->
    <!--1.auto-config: 是否使用框架自带的登录页面-->
    <!--2.use-expressions="是否使用spel表达式"-->
    <security:http auto-config="true" use-expressions="true">

        <!--配置拦截的请求地址,任何请求地址都必须有角色权限-->
        <!--access属性: 指定哪些角色可以通过拦截-->
        <security:intercept-url pattern="/**" access="hasAnyRole('ROLE_ADMIN','ROLE_XXXX')"/>

        <!--login-page: 指定登录的页面-->
        <!--login-processing-url: 登录请求处理路径(必须指定表单的路径是login才可以使用security框架做验证)-->
        <!--default-target-url: 指定登陆成功后的页面(安全框架会记录你的上一次请求,如果有则登录成功后会自动进入上一次的请求路径)-->
        <!--authentication-failure-url: 指定登录失败的页面-->
        <security:form-login login-page="/login.jsp"
                             login-processing-url="/login"
                             default-target-url="/index.jsp"
                             authentication-failure-url="/failer.jsp"/>
        <!--关闭跨域请求登录-->
        <security:csrf disabled="true"/>

        <!--退出: 当请求路径为logout的时候,为退出登录-->
        <!--invalidate-session: 退出后自动清空session域-->
        <!--logout-url: 指定退出的请求-->
        <!--logout-success-url: 退出后跳转的路径-->
        <security:logout invalidate-session="true" logout-url="/logout" logout-success-url="/login.jsp"/>
    </security:http>

    <!--配置认证信息: 管理登录信息-->
    <security:authentication-manager>
        <!--指定认证类(自己写的用于验证用户名和密码的类)-->
        <security:authentication-provider user-service-ref="userService">

            <!--配置使用spring容器中的passwordEncoder加密类 对密码进行加密操作-->
            <security:password-encoder ref="passwordEncoder"/>

            <!--<security:user-service>-->
                    <!--name: 账号-->
                    <!--password: 密码-->
                    <!--authorities: 认证角色 , ROLE_USER :要想登录,必须有一个用户角色才可以-->
                    <!--{noop}:不使用加密方式-->
            <!--<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>-->
            <!--</security:user-service>-->
        </security:authentication-provider>
    </security:authentication-manager>

    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
</beans>

4.业务层配置文件(service层):

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

                        <!--Service层配置-->

    <!--导入dao层的配置文件 (这里需要用到dao中数据源配置)-->
    <import resource="classpath:spring/applicationContext_dao.xml"/>

    <!--配置包扫描-->
    <context:component-scan base-package="com.itheima.service"/>

    <!--1.配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--2.配置事务的增强-->
    <tx:advice id="interceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <!--不需要事务的方法-->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <!--需要事务的方法-->
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
        </tx:attributes>
    </tx:advice>

    <!--3.配置事务的切点-->
    <aop:config>
        <!--原来的格式-->
        <!--<aop:pointcut id="xxx" expression="execution(* com.itheima.service.impl.*.*(..))"/>-->
        <!--<aop:advisor advice-ref="interceptor" pointcut-ref="xxx"/>-->

        <!--也可以这样-->
        <aop:advisor advice-ref="interceptor" pointcut="execution(* com.itheima.service.impl.*.*(..))"/>
    </aop:config>
</beans>

5.数据层的配置文件(dao层):

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

                    <!--dao层的设置-->

    <!--1.包扫描-->
    <context:component-scan base-package="com.itheima.dao"/>

    <!--2.配置配置数据源-->
    <!--(1)加载外部配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--(2)数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--3.配置SqlsessionFactory对象-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>

        <!--加载配置文件-->
        <!--<property name="configLocation" value="classpath:SqlMapConfig.xml"/>-->

        <!--别名包扫描-->
        <!--<property name="typeAliasesPackage" value="com.itheima.domain"/>-->
    </bean>

    <!--4.配置dao包扫描(映射创建代理对象)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>
</beans>

注意:
(1) 配置文件web.xml中需要在spring监听器中配置扫描服务层和数据层的配置文件路径;
(2) 如果项目结构是每一层都是一个单独的模块(配置文件需要放在对应的模块下);
(3) 如果是项目结构都是在一个模块下的,请使用整合版文件;

整合版配置文件如下:

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

                    <!--dao层的设置-->

    <!--1.包扫描-->
    <context:component-scan base-package="com.itheima.dao"/>

    <!--2.配置配置数据源-->
    <!--(2.1)加载外部配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--(2.2)数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--3.配置SqlsessionFactory对象(mybatis配置)-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>

        <!--加载配置文件-->
        <!--<property name="configLocation" value="classpath:SqlMapConfig.xml"/>-->

        <!--别名包扫描-->
        <!--<property name="typeAliasesPackage" value="com.itheima.domain"/>-->
    </bean>

    <!--4.配置dao包扫描(映射创建代理对象)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>

                    <!--Service层配置-->

    <!--配置包扫描-->
    <context:component-scan base-package="com.itheima.service"/>

    <!--1.配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--2.配置事务的增强-->
    <tx:advice id="interceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <!--不需要事务的方法-->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <!--需要事务的方法-->
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
        </tx:attributes>
    </tx:advice>

    <!--3.配置事务的切点(织入事务)-->
    <aop:config>
        <!--原来的格式-->
        <!--<aop:pointcut id="xxx" expression="execution(* com.itheima.service.impl.*.*(..))"/>-->
        <!--<aop:advisor advice-ref="interceptor" pointcut-ref="xxx"/>-->

        <!--另一种格式-->
        <aop:advisor advice-ref="interceptor" pointcut="execution(* com.itheima.service.impl.*.*(..))"/>
    </aop:config>
</beans>
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值