Merge remote-tracking branch 'origin/master'

# Conflicts:
#	bonus-gateway/src/main/java/com/bonus/gateway/filter/ValidateCodeFilter.java
This commit is contained in:
jjLv 2024-08-02 11:18:37 +08:00
commit d136050374
6 changed files with 96 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package com.bonus.auth.controller;
import javax.servlet.http.HttpServletRequest;
import com.bonus.common.core.utils.global.SystemGlobal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -46,7 +47,7 @@ public class TokenController
public R<?> logout(HttpServletRequest request) {
try{
String token = SecurityUtils.getToken(request);
if (StringUtils.isNotEmpty(token))
if (StringUtils.isNotEmpty(token) && !SystemGlobal.undefined.equals(token.toLowerCase()))
{
String username = JwtUtils.getUserName(token);
String userId= JwtUtils.getUserId(token);

View File

@ -9,8 +9,65 @@ public class CaptchaException extends RuntimeException
{
private static final long serialVersionUID = 1L;
public CaptchaException(String msg)
/**
* 错误码
*/
private Integer code;
/**
* 错误提示
*/
private String message;
/**
* 错误明细内部调试错误
*/
private String detailMessage;
/**
* 空构造方法避免反序列化问题
*/
public CaptchaException()
{
super(msg);
}
public CaptchaException(String message)
{
this.message = message;
}
public CaptchaException(String message, Integer code)
{
this.message = message;
this.code = code;
}
public String getDetailMessage()
{
return detailMessage;
}
@Override
public String getMessage()
{
return message;
}
public Integer getCode()
{
return code;
}
public CaptchaException setMessage(String message)
{
this.message = message;
return this;
}
public CaptchaException setDetailMessage(String detailMessage)
{
this.detailMessage = detailMessage;
return this;
}
}

View File

@ -5,6 +5,11 @@ package com.bonus.common.core.utils.global;
* 全局变量
*/
public class SystemGlobal {
/**
* true
*/
public static String undefined="undefined";
/**
* true
*/

View File

@ -103,8 +103,7 @@ public class TokenService
LoginUser user = null;
try
{
if (StringUtils.isNotEmpty(token))
{
if (StringUtils.isNotEmpty(token)) {
String userkey = JwtUtils.getUserKey(token);
user = redisService.getCacheObject(getTokenKey(userkey));
return user;

View File

@ -1,5 +1,6 @@
package com.bonus.gateway.filter;
import com.bonus.common.core.utils.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -9,6 +10,7 @@ import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import com.bonus.common.core.constant.CacheConstants;
import com.bonus.common.core.constant.HttpStatus;
@ -140,10 +142,18 @@ public class AuthFilter implements GlobalFilter, Ordered
{
String token = request.getHeaders().getFirst(TokenConstants.AUTHENTICATION);
// 如果前端设置了令牌前缀则裁剪掉前缀
if (StringUtils.isNotEmpty(token) && token.startsWith(TokenConstants.PREFIX))
{
if (StringUtils.isNotEmpty(token) && token.startsWith(TokenConstants.PREFIX)) {
token = token.replaceFirst(TokenConstants.PREFIX, StringUtils.EMPTY);
}
if(StringHelper.isEmpty(token)){
MultiValueMap<String,String> maps=request.getQueryParams();
if(maps!=null && maps.get(TokenConstants.AUTHENTICATION)!=null && maps.get(TokenConstants.AUTHENTICATION).size()>0){
token =maps.get(TokenConstants.AUTHENTICATION).get(0);
if("null".equals(token)){
token=null;
}
}
}
return token;
}

View File

@ -1,5 +1,9 @@
package com.bonus.gateway.handler;
import com.bonus.common.core.exception.GlobalException;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.slf4j.Logger;
@ -8,11 +12,14 @@ import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import com.bonus.common.core.utils.ServletUtils;
import reactor.core.publisher.Mono;
import javax.servlet.http.HttpServletRequest;
/**
* 网关统一异常处理
*
@ -27,6 +34,16 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler
private static final Logger log = LoggerFactory.getLogger(GatewayExceptionHandler.class);
/**
* 验证码异常处理
*/
@ExceptionHandler(GlobalException.class)
public AjaxResult handleGlobalException(ServiceException e, HttpServletRequest request)
{
log.error(e.getMessage(), e);
Integer code = e.getCode();
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
}
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex)