mirror of
https://github.com/TheWhiteDog9487/WebAPI.git
synced 2026-08-11 23:01:30 +08:00
将访问日志写入SQLite数据库
This commit is contained in:
@@ -37,3 +37,4 @@ out/
|
|||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
API密钥.txt
|
API密钥.txt
|
||||||
|
SQLiteDataBase.db
|
||||||
+2
-2
@@ -7,7 +7,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "xyz.thewhitedog9487"
|
group = "xyz.thewhitedog9487"
|
||||||
version = "0.5.3"
|
version = "0.6.0"
|
||||||
|
|
||||||
java {
|
java {
|
||||||
toolchain {
|
toolchain {
|
||||||
@@ -34,7 +34,7 @@ dependencies {
|
|||||||
implementation("org.hibernate.orm:hibernate-community-dialects")
|
implementation("org.hibernate.orm:hibernate-community-dialects")
|
||||||
implementation("com.discord4j:discord4j-core:3.2.8")
|
implementation("com.discord4j:discord4j-core:3.2.8")
|
||||||
implementation("com.fasterxml.jackson.core:jackson-core:2.19.2")
|
implementation("com.fasterxml.jackson.core:jackson-core:2.19.2")
|
||||||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.9")
|
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.13")
|
||||||
implementation("org.springdoc:springdoc-openapi-starter-webmvc-api:2.8.9")
|
implementation("org.springdoc:springdoc-openapi-starter-webmvc-api:2.8.9")
|
||||||
implementation("org.springframework.boot:spring-boot-starter-actuator")
|
implementation("org.springframework.boot:spring-boot-starter-actuator")
|
||||||
compileOnly("org.projectlombok:lombok")
|
compileOnly("org.projectlombok:lombok")
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class OpenAPIConfiguration {
|
|||||||
.contact(new Contact()
|
.contact(new Contact()
|
||||||
.name("TheWhiteDog9487")
|
.name("TheWhiteDog9487")
|
||||||
.url("https://www.github.com/TheWhiteDog9487/WebAPI"))
|
.url("https://www.github.com/TheWhiteDog9487/WebAPI"))
|
||||||
.version("0.5.3")
|
.version("0.6.0")
|
||||||
.license(new License()
|
.license(new License()
|
||||||
.name("WTFPL")
|
.name("WTFPL")
|
||||||
.url("https://spdx.org/licenses/WTFPL")) )
|
.url("https://spdx.org/licenses/WTFPL")) )
|
||||||
|
|||||||
@@ -5,15 +5,21 @@ import jakarta.servlet.ServletException;
|
|||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
import xyz.thewhitedog9487.WebAPI.Data.Entity.AccessLog;
|
||||||
|
import xyz.thewhitedog9487.WebAPI.Data.Repository.AccessLogRepository;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class LogClientInfo extends OncePerRequestFilter {
|
public class LogClientInfo extends OncePerRequestFilter {
|
||||||
|
@Autowired AccessLogRepository AccessLogRepository;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||||
@@ -41,4 +47,23 @@ public class LogClientInfo extends OncePerRequestFilter {
|
|||||||
log.info("请求未携带User-Agent头部,无法获取客户端软件信息。"); }
|
log.info("请求未携带User-Agent头部,无法获取客户端软件信息。"); }
|
||||||
else{
|
else{
|
||||||
log.info("请求的User-Agent:{}", UserAgent); }
|
log.info("请求的User-Agent:{}", UserAgent); }
|
||||||
|
var Log = new AccessLog(
|
||||||
|
null,
|
||||||
|
request.getRequestId(),
|
||||||
|
request.getHeader("CF-Connecting-IP".toLowerCase() ),
|
||||||
|
request.getHeader("X-Forwarded-For".toLowerCase() ),
|
||||||
|
request.getRemoteAddr(),
|
||||||
|
request.getHeader("CF-IPCountry".toLowerCase() ),
|
||||||
|
( request.getHeader("CF-IPCountry".toLowerCase() ) == null ) ? null : Locale.of(Locale.PRC.getLanguage(), request.getHeader("CF-IPCountry".toLowerCase() ), Locale.SIMPLIFIED_CHINESE.getVariant()).getISO3Country(),
|
||||||
|
UserAgent,
|
||||||
|
request.getMethod(),
|
||||||
|
request.getRequestURL().toString(),
|
||||||
|
request.getQueryString(),
|
||||||
|
Collections.list( request.getHeaderNames() )
|
||||||
|
.stream()
|
||||||
|
.map( name -> name + ": " + Collections.list(request.getHeaders(name)) )
|
||||||
|
.reduce( ( a, b ) -> a + "\n" + b )
|
||||||
|
.orElse(""),
|
||||||
|
Instant.now() );
|
||||||
|
AccessLogRepository.save(Log);
|
||||||
filterChain.doFilter(request, response); } }
|
filterChain.doFilter(request, response); } }
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package xyz.thewhitedog9487.WebAPI.Data.Entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Entity
|
||||||
|
@Table
|
||||||
|
public class AccessLog{
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(nullable = false)
|
||||||
|
Long ID;
|
||||||
|
|
||||||
|
String RequestID;
|
||||||
|
@Column(name = "\"CF-Connecting-IP\"") String CF_Connecting_IP;
|
||||||
|
@Column(name = "\"X-Forwarded-For\"") String X_Forwarded_For;
|
||||||
|
String RemoteAddress;
|
||||||
|
@Column(name = "\"CF-IPCountry\"") String CF_IPCountry;
|
||||||
|
String ISO3166;
|
||||||
|
String UserAgent;
|
||||||
|
String HttpMethod;
|
||||||
|
String URL;
|
||||||
|
String QueryString;
|
||||||
|
@Column(columnDefinition = "text") String Header;
|
||||||
|
Instant Timestamp; }
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package xyz.thewhitedog9487.WebAPI.Data.Repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import xyz.thewhitedog9487.WebAPI.Data.Entity.AccessLog;
|
||||||
|
|
||||||
|
public interface AccessLogRepository extends JpaRepository<AccessLog, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,6 +4,22 @@ spring:
|
|||||||
threads:
|
threads:
|
||||||
virtual:
|
virtual:
|
||||||
enabled: true
|
enabled: true
|
||||||
|
jpa:
|
||||||
|
hibernate:
|
||||||
|
naming:
|
||||||
|
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||||
|
ddl-auto: update
|
||||||
|
database-platform: org.hibernate.community.dialect.SQLiteDialect
|
||||||
|
show-sql: true
|
||||||
|
properties:
|
||||||
|
hibernate:
|
||||||
|
format_sql: true
|
||||||
|
datasource:
|
||||||
|
driver-class-name: org.sqlite.JDBC
|
||||||
|
username:
|
||||||
|
password:
|
||||||
|
url: jdbc:sqlite:SQLiteDataBase.db
|
||||||
|
|
||||||
server:
|
server:
|
||||||
port: 36987
|
port: 36987
|
||||||
logging:
|
logging:
|
||||||
|
|||||||
Reference in New Issue
Block a user