|
@@ -1,11 +1,5 @@
|
|
|
package com.ym.mec.auth.handler;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
-
|
|
|
-import javax.servlet.ServletException;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.ym.mec.common.entity.HttpResponseResult;
|
|
|
import org.apache.http.HttpStatus;
|
|
@@ -13,8 +7,12 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
+import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
|
|
|
import org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
@Component
|
|
|
public class BaseAuthenticationFailureEvenHandler extends ExceptionMappingAuthenticationFailureHandler {
|
|
@@ -25,13 +23,15 @@ public class BaseAuthenticationFailureEvenHandler extends ExceptionMappingAuthen
|
|
|
private final static Logger logger = LoggerFactory.getLogger(BaseAuthenticationFailureEvenHandler.class);
|
|
|
|
|
|
@Override
|
|
|
- public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException)
|
|
|
- throws IOException, ServletException {
|
|
|
-
|
|
|
+ public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
|
|
|
+ throws IOException{
|
|
|
+ String message = e.getMessage();
|
|
|
Object username = request.getAttribute("SPRING_SECURITY_LAST_USERNAME_KEY");
|
|
|
-
|
|
|
- logger.info("用户:{} 登录失败,异常:{}", username, authenticationException.getLocalizedMessage());
|
|
|
- HttpResponseResult result = new HttpResponseResult(false, HttpStatus.SC_CONFLICT, null, authenticationException.getLocalizedMessage());
|
|
|
+ if (e.getLocalizedMessage() != null && e.getLocalizedMessage().equals("Bad credentials")) {
|
|
|
+ message = "用户名或密码错误";
|
|
|
+ }
|
|
|
+ logger.info("用户:{} 登录失败,异常:{}", username, message);
|
|
|
+ HttpResponseResult result = new HttpResponseResult(false, HttpStatus.SC_CONFLICT, null, message);
|
|
|
response.setContentType("application/json; charset=utf-8");
|
|
|
response.getWriter().write(objectMapper.writeValueAsString(result));
|
|
|
// super.onAuthenticationFailure(request, response, authenticationException);
|