优化授权码模式的登录错误响应
This commit is contained in:
@@ -18,7 +18,6 @@ import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
import org.springframework.security.web.authentication.logout.LogoutHandler;
|
||||
@@ -37,8 +36,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private AuthenticationSuccessHandler authenticationSuccessHandler;
|
||||
@Autowired
|
||||
private AuthenticationFailureHandler authenticationFailureHandler;
|
||||
|
||||
@Autowired(required = false)
|
||||
private AuthenticationEntryPoint authenticationEntryPoint;
|
||||
@@ -82,7 +79,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.loginPage(SecurityConstants.LOGIN_PAGE)
|
||||
.loginProcessingUrl(SecurityConstants.OAUTH_LOGIN_PRO_URL)
|
||||
.successHandler(authenticationSuccessHandler)
|
||||
.failureHandler(authenticationFailureHandler)
|
||||
.and()
|
||||
.logout()
|
||||
.logoutUrl(SecurityConstants.LOGOUT_URL)
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
package com.central.oauth.config;
|
||||
|
||||
import com.central.common.utils.ResponseUtil;
|
||||
import com.central.oauth.handler.OauthLogoutHandler;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.common.exceptions.*;
|
||||
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
|
||||
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
|
||||
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -32,25 +26,6 @@ import java.io.IOException;
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class SecurityHandlerConfig {
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 登陆失败,返回401
|
||||
*/
|
||||
@Bean
|
||||
public AuthenticationFailureHandler loginFailureHandler() {
|
||||
return (request, response, exception) -> {
|
||||
String msg;
|
||||
if (exception instanceof BadCredentialsException) {
|
||||
msg = "密码错误";
|
||||
} else {
|
||||
msg = exception.getMessage();
|
||||
}
|
||||
ResponseUtil.responseWriter(objectMapper, response, msg, HttpStatus.UNAUTHORIZED.value());
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OauthLogoutHandler oauthLogoutHandler() {
|
||||
return new OauthLogoutHandler();
|
||||
|
||||
Reference in New Issue
Block a user