|
@ -2,8 +2,10 @@ package com.databasir.api.config.security; |
|
|
|
|
|
|
|
|
import com.databasir.common.JsonData; |
|
|
import com.databasir.common.JsonData; |
|
|
import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; |
|
|
import com.databasir.core.domain.app.exception.DatabasirAuthenticationException; |
|
|
|
|
|
import com.databasir.core.domain.log.service.OperationLogService; |
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.http.HttpStatus; |
|
|
import org.springframework.http.HttpStatus; |
|
|
import org.springframework.http.MediaType; |
|
|
import org.springframework.http.MediaType; |
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
|
import org.springframework.security.authentication.BadCredentialsException; |
|
@ -20,37 +22,51 @@ import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
|
@Component |
|
|
@Component |
|
|
@RequiredArgsConstructor |
|
|
@RequiredArgsConstructor |
|
|
|
|
|
@Slf4j |
|
|
public class DatabasirAuthenticationFailureHandler implements AuthenticationFailureHandler { |
|
|
public class DatabasirAuthenticationFailureHandler implements AuthenticationFailureHandler { |
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper; |
|
|
private final ObjectMapper objectMapper; |
|
|
|
|
|
|
|
|
|
|
|
private final OperationLogService operationLogService; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onAuthenticationFailure(HttpServletRequest request, |
|
|
public void onAuthenticationFailure(HttpServletRequest request, |
|
|
HttpServletResponse response, |
|
|
HttpServletResponse response, |
|
|
AuthenticationException exception) throws IOException, ServletException { |
|
|
AuthenticationException exception) throws IOException, ServletException { |
|
|
|
|
|
String username = request.getParameter("username"); |
|
|
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
|
|
response.setCharacterEncoding(StandardCharsets.UTF_8.name()); |
|
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE); |
|
|
response.setContentType(MediaType.APPLICATION_JSON_VALUE); |
|
|
if (exception instanceof BadCredentialsException) { |
|
|
if (exception instanceof BadCredentialsException) { |
|
|
JsonData<Void> data = JsonData.error("-1", "用户名或密码错误"); |
|
|
JsonData<Void> data = JsonData.error("-1", "用户名或密码错误"); |
|
|
|
|
|
saveLoginFailedLog(username, data.getErrMessage()); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
response.setStatus(HttpStatus.OK.value()); |
|
|
response.setStatus(HttpStatus.OK.value()); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
} else if (exception instanceof DisabledException) { |
|
|
} else if (exception instanceof DisabledException) { |
|
|
JsonData<Void> data = JsonData.error("-1", "用户已禁用"); |
|
|
JsonData<Void> data = JsonData.error("-1", "用户已禁用"); |
|
|
|
|
|
saveLoginFailedLog(username, data.getErrMessage()); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
response.setStatus(HttpStatus.OK.value()); |
|
|
response.setStatus(HttpStatus.OK.value()); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
} else if (exception instanceof DatabasirAuthenticationException) { |
|
|
} else if (exception instanceof DatabasirAuthenticationException) { |
|
|
DatabasirAuthenticationException bizException = (DatabasirAuthenticationException) exception; |
|
|
DatabasirAuthenticationException bizException = (DatabasirAuthenticationException) exception; |
|
|
JsonData<Void> data = JsonData.error(bizException.getErrCode(), bizException.getErrMessage()); |
|
|
JsonData<Void> data = JsonData.error(bizException.getErrCode(), bizException.getErrMessage()); |
|
|
|
|
|
saveLoginFailedLog(username, data.getErrMessage()); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
response.setStatus(HttpStatus.OK.value()); |
|
|
response.setStatus(HttpStatus.OK.value()); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
} else { |
|
|
} else { |
|
|
JsonData<Void> data = JsonData.error("-1", "未登录或未授权用户"); |
|
|
JsonData<Void> data = JsonData.error("-1", "未登录或未授权用户"); |
|
|
|
|
|
saveLoginFailedLog(username, data.getErrMessage()); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
String jsonString = objectMapper.writeValueAsString(data); |
|
|
response.setStatus(HttpStatus.UNAUTHORIZED.value()); |
|
|
response.setStatus(HttpStatus.UNAUTHORIZED.value()); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
response.getOutputStream().write(jsonString.getBytes(StandardCharsets.UTF_8)); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void saveLoginFailedLog(String username, String message) { |
|
|
|
|
|
if (username != null) { |
|
|
|
|
|
operationLogService.saveLoginFailedLog(username, message); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|