mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-03 17:42:20 +08:00
feat:自定义认证异常
This commit is contained in:
parent
d9649b5324
commit
57e8f80759
@ -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 ;
|
||||
}
|
||||
}
|
||||
|
@ -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<CustomOAuth2Exception> {
|
||||
@ -14,14 +15,8 @@ public class CustomOAuth2ExceptionSerializer extends StdSerializer<CustomOAuth2E
|
||||
@Override
|
||||
public void serialize(CustomOAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||
jsonGenerator.writeStartObject();
|
||||
|
||||
jsonGenerator.writeObjectField("code", e.getHttpErrorCode());
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
jsonGenerator.writeStringField("msg", e.getOAuth2ErrorCode());
|
||||
jsonGenerator.writeEndObject();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.youlai.auth.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
|
||||
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
|
||||
@ -10,10 +11,18 @@ public class CustomOAuth2ExceptionTranslator implements WebResponseExceptionTran
|
||||
@Override
|
||||
public ResponseEntity translate(Exception exception) throws Exception {
|
||||
if (exception instanceof OAuth2Exception) {
|
||||
|
||||
OAuth2Exception oAuth2Exception = (OAuth2Exception) exception;
|
||||
return ResponseEntity
|
||||
.status(oAuth2Exception.getHttpErrorCode())
|
||||
.body(new CustomOAuth2Exception(oAuth2Exception.getMessage()));
|
||||
}else if(exception instanceof AuthenticationException){
|
||||
|
||||
AuthenticationException authenticationException = (AuthenticationException) exception;
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.UNAUTHORIZED)
|
||||
.body(new CustomOAuth2Exception(authenticationException.getMessage()));
|
||||
}
|
||||
return null;
|
||||
return ResponseEntity
|
||||
.status(HttpStatus.OK)
|
||||
.body(new CustomOAuth2Exception(exception.getMessage()));
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,9 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
switch (clientId) {
|
||||
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
|
||||
Result<UserDTO> 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<MemberDTO> 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();
|
||||
|
Loading…
Reference in New Issue
Block a user