當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Security——自定义认证错误提示信息及自适应返回格式解决方案
生活随笔
收集整理的這篇文章主要介紹了
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——自定义认证错误提示信息及自适应返回格式解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL——一种简单的基于角色控制的权限管
- 下一篇: Spring Boot——获取上传文件的