From 9947000ce1cfac48af267f116f5ed4fa271e7a3e Mon Sep 17 00:00:00 2001 From: zlt2000 Date: Wed, 27 May 2020 22:57:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8E=88=E6=9D=83=E7=A0=81?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=9A=84=E7=99=BB=E5=BD=95=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=93=8D=E5=BA=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../central/oauth/config/SecurityConfig.java | 4 --- .../oauth/config/SecurityHandlerConfig.java | 25 ------------------- .../oauth/filter/ValidateCodeFilter.java | 14 +++++------ .../src/main/resources/static/js/common.js | 5 ++++ zlt-uaa/src/main/resources/static/login.html | 1 + 5 files changed, 13 insertions(+), 36 deletions(-) diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java index 85f0669..874cdf5 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/config/SecurityConfig.java @@ -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) diff --git a/zlt-uaa/src/main/java/com/central/oauth/config/SecurityHandlerConfig.java b/zlt-uaa/src/main/java/com/central/oauth/config/SecurityHandlerConfig.java index 1762992..2867ac1 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/config/SecurityHandlerConfig.java +++ b/zlt-uaa/src/main/java/com/central/oauth/config/SecurityHandlerConfig.java @@ -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(); diff --git a/zlt-uaa/src/main/java/com/central/oauth/filter/ValidateCodeFilter.java b/zlt-uaa/src/main/java/com/central/oauth/filter/ValidateCodeFilter.java index e6729ea..80fe795 100644 --- a/zlt-uaa/src/main/java/com/central/oauth/filter/ValidateCodeFilter.java +++ b/zlt-uaa/src/main/java/com/central/oauth/filter/ValidateCodeFilter.java @@ -1,17 +1,20 @@ package com.central.oauth.filter; import com.central.common.constant.SecurityConstants; +import com.central.common.utils.ResponseUtil; import com.central.oauth.exception.ValidateCodeException; import com.central.oauth.service.IValidateCodeService; import com.central.oauth2.common.properties.SecurityProperties; import com.central.oauth2.common.util.AuthUtils; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.web.authentication.AuthenticationFailureHandler; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher; import org.springframework.web.filter.OncePerRequestFilter; +import javax.annotation.Resource; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -31,11 +34,8 @@ public class ValidateCodeFilter extends OncePerRequestFilter { @Autowired private SecurityProperties securityProperties; - /** - * 验证码校验失败处理器 - */ - @Autowired - private AuthenticationFailureHandler authenticationFailureHandler; + @Resource + private ObjectMapper objectMapper; /** * 验证请求url与配置的url是否匹配的工具类 @@ -73,7 +73,7 @@ public class ValidateCodeFilter extends OncePerRequestFilter { try { validateCodeService.validate(request); } catch (ValidateCodeException e) { - authenticationFailureHandler.onAuthenticationFailure(request, response, e); + ResponseUtil.responseWriter(objectMapper, response, e.getMessage(), HttpStatus.BAD_REQUEST.value()); return; } chain.doFilter(request, response); diff --git a/zlt-uaa/src/main/resources/static/js/common.js b/zlt-uaa/src/main/resources/static/js/common.js index 79fbd8e..caffc5e 100644 --- a/zlt-uaa/src/main/resources/static/js/common.js +++ b/zlt-uaa/src/main/resources/static/js/common.js @@ -34,4 +34,9 @@ $(function(){ $(this).parent().next().hide(); } }); + + let query = location.search; + if (query === '?error') { + $('#loginError').show(); + } }); \ No newline at end of file diff --git a/zlt-uaa/src/main/resources/static/login.html b/zlt-uaa/src/main/resources/static/login.html index 6802f40..a1c0453 100644 --- a/zlt-uaa/src/main/resources/static/login.html +++ b/zlt-uaa/src/main/resources/static/login.html @@ -30,6 +30,7 @@
+
用户名或密码错误
  -- GitLab