fix: 日志入库

This commit is contained in:
wangyu 2021-01-17 10:44:47 +08:00
parent 950f337994
commit e805deb837
5 changed files with 51 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import com.flyfish.framework.domain.base.BaseQo;
import com.flyfish.framework.domain.base.NameLikeQo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.Sort;
import java.util.List;
@ -25,11 +26,18 @@ public class LogQo extends NameLikeQo<Log> {
private List<String> range;
private String type;
@Override
public CriteriaBuilder<Log> criteriaBuilder() {
return super.criteriaBuilder()
.with("operator", CriteriaBuilder.Builders.LIKE)
.with("module", "success")
.with("type", "module", "success")
.with("range", "startTime", CriteriaBuilder.Builders.DATE_RANGE);
}
@Override
public Sort sorts() {
return Sort.by(Sort.Order.desc("startTime"));
}
}

View File

@ -2,11 +2,12 @@ package com.flyfish.framework.logging.service;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.logging.config.LoggingTextRegistry;
import com.flyfish.framework.logging.domain.Log;
import com.flyfish.framework.logging.domain.LogType;
import com.flyfish.framework.service.AuthenticationLogger;
import com.flyfish.framework.service.UserFindService;
import com.flyfish.framework.utils.AuthenticationMessages;
import com.flyfish.framework.utils.MapBuilder;
import com.flyfish.framework.utils.UserUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
@ -15,7 +16,6 @@ import org.springframework.util.MultiValueMap;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
/**
@ -26,13 +26,12 @@ import java.util.Optional;
@Service
public class SimpleAuthenticationLogger implements AuthenticationLogger {
private final Map<String, String> uris = MapBuilder.<String, String>builder()
.put("login", "登录")
.put("logout", "注销")
.build();
@Resource
private LogService logService;
@Resource
private UserFindService userService;
@Resource
private LoggingTextRegistry registry;
/**
* 记录成功
@ -76,13 +75,13 @@ public class SimpleAuthenticationLogger implements AuthenticationLogger {
User raw = UserUtils.toUser(user);
Log log = new Log();
log.setType(LogType.AUTHENTICATION);
log.setSignature(uris.get(business));
log.setSignature(business);
log.setSuccess(true);
log.setBody("为了安全,本内容隐藏");
log.setModule("登录模块");
log.setBusiness(business);
log.setBusiness(registry.text(business));
log.setResponse(message);
log.setOperator(Optional.ofNullable(raw).map(Domain::getName).orElse(""));
log.setOperator(Optional.ofNullable(raw).map(Domain::getName).orElse("未知"));
log.setPeriod(0L);
log.setStartTime(new Date());
// 写入日志
@ -100,14 +99,14 @@ public class SimpleAuthenticationLogger implements AuthenticationLogger {
private void saveError(String business, String message, String username, String error) {
Log log = new Log();
log.setType(LogType.AUTHENTICATION);
log.setSignature(uris.get(business));
log.setSignature(business);
log.setSuccess(false);
log.setBody("某人尝试使用用户名'" + username + "',密码:******,进行登录");
log.setModule("登录模块");
log.setBusiness(business);
log.setBusiness(registry.text(business));
log.setError(error);
log.setResponse(message);
log.setOperator("无用户");
log.setOperator(userService.findByUsername(username).map(Domain::getName).orElse("未知"));
log.setPeriod(0L);
log.setStartTime(new Date());
// 写入日志

View File

@ -8,6 +8,9 @@ import com.flyfish.framework.service.impl.BaseServiceImpl;
import com.flyfish.framework.utils.Assert;
import com.flyfish.framework.utils.StrengthUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@ -16,7 +19,7 @@ import java.util.List;
import java.util.Optional;
@Service
public class UserService extends BaseServiceImpl<User> {
public class UserService extends BaseServiceImpl<User> implements UserFindService {
@Resource
private PasswordEncoder passwordEncoder;
@ -27,6 +30,7 @@ public class UserService extends BaseServiceImpl<User> {
* @param username 用户
* @return 结果
*/
@Override
public Optional<User> findByUsername(String username) {
return ((UserRepository) repository).findByUsername(username);
}
@ -61,4 +65,5 @@ public class UserService extends BaseServiceImpl<User> {
public List<User> getList(Qo<User> query) {
return super.getList(query);
}
}

View File

@ -31,6 +31,10 @@ public class JsonLogoutSuccessHandler implements ServerLogoutSuccessHandler {
ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
tokenProvider.removeToken(webFilterExchange.getExchange());
Object principal = authentication.getPrincipal();
if ("anonymous".equals(principal)) {
return response.writeWith(authenticationAuditor.transform(Result.ok()));
}
// 记录日志
return authenticationAuditor.logout((UserDetails) authentication.getPrincipal())
.then(response.writeWith(authenticationAuditor.transform(Result.ok())));

View File

@ -0,0 +1,20 @@
package com.flyfish.framework.service;
import com.flyfish.framework.domain.po.User;
import java.util.Optional;
/**
* 用户查找service
* @author wangyu
*/
public interface UserFindService {
/**
* 通过用户名查找
* @param username 用户名
* @return 结果
*/
Optional<User> findByUsername(String username);
}