From 57e8f8075958f8a9d1a8f6e7078a606de058535c Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Thu, 19 Nov 2020 01:15:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=87=AA=E5=AE=9A=E4=B9=89=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/config/AuthorizationServerConfig.java | 11 +++++++++++ .../CustomOAuth2ExceptionSerializer.java | 9 ++------- .../CustomOAuth2ExceptionTranslator.java | 15 ++++++++++++--- .../auth/service/UserDetailsServiceImpl.java | 7 +++++-- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/youlai-auth/src/main/java/com/youlai/auth/config/AuthorizationServerConfig.java b/youlai-auth/src/main/java/com/youlai/auth/config/AuthorizationServerConfig.java index 008c9b9aa..30894773b 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/config/AuthorizationServerConfig.java +++ b/youlai-auth/src/main/java/com/youlai/auth/config/AuthorizationServerConfig.java @@ -11,6 +11,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; @@ -121,4 +124,12 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap return accessToken; }; } + + @Bean + public AuthenticationProvider daoAuthenticationProvider(){ + DaoAuthenticationProvider impl = new DaoAuthenticationProvider(); + impl.setUserDetailsService(userDetailsService); + impl.setHideUserNotFoundExceptions(false) ; + return impl ; + } } diff --git a/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionSerializer.java b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionSerializer.java index 32462b8b7..8cc2d9fa0 100644 --- a/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionSerializer.java +++ b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionSerializer.java @@ -3,6 +3,7 @@ package com.youlai.auth.exception; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.ser.std.StdSerializer; + import java.io.IOException; public class CustomOAuth2ExceptionSerializer extends StdSerializer { @@ -14,14 +15,8 @@ public class CustomOAuth2ExceptionSerializer extends StdSerializer userResult = adminUserFeignClient.loadUserByUsername(username); - if (userResult == null || !ResultCode.SUCCESS.getCode().equals(userResult.getCode())) { + if (userResult == null || !ResultCode.SUCCESS.getCode().equals(userResult.getCode()) + || userResult.getData() == null + ) { throw new UsernameNotFoundException("用户不存在"); } UserDTO userDTO = userResult.getData(); @@ -48,7 +50,8 @@ public class UserDetailsServiceImpl implements UserDetailsService { break; case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员 Result memberResult = umsMemberFeignClient.loadMemberByOpenid(username); - if (memberResult == null || !ResultCode.SUCCESS.getCode().equals(memberResult.getCode())) { + if (memberResult == null || !ResultCode.SUCCESS.getCode().equals(memberResult.getCode()) + ||memberResult.getData()==null) { throw new UsernameNotFoundException("会员不存在"); } MemberDTO memberDTO = memberResult.getData();