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 94e7f4376..008c9b9aa 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 @@ -1,6 +1,7 @@ package com.youlai.auth.config; import com.youlai.auth.domain.User; +import com.youlai.auth.exception.CustomOAuth2ExceptionTranslator; import com.youlai.auth.service.JdbcClientDetailsServiceImpl; import com.youlai.auth.service.UserDetailsServiceImpl; import com.youlai.common.core.constant.AuthConstants; @@ -70,7 +71,9 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap // refresh token有两种使用方式:重复使用(true)、非重复使用(false),默认为true // 1 重复使用:access token过期刷新时, refresh token过期时间未改变,仍以初次生成的时间为准 // 2 非重复使用:access token过期刷新时, refresh token过期时间延续,在refresh token有效期内刷新便永不失效达到无需再次登录的目的 - .reuseRefreshTokens(false); + .reuseRefreshTokens(false) + .exceptionTranslator(new CustomOAuth2ExceptionTranslator()); + } /** diff --git a/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2Exception.java b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2Exception.java new file mode 100644 index 000000000..09602976b --- /dev/null +++ b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2Exception.java @@ -0,0 +1,17 @@ +package com.youlai.auth.exception; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; + + +@JsonSerialize(using = CustomOAuth2ExceptionSerializer.class) +public class CustomOAuth2Exception extends OAuth2Exception { + + public CustomOAuth2Exception(String msg, Throwable t) { + super(msg, t); + } + + public CustomOAuth2Exception(String msg) { + super(msg); + } +} 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 new file mode 100644 index 000000000..32462b8b7 --- /dev/null +++ b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionSerializer.java @@ -0,0 +1,27 @@ +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 { + + protected CustomOAuth2ExceptionSerializer() { + super(CustomOAuth2Exception.class); + } + + @Override + public void serialize(CustomOAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeStartObject(); + + jsonGenerator.writeObjectField("code", e.getHttpErrorCode()); + + + + + + + jsonGenerator.writeEndObject(); + } +} diff --git a/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionTranslator.java b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionTranslator.java new file mode 100644 index 000000000..fe8408b30 --- /dev/null +++ b/youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionTranslator.java @@ -0,0 +1,19 @@ +package com.youlai.auth.exception; + +import org.springframework.http.ResponseEntity; +import org.springframework.security.oauth2.common.exceptions.OAuth2Exception; +import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator; + +import javax.naming.AuthenticationException; + +public class CustomOAuth2ExceptionTranslator implements WebResponseExceptionTranslator { + @Override + public ResponseEntity translate(Exception exception) throws Exception { + if (exception instanceof OAuth2Exception) { + + }else if(exception instanceof AuthenticationException){ + + } + return null; + } +}