1.前言

这里专门 做 spring security 登出操作 的  详细记录

2.操作

(1)目录结构

(2)在security 拦截规则配置文件添加退出登录支持

源码

package com.example.security5500.securityConfig;

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.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; //@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired
private DbUserDetailsService dbUserDetailsService; //拦截规则设置
@Override
protected void configure(HttpSecurity http) throws Exception {
// http
// .authorizeRequests()
// // 匹配 "/" 路径,不需要权限即可访问
// .antMatchers("/").permitAll()
// //匹配 "/user" 及其以下所有路径,都需要 "USER" 权限
//// .antMatchers("/user/**").hasAuthority("USER")
// .and()
// //登录地址为 "/login",登录成功默认跳转到页面 "/hai"
// .formLogin().loginPage("/login").defaultSuccessUrl("/hai")
// //退出登录的地址为 "/logout",退出成功后跳转到页面 "/login"
// .and()
// //退出登录的地址为 "/logout",退出成功后跳转到页面 "/login"
// .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
// // 默认启用 CSRF ,必须post才可以访问/logout
http
//允许基于使用HttpServletRequest限制访问
.authorizeRequests()
//设置不拦截页面,可直接通过,路径访问 "/", "/index", "/home" 则不拦截,
//"/hhk/**" 的意思是 "/hhk" 及其以下所有路径
.antMatchers("/", "/index", "/home","/hhk/**")
//是允许所有的意思
.permitAll()
//其他页面都要拦截,【需要在最后设置这个】
.anyRequest().authenticated()
.and()
//设置自定义登录页面
.formLogin()
//指定自定义登录页面的访问虚拟路径
.loginPage("/login")
.permitAll()
.and()
// 添加退出登录支持。当使用WebSecurityConfigurerAdapter时,这将自动应用。默认情况是,访问URL”/ logout”,使HTTP Session无效
// 来清除用户,清除已配置的任何#rememberMe()身份验证,清除SecurityContextHolder,然后重定向到”/login?success”
.logout()
// //指定的登出操作的虚拟路径,需要以post方式请求这个 http://localhost:5500/mylogout 才可以登出 ,也可以直接清除用户认证信息达到登出目的
.logoutUrl("/mylogout")
//登出成功后访问的地址
.logoutSuccessUrl("/home");
// .permitAll();
} /**
* 添加 UserDetailsService, 实现自定义登录校验
*/
@Override
protected void configure(AuthenticationManagerBuilder builder) throws Exception {
//注入用户信息,每次登录都会来这查询一次信息,因此不建议每次都向mysql查询,应该使用redis
builder.userDetailsService(dbUserDetailsService);
} /**
* 密码加密
*/
@Bean
public static PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
} }

(3)前端以表单的形式登出

3.测试

启动后,

直接 点击 登出即可

如果把不配置security 的拦截规则 ,将会默认使用get 方式 请求   /logout  来登出  ,会进入一个界面 ,需要鼠标点击

4.后端自定义登出

package com.example.security5500.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @Controller
public class UserController { //登出操作
@RequestMapping({"/doLogout"})
public String logout(HttpServletRequest request, HttpServletResponse response) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null) {//清除认证
new SecurityContextLogoutHandler().logout(request, response, auth);
}
//重定向到指定页面
return "redirect:/login";
} }

请求这个也可以登出 ,原理是获取授权对象然后清除认证,再重定向到指定页面

spring security 登出操作 详细说明的更多相关文章

  1. Spring boot 前后台分离项目 怎么处理spring security 抛出的异常

    最近在开发一个项目 前后台分离的 使用 spring boot + spring security + jwt 实现用户登录权限控制等操作.但是 在用户登录的时候,怎么处理spring  securi ...

  2. spring boot security 登出

    <!DOCTYPE html> <html lang="zh-cn" xmlns:th="http://www.thymeleaf.org" ...

  3. django 使用其自带的验证系统 进行用户名有效性验证 登录状态验证 登入操作 登出操作

    from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login, l ...

  4. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  5. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

  6. spring security集成cas实现单点登录

    spring security集成cas 0.配置本地ssl连接 操作记录如下: =====================1.创建证书文件thekeystore ,并导出为thekeystore.c ...

  7. spring security 动态 修改当前登录用户的 权限

    1.前言 spring security 可以获取当前登录的用户信息,同时提供了接口 来修改权限列表信息 , 使用这个方法 ,可以动态的修改当前登录用户权限. 那么问题来了... 如果我是管理员 ,如 ...

  8. spring security 自动登录 --- 心得

    1.前言 仍然是使用cookie存储登录数据,但是存储的数据 由 spring security自动创建 ,当登出后自动删除cookie, 如果不登出也仍在生命周期内,关闭浏览器再打开将会自动登录,无 ...

  9. 学习Spring Boot:(二十八)Spring Security 权限认证

    前言 主要实现 Spring Security 的安全认证,结合 RESTful API 的风格,使用无状态的环境. 主要实现是通过请求的 URL ,通过过滤器来做不同的授权策略操作,为该请求提供某个 ...

随机推荐

  1. 【C/C++】习题3-1 得分/算法竞赛入门经典

    [题目]一个由O和X组成的串,O的得分为目前连续出现的O的个数,X的得分为0.要求统计得分. 我一开始以为要输出表达式,结果好像不需要? [代码] #include <stdio.h> # ...

  2. 利用Windbg分析Magicodes.IE一次错误编写导致内存剧增

    由于这近一年时间一直忙于写书和工作,一直没有水文,但是近期有几位朋友使用我们的Magicodes.IE反馈在导出过程中内存暴涨...好吧,不管怎样,不能苦了我们朋友,接下来我们通过windbg来看一下 ...

  3. shell脚本 binlog方式增量备份mysql

    一.简介 源码地址 日期:2018/4/12 介绍:复制Binlog日志方式的增量备份脚本,并保存固定天数的备份 效果图: 二.使用 适用:centos6+ 语言:中文 注意:使用前先修改脚本中变量 ...

  4. GIT最基本使用

    带'*':必须操作 不带'*':可能需要而且经常用的 常见步骤为下: *1.克隆项目:有两种不同类型的网址(https/ssh) git clone [url] *2.初始化本地仓库 git init ...

  5. ciscn_2019_en_3

    例行检查我就不放了,64位的程序放入ida中 可以看到s到buf的距离是0x10,因为puts是遇到\x00截止.而且题目没有限制我们s输入的数量,所以可以通过这个puts泄露出libc的基值 很明显 ...

  6. 新建日历(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 默认的标准日历设置好了以后,问题又来了:出现某些特殊的原因,可能还需要一个与标准日历设置不同的日历,这个可怎么弄? 没关系 ...

  7. CF984B Minesweeper 题解

    Content 有一个 \(n\times m\) 的扫雷地图,请判断这个地图是否合法,即对于所有格子,是否满足: 对应点的数字周围必须有对应数字个雷 空的地方周围没有雷 数据范围:\(1\leqsl ...

  8. mysql表死锁查询

    1.查询是否锁表show open tables where in_use>0; 2.查询进程show processlist查询到相对应的进程,然后 kill id 3.查看正在锁的事务sel ...

  9. 音视频中的PTS和DTS及同步

    视频的播放过程可以简单理解为一帧一帧的画面按照时间顺序呈现出来的过程,就像在一个本子的每一页画上画,然后快速翻动的感觉.       但是在实际应用中,并不是每一帧都是完整的画面,因为如果每一帧画面都 ...

  10. java源码——两种格式日期的转换

    这里要实现1981.07.30 格式和July 30.1981格式的日期的转换. 在输入时进行日期格式的识别,并且对字符串进行操作并且输出. 难点在于字符串格式的识别和月份的转换,我用了正则表达式匹配 ...