[prod]-修复封禁原因展示问题

This commit is contained in:
liaozetao
2024-06-07 14:44:22 +08:00
parent acf82a845f
commit b03f2b0ebe
2 changed files with 37 additions and 27 deletions

View File

@@ -1,5 +1,7 @@
package com.accompany.oauth2.exception;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.accompany.common.status.BusiStatus;
import com.accompany.core.base.SpringContextHolder;
import com.fasterxml.jackson.core.JsonGenerator;
@@ -10,6 +12,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;
public class CustomOAuthExceptionJacksonSerializer extends StdSerializer<CustomOAuth2Exception> {
protected CustomOAuthExceptionJacksonSerializer() {
@@ -20,22 +23,20 @@ public class CustomOAuthExceptionJacksonSerializer extends StdSerializer<CustomO
public void serialize(CustomOAuth2Exception value, JsonGenerator jgen, SerializerProvider serializerProvider) throws IOException {
jgen.writeStartObject();
String errorCode = value.getOAuth2ErrorCode();
BusiStatus status;
String errorMessage;
switch (errorCode){
switch (errorCode) {
case CustomOAuth2Exception.INVALID_CLIENT:
errorMessage = value.getMessage().toLowerCase();
if(errorMessage.contains("bad") && errorMessage.contains("credentials")){
if (errorMessage.contains("bad") && errorMessage.contains("credentials")) {
status = BusiStatus.CLIENT_SECRET_MISMATCH;
}else {
} else {
status = BusiStatus.INVALID_CLIENT_ID;
}
break;
case CustomOAuth2Exception.UNAUTHORIZED:
errorMessage = value.getMessage().toLowerCase();
if(errorMessage.contains("user") && errorMessage.contains("not found")) {
if (errorMessage.contains("user") && errorMessage.contains("not found")) {
status = BusiStatus.INVALID_USER;
} else {
status = BusiStatus.INVALID_REQUEST;
@@ -48,11 +49,11 @@ public class CustomOAuthExceptionJacksonSerializer extends StdSerializer<CustomO
errorMessage = value.getMessage().toLowerCase();
if (errorMessage.contains("redirect") && errorMessage.contains("match")) {
status = BusiStatus.REDIRECT_URI_MISMATCH;
}else if(errorMessage.contains("bad") && errorMessage.contains("credentials")){
} else if (errorMessage.contains("bad") && errorMessage.contains("credentials")) {
status = BusiStatus.USERNAME_PASSWORD_MISMATCH;
}else if(errorMessage.contains("invalid refresh token")){
} else if (errorMessage.contains("invalid refresh token")) {
status = BusiStatus.INVALID_TOKEN;
}else {
} else {
status = BusiStatus.INVALID_GRANT;
}
break;
@@ -61,9 +62,9 @@ public class CustomOAuthExceptionJacksonSerializer extends StdSerializer<CustomO
break;
case CustomOAuth2Exception.INVALID_TOKEN:
errorMessage = value.getMessage().toLowerCase();
if(errorMessage.contains("access token expired")){
if (errorMessage.contains("access token expired")) {
status = BusiStatus.ACCESS_TOKEN_HAS_EXPIRED;
}else{
} else {
status = BusiStatus.INVALID_TOKEN;
}
break;
@@ -149,15 +150,24 @@ public class CustomOAuthExceptionJacksonSerializer extends StdSerializer<CustomO
status = BusiStatus.UNKNOWN;
break;
}
jgen.writeNumberField("code", status.value());
String CODE = "code";
String MESSAGE = "message";
String REASON = "reason";
String DATE = "date";
jgen.writeNumberField(CODE, status.value());
Locale locale = LocaleContextHolder.getLocale();
String i18nId = BusiStatus.class.getSimpleName() + "." + status.getName();
String i18nId = BusiStatus.class.getSimpleName() + StrUtil.DOT + status.getName();
String abc = SpringContextHolder.getBean(MessageSource.class).getMessage(i18nId, null, status.getMessage(), locale);
jgen.writeStringField("message", abc);
jgen.writeStringField(MESSAGE, abc);
Map<String, String> additionalInformation = value.getAdditionalInformation();
if (CollectionUtil.isNotEmpty(additionalInformation)) {
if (additionalInformation.containsKey(REASON)) {
jgen.writeStringField(REASON, additionalInformation.get(REASON));
}
if (additionalInformation.containsKey(DATE)) {
jgen.writeStringField(DATE, additionalInformation.get(DATE));
}
}
jgen.writeEndObject();
}
}

View File

@@ -44,7 +44,7 @@ public class AccountBlockCheckService {
String phone = account.getPhone();
blockType = BlockTypeEnum.BLOCK_PHONE.getValue();
accountCache = jedisService.hget(RedisKey.block_account.getKey(blockType.toString()), phone);
if(!StringUtils.isEmpty(accountCache)){
if (!StringUtils.isEmpty(accountCache)) {
checkAccountBlock(accountCache);
}
}
@@ -58,9 +58,9 @@ public class AccountBlockCheckService {
if (StrUtil.isEmpty(deviceId)) {
return;
}
Integer blockType = BlockTypeEnum.BLOCK_DEVICE.getValue();
String deviceCache = jedisService.hget(RedisKey.block_account.getKey(blockType.toString()), deviceId);
if (!StringUtils.isEmpty(deviceCache)) {
int blockType = BlockTypeEnum.BLOCK_DEVICE.getValue();
String deviceCache = jedisService.hget(RedisKey.block_account.getKey(Integer.toString(blockType)), deviceId);
if (StrUtil.isNotEmpty(deviceCache)) {
checkAccountBlock(deviceCache);
}
}
@@ -69,20 +69,20 @@ public class AccountBlockCheckService {
if (StrUtil.isEmpty(ip)) {
return;
}
Integer blockType = BlockTypeEnum.BLOCK_IP.getValue();
String ipCache = jedisService.hget(RedisKey.block_account.getKey(blockType.toString()), ip);
if (!StringUtils.isEmpty(ipCache)) {
int blockType = BlockTypeEnum.BLOCK_IP.getValue();
String ipCache = jedisService.hget(RedisKey.block_account.getKey(Integer.toString(blockType)), ip);
if (StrUtil.isNotEmpty(ipCache)) {
checkAccountBlock(ipCache);
}
}
private void checkAccountBlock(String accountBlockCache){
private void checkAccountBlock(String accountBlockCache) {
AccountBlock accountBlock = gson.fromJson(accountBlockCache, AccountBlock.class);
boolean betweenDate = DateTimeUtil.isBetweenDate(Calendar.getInstance().getTime(), accountBlock.getBlockStartTime(), accountBlock.getBlockEndTime());
if (betweenDate && accountBlock.getBlockStatus() != null && BlockStatusEnum.BLOCKING.getValue() == accountBlock.getBlockStatus().byteValue()) {
log.info("用户被封禁,blockValue =" + accountBlock.getBlockValue());
BlockTypeEnum blockTypeEnum = BlockTypeEnum.get(accountBlock.getBlockType());
CustomOAuth2Exception exception = new CustomOAuth2Exception(CustomOAuth2Exception.ACCOUNT_ERROR,blockTypeEnum.getBlockDesc());
CustomOAuth2Exception exception = new CustomOAuth2Exception(CustomOAuth2Exception.ACCOUNT_ERROR, blockTypeEnum.getBlockDesc());
exception.addAdditionalInformation("reason", accountBlock.getBlockDesc());
exception.addAdditionalInformation("date", String.valueOf(accountBlock.getBlockEndTime().getTime()));
throw exception;