- * 扩展 JdbcClientDetailsService 支持多租户 - */ -class PigxClientDetailsService extends JdbcClientDetailsService { - - public PigxClientDetailsService(DataSource dataSource) { - super(dataSource); - } - - /** - * 重写原生方法支持redis缓存 - * - * @param clientId - * @return ClientDetails - * @throws InvalidClientException - */ - @Override - @Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null") - public ClientDetails loadClientByClientId(String clientId) { - super.setSelectClientDetailsSql(String.format(SecurityConstants.DEFAULT_SELECT_STATEMENT - , TenantContextHolder.getTenantId())); - return super.loadClientByClientId(clientId); - } -} \ No newline at end of file diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java index 99ebb2ac7..f897ccc57 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationFailureEventHandler.java @@ -17,10 +17,23 @@ package com.pig4cloud.pigx.auth.handler; -import com.pig4cloud.pigx.common.security.handler.AbstractAuthenticationFailureEventHandler; +import com.pig4cloud.pigx.admin.api.entity.SysLog; +import com.pig4cloud.pigx.admin.api.feign.RemoteLogService; +import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.util.WebUtils; +import com.pig4cloud.pigx.common.log.util.SysLogUtils; +import com.pig4cloud.pigx.common.security.handler.AuthenticationFailureHandler; +import lombok.AllArgsConstructor; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.scheduling.annotation.Async; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; +import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @@ -32,10 +45,12 @@ import javax.servlet.http.HttpServletResponse; */ @Slf4j @Component -public class PigxAuthenticationFailureEventHandler extends AbstractAuthenticationFailureEventHandler { +@AllArgsConstructor +public class PigxAuthenticationFailureEventHandler implements AuthenticationFailureHandler { + private final RemoteLogService logService; /** - * 处理登录失败方法 + * 异步处理,登录失败方法 *
* * @param authenticationException 登录的authentication 对象 @@ -43,8 +58,20 @@ public class PigxAuthenticationFailureEventHandler extends AbstractAuthenticatio * @param request 请求 * @param response 响应 */ + @Async @Override + @SneakyThrows public void handle(AuthenticationException authenticationException, Authentication authentication, HttpServletRequest request, HttpServletResponse response) { - log.info("用户:{} 登录失败,异常:{}", authentication.getPrincipal(), authenticationException.getLocalizedMessage()); + String username = authentication.getName(); + SysLog sysLog = SysLogUtils.getSysLog(request, username); + sysLog.setTitle(username + "用户登录"); + sysLog.setType(CommonConstants.STATUS_LOCK); + sysLog.setParams(username); + String header = request.getHeader(HttpHeaders.AUTHORIZATION); + sysLog.setServiceId(WebUtils.getClientId(header)[0]); + sysLog.setException(authenticationException.getLocalizedMessage()); + logService.saveLog(sysLog, SecurityConstants.FROM_IN); + + log.info("用户:{} 登录失败,异常:{}", username, authenticationException.getLocalizedMessage()); } } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java index 0dd045565..6592d5e8f 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/handler/PigxAuthenticationSuccessEventHandler.java @@ -17,13 +17,24 @@ package com.pig4cloud.pigx.auth.handler; -import com.pig4cloud.pigx.common.security.handler.AbstractAuthenticationSuccessEventHandler; +import com.pig4cloud.pigx.admin.api.entity.SysLog; +import com.pig4cloud.pigx.admin.api.feign.RemoteLogService; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.util.WebUtils; +import com.pig4cloud.pigx.common.log.util.SysLogUtils; +import com.pig4cloud.pigx.common.security.handler.AuthenticationSuccessHandler; +import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.scheduling.annotation.Async; import org.springframework.security.core.Authentication; +import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.concurrent.TimeUnit; /** * @author lengleng @@ -31,7 +42,9 @@ import javax.servlet.http.HttpServletResponse; */ @Slf4j @Component -public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticationSuccessEventHandler { +@AllArgsConstructor +public class PigxAuthenticationSuccessEventHandler implements AuthenticationSuccessHandler { + private final RemoteLogService logService; /** * 处理登录成功方法 @@ -42,8 +55,16 @@ public class PigxAuthenticationSuccessEventHandler extends AbstractAuthenticatio * @param request 请求 * @param response 返回 */ + @Async @Override public void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response) { - log.info("用户:{} 登录成功", authentication.getPrincipal()); + String username = authentication.getName(); + SysLog sysLog = SysLogUtils.getSysLog(request, username); + sysLog.setTitle(username + "用户登录"); + sysLog.setParams(username); + String header = request.getHeader(HttpHeaders.AUTHORIZATION); + sysLog.setServiceId(WebUtils.getClientId(header)[0]); + logService.saveLog(sysLog, SecurityConstants.FROM_IN); + log.info("用户:{} 登录成功", username); } } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxClientDetailsServiceImpl.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxClientDetailsServiceImpl.java new file mode 100644 index 000000000..552f255e3 --- /dev/null +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxClientDetailsServiceImpl.java @@ -0,0 +1,42 @@ +package com.pig4cloud.pigx.auth.service; + +import com.pig4cloud.pigx.common.core.constant.CacheConstants; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder; +import lombok.AllArgsConstructor; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.security.oauth2.common.exceptions.InvalidClientException; +import org.springframework.security.oauth2.provider.ClientDetails; +import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService; +import org.springframework.stereotype.Service; + +import javax.sql.DataSource; + +/** + * @author lengleng + * @date 2020/03/25 + *
+ * 扩展 JdbcClientDetailsService 支持多租户
+ */
+@Service
+public class PigxClientDetailsServiceImpl extends JdbcClientDetailsService {
+ public PigxClientDetailsServiceImpl(DataSource dataSource) {
+ super(dataSource);
+ }
+
+
+ /**
+ * 重写原生方法支持redis缓存
+ *
+ * @param clientId
+ * @return ClientDetails
+ * @throws InvalidClientException
+ */
+ @Override
+ @Cacheable(value = CacheConstants.CLIENT_DETAILS_KEY, key = "#clientId", unless = "#result == null")
+ public ClientDetails loadClientByClientId(String clientId) {
+ super.setSelectClientDetailsSql(String.format(SecurityConstants.DEFAULT_SELECT_STATEMENT
+ , TenantContextHolder.getTenantId()));
+ return super.loadClientByClientId(clientId);
+ }
+}
diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java
similarity index 94%
rename from pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java
rename to pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java
index 819269f59..f9d307532 100644
--- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/service/PigxUserDetailsServiceImpl.java
+++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxUserDetailsServiceImpl.java
@@ -15,7 +15,7 @@
* Author: lengleng (wangiegie@gmail.com)
*/
-package com.pig4cloud.pigx.common.security.service;
+package com.pig4cloud.pigx.auth.service;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
@@ -26,6 +26,8 @@ import com.pig4cloud.pigx.common.core.constant.CacheConstants;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
+import com.pig4cloud.pigx.common.security.service.PigxUser;
+import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@@ -35,6 +37,7 @@ import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collection;
@@ -47,6 +50,7 @@ import java.util.Set;
* @author lengleng
*/
@Slf4j
+@Service
@AllArgsConstructor
public class PigxUserDetailsServiceImpl implements PigxUserDetailsService {
private final RemoteUserService remoteUserService;
diff --git a/pigx-auth/src/main/resources/bootstrap.yml b/pigx-auth/src/main/resources/bootstrap.yml
index 124c2091d..61c48d630 100644
--- a/pigx-auth/src/main/resources/bootstrap.yml
+++ b/pigx-auth/src/main/resources/bootstrap.yml
@@ -8,6 +8,8 @@ spring:
nacos:
discovery:
server-addr: ${NACOS-HOST:pigx-register}:${NACOS-PORT:8848}
+ metadata:
+ VERSION: lengleng
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java
index 7b6817a83..5017515fc 100644
--- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java
+++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/WebUtils.java
@@ -212,8 +212,7 @@ public class WebUtils extends org.springframework.web.util.WebUtils {
* @return
*/
@SneakyThrows
- public String[] getClientId(ServerHttpRequest request) {
- String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
+ public String[] getClientId(String header) {
if (header == null || !header.startsWith(BASIC_)) {
throw new CheckedException("请求头中client信息为空");
diff --git a/pigx-common/pigx-common-data/pom.xml b/pigx-common/pigx-common-data/pom.xml
index be77f75f4..fe15dec66 100644
--- a/pigx-common/pigx-common-data/pom.xml
+++ b/pigx-common/pigx-common-data/pom.xml
@@ -49,6 +49,11 @@
- *
- * @param authenticationException 登录的authentication 对象
- * @param authentication 登录的authenticationException 对象
- * @param request 请求
- * @param response 响应
- */
- public abstract void handle(AuthenticationException authenticationException, Authentication authentication
- , HttpServletRequest request, HttpServletResponse response);
}
diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java
similarity index 73%
rename from pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java
rename to pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java
index fb69571f2..2cf9985f8 100644
--- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/handler/AbstractAuthenticationSuccessEventHandler.java
+++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/listener/AuthenticationSuccessEventListener.java
@@ -15,12 +15,16 @@
* Author: lengleng (wangiegie@gmail.com)
*/
-package com.pig4cloud.pigx.common.security.handler;
+package com.pig4cloud.pigx.common.security.listener;
import cn.hutool.core.collection.CollUtil;
+import com.pig4cloud.pigx.common.security.handler.AuthenticationSuccessHandler;
+import lombok.AllArgsConstructor;
+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.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -29,10 +33,13 @@ import javax.servlet.http.HttpServletResponse;
/**
* @author lengleng
- * @date 2018/10/8
- * 认证成功事件处理器
+ * @date 2020/03/25
+ * 认证成功事件监听器
*/
-public abstract class AbstractAuthenticationSuccessEventHandler implements ApplicationListener
- * 获取到登录的authentication 对象
- *
- * @param authentication 登录对象
- * @param request 请求
- * @param response 响应
- */
- public abstract void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response);
}
diff --git a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories
index f2826c158..367df029e 100644
--- a/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories
+++ b/pigx-common/pigx-common-security/src/main/resources/META-INF/spring.factories
@@ -2,5 +2,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.security.component.GlobalExceptionHandlerResolver,\
com.pig4cloud.pigx.common.security.component.PigxTokenEnhancer,\
com.pig4cloud.pigx.common.security.component.PigxSecurityInnerAspect,\
- com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl
+ com.pig4cloud.pigx.common.security.listener.AuthenticationFailureEventListener,\
+ com.pig4cloud.pigx.common.security.listener.AuthenticationSuccessEventListener
diff --git a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java
index bdbabf41f..89d8e1f0e 100644
--- a/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java
+++ b/pigx-gateway/src/main/java/com/pig4cloud/pigx/gateway/filter/ValidateCodeGatewayFilter.java
@@ -34,6 +34,7 @@ import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
+import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
@@ -74,7 +75,8 @@ public class ValidateCodeGatewayFilter extends AbstractGatewayFilterFactory {
// 终端设置不校验, 直接向下执行
try {
- String[] clientInfos = WebUtils.getClientId(request);
+ String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
+ String[] clientInfos = WebUtils.getClientId(header);
if (filterIgnorePropertiesConfig.getClients().contains(clientInfos[0])) {
return chain.filter(exchange);
}
diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysRouteConf.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysRouteConf.java
index 0583648a5..9fb4e618c 100755
--- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysRouteConf.java
+++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/entity/SysRouteConf.java
@@ -92,5 +92,8 @@ public class SysRouteConf extends Model