|
|
|
@ -26,6 +26,7 @@ import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
|
@ -65,14 +66,18 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
|
|
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request,
|
|
|
|
|
HttpServletResponse response) throws AuthenticationException {
|
|
|
|
|
// Get logged in information from the input stream.
|
|
|
|
|
Authentication authenticate = null;
|
|
|
|
|
try {
|
|
|
|
|
LoginUser loginUser = new ObjectMapper().readValue(request.getInputStream(), LoginUser.class);
|
|
|
|
|
rememberMe.set(loginUser.getRememberMe());
|
|
|
|
|
return authenticationManager.authenticate(
|
|
|
|
|
authenticate = authenticationManager.authenticate(
|
|
|
|
|
new UsernamePasswordAuthenticationToken(loginUser.getUsername(), loginUser.getPassword(), new ArrayList()));
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("attemptAuthentication error :{}", e);
|
|
|
|
|
return null;
|
|
|
|
|
} catch (BadCredentialsException e) {
|
|
|
|
|
log.warn("BadCredentialsException:{}", e.getMessage());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("attemptauthentication error:", e);
|
|
|
|
|
} finally {
|
|
|
|
|
return authenticate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -102,7 +107,7 @@ public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
|
|
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException {
|
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
response.getWriter().write(JSONUtil.toJsonStr(new ReturnT(-1, "Server Error")));
|
|
|
|
|
}
|
|
|
|
|