Skip to content

Commit 6568190

Browse files
committed
add a code comment
1 parent 4384ea4 commit 6568190

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

blade-security/src/main/java/com/hellokaton/blade/security/csrf/CsrfMiddleware.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean before(RouteContext context) {
8080
if (null != csrfOptions.getErrorHandler()) {
8181
return csrfOptions.getErrorHandler().apply(context);
8282
} else {
83-
context.badRequest().text("CSRF token mismatch.");
83+
context.badRequest().text("CSRF token mismatch :(");
8484
return false;
8585
}
8686
}

blade-security/src/main/java/com/hellokaton/blade/security/csrf/CsrfOptions.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import java.util.function.Function;
1313

1414
/**
15-
* Csrf config
15+
* Csrf Options
1616
* <p>
17-
* Created by hellokaton on 11/07/2017.
17+
* Created by hellokaton on 2022/5/5
1818
*/
1919
@Getter
2020
@Setter
@@ -24,28 +24,69 @@ public class CsrfOptions {
2424
Arrays.asList(HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)
2525
);
2626

27+
/**
28+
* Enable csrf, default is enabled by default.
29+
* <p>
30+
* after this function is disabled, the middleware does not take effect.
31+
*/
2732
private boolean enabled = true;
33+
34+
/**
35+
* The attribute name that puts csrf_token into the request context.
36+
* <p>
37+
* you can get this value from the template engine.
38+
*/
2839
private String attrKeyName = "_csrf_token";
40+
41+
/**
42+
* The header name that carries the token in the request header.
43+
*/
2944
private String headerKeyName = "X-CSRF-TOKEN";
45+
46+
/**
47+
* The form input name that carries the token in the request.
48+
*/
3049
private String formKeyName = "_csrf_token";
50+
51+
/**
52+
* To generate a token key, change the value.
53+
* <p>
54+
* the token is generated in JWT mode.
55+
*/
3156
private String secret = "UXOwbPd+P0u8YyBkQbuyXiv7UVc1JmMS061HUuaDRms=";
3257

33-
private Set<String> urlExclusions = new HashSet<>();
58+
/**
59+
* A list of urls to exclude, which will not be limited by the frequency of requests.
60+
* <p>
61+
* for example:
62+
* <p>
63+
* /notify/**
64+
* /upload/**
65+
* /admin/roles/**
66+
*/
67+
private Set<String> excludeURLs;
68+
69+
/**
70+
* For the following set of request methods, tokens will need to be validated.
71+
*/
3472
private Set<HttpMethod> verifyMethods = DEFAULT_VERIFY_METHODS;
3573

74+
/**
75+
* The processor that triggers the request frequency limit will, by default, prompt you for CSRF token mismatch.
76+
*/
3677
private Function<RouteContext, Boolean> errorHandler;
3778

3879
public static CsrfOptions create() {
3980
return new CsrfOptions();
4081
}
4182

4283
public CsrfOptions exclusion(@NonNull String... urls) {
43-
this.urlExclusions.addAll(Arrays.asList(urls));
84+
this.excludeURLs.addAll(Arrays.asList(urls));
4485
return this;
4586
}
4687

4788
public boolean isExclusion(@NonNull String url) {
48-
for (String excludeURL : this.urlExclusions) {
89+
for (String excludeURL : this.excludeURLs) {
4990
if (url.equals(excludeURL)) {
5091
return true;
5192
}

blade-security/src/main/java/com/hellokaton/blade/security/limit/LimitOptions.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,56 @@
66
import lombok.Setter;
77

88
import java.util.Set;
9-
import java.util.function.Consumer;
109
import java.util.function.Function;
1110

1211
@Getter
1312
@Setter
1413
public class LimitOptions {
1514

15+
/**
16+
* Enable request frequency limit, default is enabled by default.
17+
* <p>
18+
* after this function is disabled, the middleware does not take effect.
19+
*/
1620
private boolean enabled = true;
21+
22+
/**
23+
* To determine the uniqueness of a Request, pass in a Request object.
24+
* <p>
25+
* The default is the md5(remote_host+request_uri+request_method)
26+
*/
1727
private Function<Request, String> keyFunc;
28+
29+
/**
30+
* The processor that triggers the request frequency limit will, by default, prompt you for too many requests
31+
*/
1832
private Function<RouteContext, Boolean> limitHandler;
33+
34+
/**
35+
* Use expressions to control request frequency.
36+
* <p>
37+
* for example:
38+
* <p>
39+
* 5/s allow 5 requests per second
40+
* 5/1s allow 5 requests per second
41+
* 5/1m allow 60 requests per minute
42+
* 5/3s/warmup allow 5 requests in 3 seconds.
43+
* after startup, there is a warm-up period to gradually increase the distribution frequency to the configured rate.
44+
*/
1945
private String expression = "5/s";
46+
47+
/**
48+
* A list of urls to exclude, which will not be limited by the frequency of requests.
49+
* <p>
50+
* for example:
51+
* <p>
52+
* /notify/**
53+
* /upload/**
54+
* /admin/roles/**
55+
*/
2056
private Set<String> excludeURLs;
2157

22-
public static LimitOptions create(){
58+
public static LimitOptions create() {
2359
return new LimitOptions();
2460
}
2561

0 commit comments

Comments
 (0)