解决方案

package com.hailiu.web.handler;import com.hailiu.model.Log;
import com.hailiu.web.bean.ResponseBean;
import com.hailiu.web.util.HttpServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;/*** @author ShenTuZhiGang* @version 1.0.0* @date 2020-03-21 16:48*/
@Slf4j
@Component
public class CustomAuthenticationFailureHandler extendsSimpleUrlAuthenticationFailureHandler {public CustomAuthenticationFailureHandler(){super("/login?error");}@Overridepublic void onAuthenticationFailure(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,AuthenticationException e) throws IOException, ServletException {if (e instanceof LockedException) {e = new LockedException("账户被锁定,请联系管理员!", e);} else if (e instanceof CredentialsExpiredException) {e = new CredentialsExpiredException("密码过期,请联系管理员!", e);} else if (e instanceof AccountExpiredException) {e = new AccountExpiredException("账户过期,请联系管理员!", e);} else if (e instanceof DisabledException) {e = new DisabledException("账户被禁用,请联系管理员!", e);} else if (e instanceof BadCredentialsException) {e = new BadCredentialsException("用户名或者密码输入错误,请重新输入!", e);}MediaType acceptMediaType = HttpServletUtil.getRequestPrimaryAcceptMediaType(httpServletRequest);if(MediaType.TEXT_HTML.compareTo(acceptMediaType)==0){super.onAuthenticationFailure(httpServletRequest, httpServletResponse, e);}else if(MediaType.APPLICATION_JSON.compareTo(acceptMediaType)==0 || MediaType.ALL.compareTo(acceptMediaType)==0){ResponseBean retTemp = ResponseBean.builder().status(400).msg("登录失败:"+e.getMessage()).build();HttpServletUtil.writeObjectToResponse(httpServletResponse,retTemp);}}
}
public class HttpServletUtil {/*** 获取请求媒体类型* @param request http请求* @return 媒体类型*/public static MediaType getRequestPrimaryAcceptMediaType(HttpServletRequest request){String accept = request.getHeader("Accept");MediaType acceptMediaType;if(StringUtils.isEmpty(accept)){acceptMediaType = MediaType.APPLICATION_JSON;}else{String[] str = accept.split(",");if(str.length>0){try {acceptMediaType = MediaType.valueOf(str[0]);}catch (InvalidMediaTypeException ex){acceptMediaType = MediaType.APPLICATION_JSON;}}else{acceptMediaType = MediaType.APPLICATION_JSON;}}return acceptMediaType;}
}

参考文章

Spring security登录 显示用户名不存在或者密码错误

Spring-Security自定义登陆错误提示信息

spring security自定义登录失败返回错误信息

springsecurity中详细显示登录错误的提示信息

Spring Security——自定义认证错误提示信息及自适应返回格式解决方案相关推荐

  1. spring security 自定义认证登录

    spring security 自定义认证登录 1.概要 1.1.简介 spring security是一种基于 Spring AOP 和 Servlet 过滤器的安全框架,以此来管理权限认证等. 1 ...

  2. spring security:自定义认证成功处理器

    使用spring认证登录,登录之后,一般还需要进行其他处理,例如:保存登录时间.登录ip到数据库,缓存用户信息到redis数据库等等,这些操作可以通过自定义一个登录成功处理器来处理. 自定义认证成功处 ...

  3. spring security自定义指南

    序 本文主要研究一下几种自定义spring security的方式 主要方式 自定义UserDetailsService 自定义passwordEncoder 自定义filter 自定义Authent ...

  4. 3.Spring Security 自定义用户认证

    Spring Security自定义用户认证 自定义认证过程 自定义认证的过程需要实现Spring Security提供的UserDetailService接口,该接口只有一个抽象方法loadUser ...

  5. (二)Spring Security自定义登录成功或失败处理器

    目录 一:创建登录成功处理器 二:创建登录失败处理器 三:添加处理器 三. 项目地址 我们接着上一章 Spring Security最简单的搭建,进行开发 LoginSuccessHandler 和L ...

  6. Spring Security用户认证和权限控制(默认实现)

    1 背景 实际应用系统中,为了安全起见,一般都必备用户认证(登录)和权限控制的功能,以识别用户是否合法,以及根据权限来控制用户是否能够执行某项操作. Spring Security是一个安全相关的框架 ...

  7. 浅析 Spring Security 的认证过程及相关过滤器

    前言 上一篇文章 浅析 Spring Security 核心组件 中介绍了Spring Security的基本组件,有了前面的基础,这篇文章就来详细分析下Spring Security的认证过程. S ...

  8. Spring Security自定义登录验证及登录返回结果

    Spring Security自定义登录验证及登录返回结果 一.功能描述 二.处理逻辑 简单流程 自定义UserDetails 自定义UserDetailsDAO 自定义UserDetailsServ ...

  9. Spring Security 自定义资源服务器实践

    相关文章: OAuth2的定义和运行流程 Spring Security OAuth实现Gitee快捷登录 Spring Security OAuth实现GitHub快捷登录 Spring Secur ...

最新文章

  1. Android URL
  2. 五花八门的bug跟踪工具
  3. Echarts笔记-折线图定制(Y轴百分数,鼠标移动显示百分数,显示X轴,Y轴值)
  4. spring编程式事务
  5. 埃氏筛 线性筛(欧拉筛) 算法解析
  6. linux下自己安装软件做成命令
  7. 双十一清醒指南,送3本Python书
  8. java加法运算器界面_Java 接口实现计算器加减乘除(字符交互界面)
  9. leetcode之53.最大子序和
  10. php使用jasperreport,用PHP访问JasperReport | 学步园
  11. [python + debug] set()操作对象的元素为字符串,则结果随机排序,使用sorted()函数以固定顺序
  12. python机器学习库sklearn——朴素贝叶斯分类器
  13. mappartitions java_Spark算子:RDD基本转换操作(5)–mapPartitions、mapPartitionsWithIndex
  14. 阿里代码规范检查工具的安装使用
  15. 同比增长率,环比增长速度、环比发展速度
  16. 2018年俄罗斯世界杯之Java数据爬虫(二)
  17. VUE+Nodejs 商城项目练习项目(前台购物)
  18. 极限思想在计算机中的应用,高等数学中极限思想的应用
  19. ZFS case : top CPU 100%sy, when no free memory trigger it.
  20. 新神魔大陆服务器现在在维护吗,新神魔大陆1月22日合服维护公告

热门文章

  1. Java业务逻辑pyqt_PyQt5 UI界面与业务逻辑分离
  2. 报错 插入更新_自增主键,三类插入测验答案,在这里。
  3. 把html变成桌面的软件,5 分钟把任意网站变成桌面软件
  4. matlab画置信区间图,matlab绘制带置信区间的双y轴图形 | 学步园
  5. 华硕无线路由打印机服务器,享受DIY的快乐 篇四:当普通打印机遇上智能路由器——网络打印机配置教程(以华硕AC66U B1为例)...
  6. 下载Java Jar包的网站(托管厂库)
  7. 一、使用两台Centos7系统搭建Hadoop-3.1.4完全分布式集群
  8. 期末考试前的预习,科目:化工设备与反应器(2)
  9. 化工原理知识点总结复习重点
  10. PageRanke算法