Merge remote-tracking branch 'origin/very_pig' into leng_dev

This commit is contained in:
pig666 committed 2020-05-30 10:11:38 +08:00
commit d7bb25f416
5 files changed
+39 -23

No files matched your search

@@ -17,7 +17,6 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.entity.SysLog;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
@@ -67,9 +66,7 @@ public class PigxAuthenticationFailureEventHandler implements AuthenticationFail
sysLog.setParams(username);
sysLog.setException(authenticationException.getLocalizedMessage());
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (StrUtil.isNotBlank(header)) {
sysLog.setServiceId(WebUtils.getClientId(header));
}
sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A"));
logService.saveLog(sysLog, SecurityConstants.FROM_IN);
@@ -17,7 +17,6 @@
package com.pig4cloud.pigx.auth.handler;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.pigx.admin.api.entity.SysLog;
import com.pig4cloud.pigx.admin.api.feign.RemoteLogService;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
@@ -61,9 +60,7 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc
sysLog.setTitle(username + "用户登录");
sysLog.setParams(username);
String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (StrUtil.isNotBlank(header)) {
sysLog.setServiceId(WebUtils.getClientId(header));
}
sysLog.setServiceId(WebUtils.extractClientId(header).orElse("N/A"));
logService.saveLog(sysLog, SecurityConstants.FROM_IN);
log.info("用户:{} 登录成功", username);
@@ -23,7 +23,6 @@ import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.pig4cloud.pigx.common.core.exception.CheckedException;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
@@ -40,6 +39,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
/**
@@ -205,23 +205,24 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
}
/**
* 从request 获取CLIENT_ID
*
* @return
* 解析 client id
* @param header
* @param defVal
* @return 如果解析失败返回默认值
*/
@SneakyThrows
public String getClientId(String header) {
public String extractClientId(String header,final String defVal) {
if (header == null || !header.startsWith(BASIC_)) {
throw new CheckedException("请求头中client信息为空");
log.debug("请求头中client信息为空: {}",header);
return defVal;
}
byte[] base64Token = header.substring(6).getBytes("UTF-8");
byte[] base64Token = header.substring(6).getBytes(StandardCharsets.UTF_8);
byte[] decoded;
try {
decoded = Base64.decode(base64Token);
} catch (IllegalArgumentException e) {
throw new CheckedException(
"Failed to decode basic authentication token");
log.debug("Failed to decode basic authentication token: {}",header);
return defVal;
}
String token = new String(decoded, StandardCharsets.UTF_8);
@@ -229,9 +230,30 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
int delim = token.indexOf(":");
if (delim == -1) {
throw new CheckedException("Invalid basic authentication token");
log.debug("Invalid basic authentication token: {}",header);
return defVal;
}
return token.substring(0, delim);
}
/**
* 从请求头中解析 client id
* @param header
* @return
*/
public Optional<String> extractClientId(String header){
return Optional.ofNullable(extractClientId(header,null));
}
/**
* 从request 获取CLIENT_ID
*
* @return
*/
public String getClientId(String header) {
String clientId = extractClientId(header,null);
if(clientId == null){
throw new CheckedException("Invalid basic authentication token");
}
return clientId;
}
}
@@ -19,11 +19,11 @@ package com.pig4cloud.pigx.common.security.listener;
import cn.hutool.core.collection.CollUtil;
import com.pig4cloud.pigx.common.security.handler.AuthenticationSuccessHandler;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -58,7 +58,7 @@ public class AuthenticationSuccessEventListener implements ApplicationListener<A
}
private boolean isUserAuthentication(Authentication authentication){
return authentication instanceof User || CollUtil.isNotEmpty(authentication.getAuthorities());
return authentication.getPrincipal() instanceof PigxUser || CollUtil.isNotEmpty(authentication.getAuthorities());
}
}
@@ -80,8 +80,8 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
// 终端设置不校验, 直接向下执行
try {
String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
String clientId = WebUtils.getClientId(header);
if (filterIgnorePropertiesConfig.getClients().contains(clientId)) {
String clientId = WebUtils.extractClientId(header).orElse(null);
if (clientId != null && filterIgnorePropertiesConfig.getClients().contains(clientId)) {
return chain.filter(exchange);
}