feat:添加auth模块自定义异常

This commit is contained in:
haoxr 2020-11-18 20:23:12 +08:00
parent 48d97c9eec
commit d9649b5324
4 changed files with 67 additions and 1 deletions

View File

@ -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());
}
/**

View File

@ -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);
}
}

View File

@ -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<CustomOAuth2Exception> {
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();
}
}

View File

@ -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;
}
}