2.4dev 修复一些错误和新增日志接口 (#217)

* 修复到处错误日志,请求路径不存在问题

(cherry picked from commit ccb7b4f908816f14f21ecef648321d722ac50fc7)

* 1.新增日志清空接口

* 1.修复ip2region 不会自动关闭连接问题

* 1.新增单点登录,多设备登录,以最后登录为准,之前的登录都会被踢掉,可以使用single.login =false 关闭
2.默认开启
pull/302/head
zoulejiu 2019-12-10 10:16:19 +08:00 committed by elunez
parent 1123f35349
commit a9e12b5ccd
7 changed files with 107 additions and 6 deletions

View File

@ -8,6 +8,7 @@ import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -143,12 +144,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
* ip
*/
public static String getCityInfo(String ip) {
DbSearcher searcher = null;
try {
String path = "ip2region/ip2region.db";
String name = "ip2region.db";
DbConfig config = new DbConfig();
File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getStream(), name);
DbSearcher searcher = new DbSearcher(config, file.getPath());
searcher = new DbSearcher(config, file.getPath());
Method method;
method = searcher.getClass().getMethod("btreeSearch", String.class);
DataBlock dataBlock;
@ -161,6 +163,14 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return address.equals(ElAdminConstant.REGION)?"内网IP":address;
} catch (Exception e) {
e.printStackTrace();
}finally {
if(searcher!=null){
try {
searcher.close();
} catch (IOException ignored) {
}
}
}
return "";
}
@ -185,4 +195,4 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
}
return weekDays[w];
}
}
}

View File

@ -3,9 +3,12 @@ package me.zhengjie.repository;
import me.zhengjie.domain.Log;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
/**
* @author Zheng Jie
* @date 2018-11-24
@ -21,4 +24,13 @@ public interface LogRepository extends JpaRepository<Log,Long>, JpaSpecification
*/
@Query(value = "select count(*) FROM (select request_ip FROM log where create_time between ?1 and ?2 GROUP BY request_ip) as s",nativeQuery = true)
Long findIp(String date1, String date2);
/**
*
* @param logType
*/
@Query(nativeQuery = true,value = "delete from log where log_type = ?1")
@Modifying
@Transactional
void deleteByLogType(String logType);
}

View File

@ -10,10 +10,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@ -38,9 +35,18 @@ public class LogController {
@GetMapping(value = "/download")
@PreAuthorize("@el.check()")
public void download(HttpServletResponse response, LogQueryCriteria criteria) throws IOException {
criteria.setLogType("INFO");
logService.download(logService.queryAll(criteria), response);
}
@Log("导出错误数据")
@ApiOperation("导出错误数据")
@GetMapping(value = "/error/download")
@PreAuthorize("@el.check()")
public void errorDownload(HttpServletResponse response, LogQueryCriteria criteria) throws IOException {
criteria.setLogType("ERROR");
logService.download(logService.queryAll(criteria), response);
}
@GetMapping
@ApiOperation("日志查询")
@PreAuthorize("@el.check()")
@ -71,4 +77,21 @@ public class LogController {
public ResponseEntity getErrorLogs(@PathVariable Long id){
return new ResponseEntity<>(logService.findByErrDetail(id), HttpStatus.OK);
}
@DeleteMapping(value = "/del/error")
@Log("删除所有ERROR日志")
@ApiOperation("删除所有ERROR日志")
@PreAuthorize("@el.check()")
public ResponseEntity delAllByError(){
logService.delAllByError();
return new ResponseEntity(HttpStatus.OK);
}
@DeleteMapping(value = "/del/info")
@Log("删除所有INFO日志")
@ApiOperation("删除所有INFO日志")
@PreAuthorize("@el.check()")
public ResponseEntity delAllByInfo(){
logService.delAllByInfo();
return new ResponseEntity(HttpStatus.OK);
}
}

View File

@ -64,4 +64,14 @@ public interface LogService {
* @throws IOException /
*/
void download(List<Log> logs, HttpServletResponse response) throws IOException;
/**
*
*/
void delAllByError();
/**
* INFO
*/
void delAllByInfo();
}

View File

@ -137,4 +137,16 @@ public class LogServiceImpl implements LogService {
}
FileUtil.downloadExcel(list, response);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delAllByError() {
logRepository.deleteByLogType("ERROR");
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delAllByInfo() {
logRepository.deleteByLogType("INFO");
}
}

View File

@ -48,6 +48,8 @@ public class AuthController {
private Long expiration;
@Value("${rsa.private_key}")
private String privateKey;
@Value("${single.login:true}")
private Boolean singleLogin;
private final SecurityProperties properties;
private final RedisUtils redisUtils;
private final UserDetailsService userDetailsService;
@ -97,6 +99,10 @@ public class AuthController {
put("token", properties.getTokenStartWith() + token);
put("user", jwtUser);
}};
if(singleLogin){
//踢掉之前已经登录的token
onlineUserService.checkLoginOnUser(authUser.getUsername(),token);
}
return ResponseEntity.ok(authInfo);
}

View File

@ -1,5 +1,6 @@
package me.zhengjie.modules.security.service;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.security.config.SecurityProperties;
import me.zhengjie.modules.security.security.vo.JwtUser;
import me.zhengjie.modules.security.security.vo.OnlineUser;
@ -16,6 +17,7 @@ import java.util.*;
* @Date 2019102621:56:27
*/
@Service
@Slf4j
public class OnlineUserService {
private final SecurityProperties properties;
@ -131,4 +133,30 @@ public class OnlineUserService {
public OnlineUser getOne(String key) {
return (OnlineUser)redisUtils.get(key);
}
/**
* 线
* @param userName
*/
public void checkLoginOnUser(String userName, String igoreToken){
List<OnlineUser> onlineUsers = getAll(userName);
if(onlineUsers ==null || onlineUsers.isEmpty()){
return;
}
for(OnlineUser onlineUser:onlineUsers){
if(onlineUser.getUserName().equals(userName)){
try {
String token =EncryptUtils.desDecrypt(onlineUser.getKey());
if(StringUtils.isNotBlank(igoreToken)&&!igoreToken.equals(token)){
this.kickOut(onlineUser.getKey());
}else if(StringUtils.isBlank(igoreToken)){
this.kickOut(onlineUser.getKey());
}
} catch (Exception e) {
log.error("checkUser is error",e);
}
}
}
}
}