✨ Introducing new features. 登录日志保存
This commit is contained in:
1 parent
f3bbb4295d
commit
cedd7eb91a
24 files changed
+239
-95
No files matched your search
@@ -48,6 +48,11 @@
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-upms-api</artifactId>
|
||||
</dependency>
|
||||
<!--log-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-log</artifactId>
|
||||
</dependency>
|
||||
<!--security-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
|
||||
+6
-40
@@ -20,35 +20,29 @@
|
||||
package com.pig4cloud.pigx.auth.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
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 com.pig4cloud.pigx.common.security.component.PigxWebResponseExceptionTranslator;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
|
||||
import org.springframework.security.oauth2.provider.ClientDetails;
|
||||
import org.springframework.security.oauth2.provider.ClientDetailsService;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
|
||||
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
|
||||
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/6/22
|
||||
@@ -58,17 +52,16 @@ import javax.sql.DataSource;
|
||||
@AllArgsConstructor
|
||||
@EnableAuthorizationServer
|
||||
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
|
||||
private final DataSource dataSource;
|
||||
private final TokenEnhancer pigxTokenEnhancer;
|
||||
private final PigxUserDetailsService pigxUserDetailsService;
|
||||
private final ClientDetailsService pigxClientDetailsServiceImpl;
|
||||
private final AuthenticationManager authenticationManagerBean;
|
||||
private final RedisConnectionFactory redisConnectionFactory;
|
||||
private final UserDetailsService pigxUserDetailsService;
|
||||
private final TokenEnhancer pigxTokenEnhancer;
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public void configure(ClientDetailsServiceConfigurer clients) {
|
||||
PigxClientDetailsService clientDetailsService = new PigxClientDetailsService(dataSource);
|
||||
clients.withClientDetails(clientDetailsService);
|
||||
clients.withClientDetails(pigxClientDetailsServiceImpl);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -106,30 +99,3 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/03/25
|
||||
* <p>
|
||||
* 扩展 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);
|
||||
}
|
||||
}
|
||||
+31
-4
@@ -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;
|
||||
|
||||
/**
|
||||
* 处理登录失败方法
|
||||
* 异步处理,登录失败方法
|
||||
* <p>
|
||||
*
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
+24
-3
@@ -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);
|
||||
}
|
||||
}
|
||||
+42
@@ -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
|
||||
* <p>
|
||||
* 扩展 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);
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -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;
|
||||
@@ -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
|
||||
|
||||
+1
-2
@@ -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信息为空");
|
||||
|
||||
@@ -49,6 +49,11 @@
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-common-security</artifactId>
|
||||
</dependency>
|
||||
<!--feign client-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-upms-api</artifactId>
|
||||
</dependency>
|
||||
<!--缓存依赖-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
+13
@@ -56,6 +56,19 @@ public class SysLogUtils {
|
||||
return sysLog;
|
||||
}
|
||||
|
||||
public SysLog getSysLog(HttpServletRequest request, String username) {
|
||||
SysLog sysLog = new SysLog();
|
||||
sysLog.setCreateBy(username);
|
||||
sysLog.setType(CommonConstants.STATUS_NORMAL);
|
||||
sysLog.setRemoteAddr(ServletUtil.getClientIP(request));
|
||||
sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
|
||||
sysLog.setMethod(request.getMethod());
|
||||
sysLog.setUserAgent(request.getHeader("user-agent"));
|
||||
sysLog.setParams(HttpUtil.toParams(request.getParameterMap()));
|
||||
sysLog.setServiceId(getClientId());
|
||||
return sysLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户端
|
||||
*
|
||||
|
||||
@@ -48,10 +48,10 @@
|
||||
<groupId>io.github.openfeign</groupId>
|
||||
<artifactId>feign-core</artifactId>
|
||||
</dependency>
|
||||
<!--UPMS API-->
|
||||
<!--aop-->
|
||||
<dependency>
|
||||
<groupId>com.pig4cloud</groupId>
|
||||
<artifactId>pigx-upms-api</artifactId>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+3
-3
@@ -18,7 +18,7 @@
|
||||
package com.pig4cloud.pigx.common.security.component;
|
||||
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.security.annotation.Inner;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -71,13 +71,13 @@ public class PermitAllUrlProperties implements InitializingBean {
|
||||
Inner method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Inner.class);
|
||||
Optional.ofNullable(method)
|
||||
.ifPresent(inner -> info.getPatternsCondition().getPatterns()
|
||||
.forEach(url -> ignoreUrls.add(ReUtil.replaceAll(url, PATTERN, StringPool.ASTERISK))));
|
||||
.forEach(url -> ignoreUrls.add(ReUtil.replaceAll(url, PATTERN, "*"))));
|
||||
|
||||
// 获取类上边的注解, 替代path variable 为 *
|
||||
Inner controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Inner.class);
|
||||
Optional.ofNullable(controller)
|
||||
.ifPresent(inner -> info.getPatternsCondition().getPatterns()
|
||||
.forEach(url -> ignoreUrls.add(ReUtil.replaceAll(url, PATTERN, StringPool.ASTERISK))));
|
||||
.forEach(url -> ignoreUrls.add(ReUtil.replaceAll(url, PATTERN, "*"))));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.pig4cloud.pigx.common.security.handler;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/03/25
|
||||
* token 发放失败处理
|
||||
*/
|
||||
public interface AuthenticationFailureHandler {
|
||||
|
||||
/**
|
||||
* 业务处理
|
||||
*
|
||||
* @param authenticationException 错误信息
|
||||
* @param authentication 认证信息
|
||||
* @param request 请求信息
|
||||
* @param response 响应信息
|
||||
*/
|
||||
void handle(AuthenticationException authenticationException, Authentication authentication, HttpServletRequest request, HttpServletResponse response);
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.pig4cloud.pigx.common.security.handler;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/03/25
|
||||
* token 发放成功处理
|
||||
*/
|
||||
public interface AuthenticationSuccessHandler {
|
||||
|
||||
/**
|
||||
* 业务处理
|
||||
*
|
||||
* @param authentication 认证信息
|
||||
* @param request 请求信息
|
||||
* @param response 响应信息
|
||||
*/
|
||||
void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response);
|
||||
}
|
||||
+14
-17
@@ -15,12 +15,16 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.security.handler;
|
||||
package com.pig4cloud.pigx.common.security.listener;
|
||||
|
||||
import com.pig4cloud.pigx.common.security.handler.AuthenticationFailureHandler;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
@@ -29,10 +33,12 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/10/8
|
||||
* 认证失败事件处理器
|
||||
* @date 2020/03/25
|
||||
* 认证失败事件监听器
|
||||
*/
|
||||
public abstract class AbstractAuthenticationFailureEventHandler implements ApplicationListener<AbstractAuthenticationFailureEvent> {
|
||||
public class AuthenticationFailureEventListener implements ApplicationListener<AbstractAuthenticationFailureEvent> {
|
||||
@Autowired(required = false)
|
||||
private AuthenticationFailureHandler failureHandler;
|
||||
|
||||
/**
|
||||
* Handle an application event.
|
||||
@@ -49,18 +55,9 @@ public abstract class AbstractAuthenticationFailureEventHandler implements Appli
|
||||
AuthenticationException authenticationException = event.getException();
|
||||
Authentication authentication = (Authentication) event.getSource();
|
||||
|
||||
handle(authenticationException, authentication, request, response);
|
||||
// 调用自定义业务实现
|
||||
if (failureHandler != null) {
|
||||
failureHandler.handle(authenticationException, authentication, request, response);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录失败方法
|
||||
* <p>
|
||||
*
|
||||
* @param authenticationException 登录的authentication 对象
|
||||
* @param authentication 登录的authenticationException 对象
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*/
|
||||
public abstract void handle(AuthenticationException authenticationException, Authentication authentication
|
||||
, HttpServletRequest request, HttpServletResponse response);
|
||||
}
|
||||
+13
-16
@@ -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<AuthenticationSuccessEvent> {
|
||||
public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
|
||||
@Autowired(required = false)
|
||||
private AuthenticationSuccessHandler successHandler;
|
||||
|
||||
/**
|
||||
* Handle an application event.
|
||||
*
|
||||
@@ -46,19 +53,9 @@ public abstract class AbstractAuthenticationSuccessEventHandler implements Appli
|
||||
HttpServletResponse response = requestAttributes.getResponse();
|
||||
|
||||
Authentication authentication = (Authentication) event.getSource();
|
||||
if (CollUtil.isNotEmpty(authentication.getAuthorities())) {
|
||||
handle(authentication, request, response);
|
||||
if (CollUtil.isNotEmpty(authentication.getAuthorities()) && successHandler != null) {
|
||||
successHandler.handle(authentication, request, response);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录成功方法
|
||||
* <p>
|
||||
* 获取到登录的authentication 对象
|
||||
*
|
||||
* @param authentication 登录对象
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*/
|
||||
public abstract void handle(Authentication authentication, HttpServletRequest request, HttpServletResponse response);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
+3
-1
@@ -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);
|
||||
}
|
||||
|
||||
+4
-1
@@ -92,5 +92,8 @@ public class SysRouteConf extends Model<SysRouteConf> {
|
||||
@TableLogic
|
||||
@ApiModelProperty(value = "删除标记,1:已删除,0:正常")
|
||||
private String delFlag;
|
||||
|
||||
/**
|
||||
* 租户ID
|
||||
*/
|
||||
private Integer tenantId;
|
||||
}
|
||||
+1
-1
@@ -73,7 +73,7 @@ public class DynamicRouteInitRunner {
|
||||
JSONArray predicateObj = JSONUtil.parseArray(route.getPredicates());
|
||||
vo.setPredicates(predicateObj.toList(PredicateDefinition.class));
|
||||
|
||||
log.info("加载路由ID:{},{}", route.getRouteId(), vo);
|
||||
log.info("加载租户 {},路由ID:{},{}", route.getTenantId(), route.getRouteId(), vo);
|
||||
redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class));
|
||||
redisTemplate.opsForHash().put(CacheConstants.ROUTE_KEY, route.getRouteId(), vo);
|
||||
});
|
||||
|
||||
+1
-1
@@ -84,7 +84,7 @@ public class SysDictController {
|
||||
* @return 同类型字典
|
||||
*/
|
||||
@GetMapping("/type/{type}")
|
||||
@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.size == 0")
|
||||
@Cacheable(value = CacheConstants.DICT_DETAILS, key = "#type", unless = "#result.data.size() == 0")
|
||||
public R getDictByType(@PathVariable String type) {
|
||||
return R.ok(sysDictItemService.list(Wrappers
|
||||
.<SysDictItem>query().lambda()
|
||||
|
||||
+1
-1
@@ -60,7 +60,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
|
||||
private final SysRoleMenuMapper sysRoleMenuMapper;
|
||||
|
||||
@Override
|
||||
@Cacheable(value = CacheConstants.MENU_DETAILS, key = "#roleId + '_menu'", unless = "#result.size() == 0")
|
||||
@Cacheable(value = CacheConstants.MENU_DETAILS, key = "#roleId + '_menu'")
|
||||
public List<MenuVO> findMenuByRoleId(Integer roleId) {
|
||||
return baseMapper.listMenusByRoleId(roleId);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+10
@@ -17,6 +17,11 @@
|
||||
|
||||
package com.pig4cloud.pigx.admin;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/10/7
|
||||
@@ -24,4 +29,9 @@ package com.pig4cloud.pigx.admin;
|
||||
*/
|
||||
public class PigxAdminApplicationTest {
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
ArrayList<String> stringList = new ArrayList<>();
|
||||
stringList.size();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user