SpringSecurity、Spring Social、SpringSession、TX-LCN、Spring Cloud Data Flow、JWT 架构(三)

继续SpringSecurity,今天我们来聊聊退出登录时的页面跳转和session注销。SpringSecurity提供了这方面的支持。在上一篇文章的基础之上直接来聊聊:

springsecrity.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">
    
    <!--
    配置具体的规则:
    auto-config="true"    不用自己编写登录的页面,框架提供默认登录页面
    use-expressions="false"    是否使用SPEL表达式(没学习过)
    -->
    <beans:bean id="failureCDW" class="com.alibaba.failureCDW"></beans:bean>
    <http auto-config="false" use-expressions="false">
        <!--配置具体拦截的url,pattern是拦截的url,access是访问被拦截的url需要的权限-->
        <intercept-url pattern="/*" access="ROLE_USER" />
        <intercept-url pattern="/**/to**" access="ROLE_USER" />
        <intercept-url pattern="/**/gotoUserLoginPageJsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
        <!-- 定义跳转的具体页面
        form-login是spring security命名空间配置登录相关信息的标签,它包含如下属性:
        1. login-page 自定义登录页url,默认为/login
        2. login-processing-url 登录请求拦截的url,也就是form表单提交时指定的action
        3. default-target-url 默认登录成功后跳转的url
        4. always-use-default-target 是否总是使用默认的登录成功后跳转url
        5. authentication-failure-url 登录失败后跳转的url
        6. username-parameter 用户名的请求字段 默认为userName
        7. password-parameter 密码的请求字段 默认为password
        8. authentication-success-handler-ref 指向一个
           AuthenticationSuccessHandler用于处理认证成功的请求,不能和default-target-url
                      还有always-use-default-target同时使用
        9. authentication-success-forward-url 用于authentication-failure-handler-ref
        10. authentication-failure-handler-ref 指向一个AuthenticationFailureHandler用于处理失败的认证请求
        11. authentication-failure-forward-url 用于authentication-failure-handler-ref
        12. authentication-details-source-ref 指向一个AuthenticationDetailsSource,在认证过滤器中使用
        -->
        <form-login 
            login-page="/SpringSecurityPageController/gotoUserLoginPageJsp.do" 
            default-target-url="/SpringSecurityPageController/toUserWelComePageJsp.do" 
            authentication-failure-url="/SpringSecurityPageController/gotoUserLoginPageJsp.do?error=error" 
            always-use-default-target="true"
            username-parameter="username"
            password-parameter="password" />
        <!-- logout 属性详解
        logout-url LogoutFilter要读取的url,也就是指定spring security拦截的注销url
        logout-success-url 用户退出后要被重定向的url
        invalidate-session 默认为true,用户在退出后Http session失效
        success-handler-ref 对一个LogoutSuccessHandler的引用,用来自定义退出成功后的操作 
        这里需要注意的一点是,spring security 3.x默认的注销拦截url为/j_spring_security_logout,而4.x则默认使用/logout-->
         <logout logout-url="/SpringSecurityPageController/toUserLogoutPageJsp*"
                  logout-success-url="/SpringSecurityPageController/gotoUserLoginPageJsp" 
                  invalidate-session="true" />
         <!--关闭跨域请求,不关闭就会拦截所有的访问,显示权限不够-->
        <csrf/>
    </http>

    <authentication-manager>
      <authentication-provider>
        <user-service>
            <user name="chendawei" password="123456" authorities="ROLE_USER"/>
        </user-service>
      </authentication-provider>
    </authentication-manager>

</beans:beans>
在SpringSecurityPageController页面跳转的里面加入:

/**
     *  用户退出成功以后的页面
     */
    @RequestMapping(value="/toUserLogoutPageJsp",method = RequestMethod.GET)
    public String toUserLogoutPageJsp(HttpServletRequest request, HttpServletResponse response) {
        try {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null){    
                new SecurityContextLogoutHandler().logout(request, response, auth);
            }
            //You can redirect wherever you want, but generally it's a good practice to show login screen again.
            return "redirect:SpringSecurityPageController/toUserLogoutPageJsp";

        }
        catch (Exception e) {
            System.out.println("SpringSecurityPageController/toUserLoginPageJsp Exception:" + e.getMessage());
            return null;
        }
    }

以上大概就是主要的逻辑。因为注销就是将sesson退出消除,这个没啥可说的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章