Skip to content

Commit 3836c7e

Browse files
committed
请求日志打印
1 parent 8255925 commit 3836c7e

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

sample.env

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# server port
22
PORT=7837
3-
43
# server host
5-
HOST=127.0.0.1
4+
HOST=0.0.0.0
5+
6+
FILE_STORE_PATH=/app/config
67

78
# jwt secret, must set your owner : openssl rand -base64 32
89
# JWT_SECRET=283ert544trggger283ert544trggger283ert544trggger283ert544trggger283ert544trggger283ert544trggger
9-
# JWT_USERS=username1,username2,xxxx,hhhh
10-
FILE_STORE_PATH=/app/config
10+
# JWT_USERS=username1,username2,xxxx,hhhh

src/main/java/ElectermSync/App.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import io.jsonwebtoken.Jws;
66
import io.jsonwebtoken.JwtException;
77
import io.jsonwebtoken.Jwts;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810

911
import java.nio.charset.StandardCharsets;
1012
import java.util.Arrays;
@@ -13,6 +15,7 @@
1315
import static spark.Spark.*;
1416

1517
public class App {
18+
private static final Logger log = LoggerFactory.getLogger(App.class);
1619

1720
public static void main(String[] args) {
1821
Gson gson = new Gson();
@@ -21,7 +24,7 @@ public static void main(String[] args) {
2124
Config dotenv = new Config();
2225
String secretOri = dotenv.getValue("JWT_SECRET");
2326
byte[] bytesToEncode = secretOri.getBytes(StandardCharsets.UTF_8);
24-
27+
2528
// Encode the bytes using Base64
2629
String secret = Base64.getEncoder().encodeToString(bytesToEncode);
2730

@@ -36,20 +39,20 @@ public static void main(String[] args) {
3639
before("/api/sync", (request, response) -> {
3740
String authHeader = request.headers("Authorization");
3841
try {
39-
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
40-
throw new JwtException("Missing or invalid token");
41-
} else {
42-
String token = authHeader.substring(7);
43-
Jws<Claims> claimsJws = Jwts.parserBuilder().setSigningKey(secret).build().parseClaimsJws(token);
44-
String id = claimsJws.getBody().get("id").toString();
45-
boolean found = Arrays.stream(idArrStrings).anyMatch(element -> element.equals(id));
46-
if (!found) {
47-
throw new JwtException("Unauthorized access");
48-
}
49-
request.attribute("jwtId", id);
50-
}
42+
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
43+
throw new JwtException("Missing or invalid token");
44+
} else {
45+
String token = authHeader.substring(7);
46+
Jws<Claims> claimsJws = Jwts.parserBuilder().setSigningKey(secret).build().parseClaimsJws(token);
47+
String id = claimsJws.getBody().get("id").toString();
48+
boolean found = Arrays.stream(idArrStrings).anyMatch(element -> element.equals(id));
49+
if (!found) {
50+
throw new JwtException("Unauthorized access");
51+
}
52+
request.attribute("jwtId", id);
53+
}
5154
} catch (JwtException ex) {
52-
halt(401, "Unauthorized: " + ex.getMessage());
55+
halt(401, "Unauthorized: " + ex.getMessage());
5356
}
5457
});
5558

@@ -70,10 +73,11 @@ public static void main(String[] args) {
7073
response.status(r.statusCode);
7174
return r.message;
7275
});
73-
76+
7477
after((request, response) -> {
7578
response.type("application/json");
7679
response.header("Content-Encoding", "gzip");
80+
log.info(String.format("%s[%s] - [%s]", request.requestMethod(), request.url(), response.status()));
7781
});
7882
}
7983
}

src/main/resources/logback.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<configuration>
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
4+
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n</pattern>
5+
<charset>UTF-8</charset>
6+
</encoder>
7+
</appender>
8+
9+
<root level="info">
10+
<appender-ref ref="STDOUT" />
11+
</root>
12+
</configuration>

0 commit comments

Comments
 (0)