SpringSecurity使用

工作流程:

    当用户向服务器发送请求时,SpringSecurity会通过过滤器,拦截SpringSecurity配置文件中设置的请求,如果设置了认证页面则跳向自定义的认证页面,否则跳向SpringSecurity提供的认证页面,当提交用户名或密码后SpringSecurity会调用设置的业务层代码,获取到指定的对象,然后SpringSecurity框架会自动对登录的用户进行密码或权限的检查,如果验证通过则跳向指定的验证成功后的页面,否则跳向指定的验证失败后的页面;

SpringSecurity相关配置:

    1)、导入SpringSecurity所依赖的jar包;

            1)、spring-security-web;

            2)、spring-security-config;

    2)、配置wen.xml文件;

            SpringSecurity底层是通过11个过滤器组成的过滤器链来工作的,所以使用SpringSecurity之前需要在web.xml配置文件中添加SpringSecurity提供的过滤器实现类(过滤器链的入口):

    springSecurityFilterChain

    org.springframework.web.filter.DelegatingFilterProxy

    springSecurityFilterChain

    /*

    contextConfigLocation

    classpath:spring/spring-security.xml

3)、配置SpringSecurity核心配置文件:

       a)、导入配置文件的依赖;

        

                       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"

                      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/aop

                    http://www.springframework.org/schema/aop/spring-aop.xsd

                    http://www.springframework.org/schema/tx

                    http://www.springframework.org/schema/tx/spring-tx.xsd

                    http://www.springframework.org/schema/mvc

                    http://www.springframework.org/schema/mvc/spring-mvc.xsd

                    http://www.springframework.org/schema/security

                    http://www.springframework.org/schema/security/spring-security.xsd">

    b)、释放对静态资源拦截或指定文件的拦截

SpringSecurity主配置

   

   

   

   

                        login-processing-url="/login"

                        default-target-url="/index.jsp"

                        authentication-failure-url="/failer.jsp"

                        always-use-default-target="true"

/>

   

   

                    logout-url="/logout"

                    logout-success-url="/login.jsp"/>

   

   

   c)、为SpringSecurity提供数据认证来源

 

   

   

       

   

SpringSecurity业务层编写步骤:

    1)、实现UserDetailsService接口;

    2)、重写loadUserByUsername方法;

           a)、 通过传入的username查询数据库获取用户对象;

           b)、验证用户是否存在;

           c)、将查询到的用户信息封装到UserDetails实例中;

                   该对象实例的构造方法需要传入username、password、该用户所拥有的角色名集合;

                        用户的角色名称需要封装到SimpleGrantedAuthority实例中;

                        然后将所有的SimpleGrantedAuthority实例封装带集合中,传入UserDetails实例;

      3)、返回UserDetails对象;

你可能感兴趣的:(SpringSecurity使用)