feat:自定义认证异常

This commit is contained in:
haoxr 2020-11-19 01:15:33 +08:00
parent d9649b5324
commit 57e8f80759
4 changed files with 30 additions and 12 deletions

View File

@ -11,6 +11,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager; 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.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
@ -121,4 +124,12 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
return accessToken; return accessToken;
}; };
} }
@Bean
public AuthenticationProvider daoAuthenticationProvider(){
DaoAuthenticationProvider impl = new DaoAuthenticationProvider();
impl.setUserDetailsService(userDetailsService);
impl.setHideUserNotFoundExceptions(false) ;
return impl ;
}
} }

View File

@ -3,6 +3,7 @@ package com.youlai.auth.exception;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException; import java.io.IOException;
public class CustomOAuth2ExceptionSerializer extends StdSerializer<CustomOAuth2Exception> { public class CustomOAuth2ExceptionSerializer extends StdSerializer<CustomOAuth2Exception> {
@ -14,14 +15,8 @@ public class CustomOAuth2ExceptionSerializer extends StdSerializer<CustomOAuth2E
@Override @Override
public void serialize(CustomOAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { public void serialize(CustomOAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject(); jsonGenerator.writeStartObject();
jsonGenerator.writeObjectField("code", e.getHttpErrorCode()); jsonGenerator.writeObjectField("code", e.getHttpErrorCode());
jsonGenerator.writeStringField("msg", e.getOAuth2ErrorCode());
jsonGenerator.writeEndObject(); jsonGenerator.writeEndObject();
} }
} }

View File

@ -1,5 +1,6 @@
package com.youlai.auth.exception; package com.youlai.auth.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator; import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
@ -10,10 +11,18 @@ public class CustomOAuth2ExceptionTranslator implements WebResponseExceptionTran
@Override @Override
public ResponseEntity translate(Exception exception) throws Exception { public ResponseEntity translate(Exception exception) throws Exception {
if (exception instanceof OAuth2Exception) { if (exception instanceof OAuth2Exception) {
OAuth2Exception oAuth2Exception = (OAuth2Exception) exception;
return ResponseEntity
.status(oAuth2Exception.getHttpErrorCode())
.body(new CustomOAuth2Exception(oAuth2Exception.getMessage()));
}else if(exception instanceof AuthenticationException){ }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()));
} }
} }

View File

@ -39,7 +39,9 @@ public class UserDetailsServiceImpl implements UserDetailsService {
switch (clientId) { switch (clientId) {
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户 case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
Result<UserDTO> userResult = adminUserFeignClient.loadUserByUsername(username); 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("用户不存在"); throw new UsernameNotFoundException("用户不存在");
} }
UserDTO userDTO = userResult.getData(); UserDTO userDTO = userResult.getData();
@ -48,7 +50,8 @@ public class UserDetailsServiceImpl implements UserDetailsService {
break; break;
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员 case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
Result<MemberDTO> memberResult = umsMemberFeignClient.loadMemberByOpenid(username); 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("会员不存在"); throw new UsernameNotFoundException("会员不存在");
} }
MemberDTO memberDTO = memberResult.getData(); MemberDTO memberDTO = memberResult.getData();