From d9649b5324fad49829cbb489eca818b60c4b80f6 Mon Sep 17 00:00:00 2001 From: haoxr <1490493387@qq.com> Date: Wed, 18 Nov 2020 20:23:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0auth=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/AuthorizationServerConfig.java | 5 +++- .../auth/exception/CustomOAuth2Exception.java | 17 ++++++++++++ .../CustomOAuth2ExceptionSerializer.java | 27 +++++++++++++++++++ .../CustomOAuth2ExceptionTranslator.java | 19 +++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2Exception.java create mode 100644 youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionSerializer.java create mode 100644 youlai-auth/src/main/java/com/youlai/auth/exception/CustomOAuth2ExceptionTranslator.java 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; + } +}