日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2024/10/5 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Security——自定义认证错误提示信息及自适应返回格式解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

解決方案

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——自定义认证错误提示信息及自适应返回格式解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。