SpringSecurity如何退出登录
⒈如何退出登录?
SpringSecurity默认为我们提供了退出操作,我们只需要访问特定的url就可以退出登录了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>退出登录</title>
</head>
<body>
<a href="/logout">退出登录</a>
</body>
</html>
⒉SpringSecurity默认为我们做了什么?
1.使当前Session失效
2.清除与当前用户相关的remember-me记录
3.清空当前的SecurityContext
4.重定向到登陆页面
⒊我们如何自定义退出登录
package cn.coreqi.security.config; import cn.coreqi.security.Filter.SmsCodeFilter;
import cn.coreqi.security.Filter.ValidateCodeFilter;
import cn.coreqi.security.handler.CoreqiLogoutSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired
private AuthenticationSuccessHandler coreqiAuthenticationSuccessHandler; @Autowired
private AuthenticationFailureHandler coreqiAuthenticationFailureHandler; @Autowired
private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig; @Bean
public PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
} @Override
protected void configure(HttpSecurity http) throws Exception {
ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
validateCodeFilter.setAuthenticationFailureHandler(coreqiAuthenticationFailureHandler); SmsCodeFilter smsCodeFilter = new SmsCodeFilter(); //http.httpBasic() //httpBasic登录 BasicAuthenticationFilter
http.addFilterBefore(smsCodeFilter, UsernamePasswordAuthenticationFilter.class) //加载用户名密码过滤器的前面
.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class) //加载用户名密码过滤器的前面
.formLogin() //表单登录 UsernamePasswordAuthenticationFilter
.loginPage("/coreqi-signIn.html") //指定登录页面
//.loginPage("/authentication/require")
.loginProcessingUrl("/authentication/form") //指定表单提交的地址用于替换UsernamePasswordAuthenticationFilter默认的提交地址
.successHandler(coreqiAuthenticationSuccessHandler) //登录成功以后要用我们自定义的登录成功处理器,不用Spring默认的。
.failureHandler(coreqiAuthenticationFailureHandler) //自己体会把
.and()
.logout() //退出登录相关配置
.logoutUrl("signOut") //自定义退出登录页面
.logoutSuccessHandler(new CoreqiLogoutSuccessHandler()) //退出成功后要做的操作(如记录日志),和logoutSuccessUrl互斥
//.logoutSuccessUrl("/index") //退出成功后跳转的页面
.deleteCookies("JSESSIONID") //退出时要删除的Cookies的名字
.and()
.authorizeRequests() //对授权请求进行配置
.antMatchers("/coreqi-signIn.html","/code/image","/session/invalid").permitAll() //指定登录页面不需要身份认证
.anyRequest().authenticated() //任何请求都需要身份认证
.and().csrf().disable() //禁用CSRF
.apply(smsCodeAuthenticationSecurityConfig);
//FilterSecurityInterceptor 整个SpringSecurity过滤器链的最后一环
}
}
SpringSecurity如何退出登录的更多相关文章
- springsecurity的退出登陆
登陆成功就有退出,退出的实质就是让session失效 要实现退出登录只需要在spring-security配置文件中在加一行代码就可以了 <!--退出登陆--> <security: ...
- 模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能
Login <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- phpcms前台退出登录的时候提示信息'退出成功0'
问题背景: phpcms前台退出登录的时候,提示了一个退出成功0 让我很困惑为啥有个0呢? 问题分析: 进入 ./phpcms/modules/member/index.php 找到logout方法, ...
- linux笔记:关机重启命令shutdown,系统运行级别init,退出登录logout
命令名称:shutdown功能:关机或重启用法:shutdown [选项] [时间]选项参数:-c 取消前一个关机命令-h 关机-r 重启时间格式:now 现在时:分 20:30其他:会正常关闭正在启 ...
- Spring Security(10)——退出登录logout
要实现退出登录的功能我们需要在http元素下定义logout元素,这样Spring Security将自动为我们添加用于处理退出登录的过滤器LogoutFilter到FilterChain.当我们指定 ...
- iOS 创建一个在退出登录时可以销毁的单例
一.单例简介 单例模式是在软件开发中经常用的一种模式.单例模式通俗的理解是,在整个软件生命周期内,一个类只能有一个实例对象存在. 二.遇到的问题 在平时开发使用单例的过程中,有时候会有这样的需求,在用 ...
- IdentityServer4 退出登录后,跳转到原来页面
IdentityServer4 退出登录后,默认会跳转到Config.Client配置的PostLogoutRedirectUris地址,那我们如何动态的跳转到原来的地址呢?实现很简单,Logout修 ...
- Spring Security 入门(1-3-3)Spring Security - logout 退出登录
要实现退出登录的功能我们需要在 http 元素下定义 logout 元素,这样 Spring Security 将自动为我们添加用于处理退出登录的过滤器 LogoutFilter 到 FilterCh ...
- ionic3.x版本-实现点击tab导航栏判断是否已经登陆然后加载不同页面,和退出登录功能。
html代码: <ion-tabs #myTabs> <ion-tab [root]="tab1Root" tabTitle="首页" tab ...
随机推荐
- 【优秀的艺术文字和图标设计软件】Art Text 3.2.3 for Mac
[简介] Art Text 3.2.3 版本,这是一款Mac上简单易用的艺术文字和图标设计软件,今这款软件内置了大量的背景纹理和特效,能够让我们非常快速的制作出漂亮的图标,相比专业的PS,Art ...
- Prometheus-自定义Node_Exporter
标量(Scalar):一个浮点型的数字值 标量只有一个数字,没有时序. 需要注意的是,当使用表达式count(http_requests_total),返回的数据类型,依然是瞬时向量.用户可以通过内置 ...
- websocket实现简单的通信
websocket server端 #coding=utf8 #!/usr/bin/python import struct,socket import hashlib import threadin ...
- CentOS6.9快速安装配置svn
CentOS6.9快速安装配置svn 环境介绍: 操作系统:CentOS release 6.9 (Final)192.168.65.130 (svn服务器)192.168.65.129 (svn客户 ...
- python对象初始化
当python对象被创建以后,需要将对象进行初始化.Python有一个构造函数和一个初始化函数: 1.构造函数__new__,只接受一个参数,即类本身(它会在对象被构造之前调用,所以这里也就没有sel ...
- IO流--与properties集合配合使用
IO流--与properties集合配合使用: 注:生产上主要用于常量文件的配置,读取常量文件: 1:properties集合的放值与取值: /* * properties集合继承自hashTable ...
- tedu训练营day03
Day03笔记1.作业 1.假如你现在25周岁,每年365天,计算你过了多少个星期天(大概数字) 提示 :地板除 2.毕业薪资为10000元,每年涨20%,十年之后你的薪资为多少元? 提示: 幂运算( ...
- JavaScript 实现一个简单的MVVM前端框架(ES6语法)
前言 随着前端各大框架的崛起,为我们平时的开发带来了相当的便利,我们不能一直停留在应用层面,今天就自己动手实现一个乞丐版的MVVM小框架 完整代码github地址 效果 html代码 <div ...
- showMem.c setMem.c 及其改进
#ifndef MEMUTIL_H_INCLUDED #define MEMUTIL_H_INCLUDED // Show memory void showMem(void *, unsigned); ...
- VS WebDev.WebServer40
vs2010下有一个WebDev.WebServer40工具,可以直接拿来当服务器用,这样就不用再安装iis了.位置在 C:\Program Files (x86)\Common Files\micr ...