This guide is for Spring Boot 3 and later (Spring MVC on Tomcat, Java 17 or later), with Korzen's package for Java, dev.korzen:bot-protection. It has no dependency of its own: a servlet filter on the JDK's HttpClient, one client for the process, which keeps the connection open. It asks /v1/decide, gives up at your timeout and lets the visitor through on any error.
Payment, login and sign-up routes: turn on .protectedAction(true), so a script that never loads your page cannot take the action. Protecting a checkout
The tag
Put the tag in the <head> of the layout your sign-up, sign-in and checkout pages use (with Thymeleaf, the fragment they share):
<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>
For visitors whose network blocks Korzen's host, .firstParty(true) serves the tag and its beacon from your own domain. First-party mode
The call
Behind Cloudflare? A Worker can do this without changing your app. Cloudflare Workers
Add the package to pom.xml (with Gradle, implementation("dev.korzen:bot-protection:3.0.0")):
<dependency> <groupId>dev.korzen</groupId> <artifactId>bot-protection</artifactId> <version>3.0.0</version> </dependency>
Register KorzenFilter on the paths of the actions. It asks before your controller runs, answers a block itself, and leaves the answer for the controller in KorzenFilter.decision(request), empty when there is none:
# src/main/resources/application.properties korzen.api-key=${KORZEN_API_KEY}
// src/main/java/com/example/KorzenConfig.java package com.example; import dev.korzen.botprotection.IpReader; import dev.korzen.botprotection.KorzenClient; import dev.korzen.botprotection.KorzenFilter; import java.time.Duration; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class KorzenConfig { @Bean FilterRegistrationBean<KorzenFilter> korzen(@Value("${korzen.api-key}") String apiKey) { KorzenClient client = KorzenClient.builder(apiKey) // one for the process: it keeps the connection open .url("https://eu.korzen.dev") // The timeout for your server's region: https://korzen.dev/bot-protection/docs/server-call#latency .timeout(Duration.ofMillis(200)) .build(); KorzenFilter filter = KorzenFilter.builder(client) .ip(IpReader.LAST_FORWARDED) // one proxy of yours in front: see "The visitor's IP" .protectedAction(true) // sign-up, login and payment: a script that never loaded your page holds no token; a page load spends none .onBlock((request, response, decision) -> { response.sendError(400, "We could not create your account."); // the form's own error return true; }) .build(); FilterRegistrationBean<KorzenFilter> r = new FilterRegistrationBean<>(filter); r.addUrlPatterns("/signup"); return r; } }
Then, in the controller that creates the account:
// src/main/java/com/example/SignupController.java package com.example; import dev.korzen.botprotection.Decision; import dev.korzen.botprotection.KorzenFilter; import jakarta.servlet.http.HttpServletRequest; import java.net.URI; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class SignupController { @PostMapping("/signup") public ResponseEntity<Void> signup(HttpServletRequest request, @RequestParam String email) { var user = accounts.create(email); // your own code user.setKorzenRequestId(KorzenFilter.decision(request).map(Decision::getRequestId).orElse(null)); // see "Apply the action" return ResponseEntity.status(HttpStatus.SEE_OTHER).location(URI.create("/welcome")).build(); } }
The filter also opens the client's connection when it starts, so the first visitor does not wait for the TLS handshake. To ask without it, client.decide(ServletRequests.fromRequest(request, IpReader.LAST_FORWARDED)) returns an Optional<Decision>, empty on any error; it never throws. A servlet container keeps no order of the request's headers, so none is sent.
The visitor's IP
Without .ip, the filter takes request.getRemoteAddr(), the connection's address, your proxy's if you have one. Behind one load balancer or nginx of yours, IpReader.LAST_FORWARDED takes the entry it added to X-Forwarded-For. Behind several proxies on a private network, set server.forward-headers-strategy=native instead: Tomcat's RemoteIpValve then takes the last entry of X-Forwarded-For that none of them added (10.x, 172.16–31.x, 192.168.x, loopback, and the addresses in server.tomcat.remoteip.internal-proxies), and .ip(r -> r.getRemoteAddr()) sends it. Behind Cloudflare, pass IpReader.CLOUDFLARE, which reads CF-Connecting-IP, if your server only takes traffic from Cloudflare.
Apply the action
The filter applies action: on block it answers 403, or what your onBlock writes, and the controller never runs. Make that answer the error the form shows when it fails for any other reason, so a bot learns nothing. The account starts in monitor mode, where action is always allow: nothing changes for visitors until you turn on block mode.
Keep the answer's request_id with what the action created, such as the account or the order: it names that decision when you ask us about it. The answer
Test it on your machine
Run ./mvnw spring-boot:run (or ./gradlew bootRun) and open it on localhost. Pages on localhost always accept the tag and are never listed as a domain. Locally the IP is 127.0.0.1 or 0:0:0:0:0:0:0:1, and that is fine.
- Set
KORZEN_API_KEYin the environment and open the sign-up page. In the browser's developer tools, the_kzcookie appears after a few seconds. - Sign up. The decision shows on the dashboard's Decisions page, under the path you sent.
- Check fail open: set a wrong key and sign up again. The call answers
401, and the sign-up goes through.
Common mistakes
- No
.ipbehind a load balancer. Every visitor then has the balancer's private IP, which Bot Protection takes for no IP: it sees much less, and the Set-up page warns you. - Reading the first entry of
X-Forwarded-Foryourself. The visitor writes it, so a bot picks its own IP. IpReader.LAST_FORWARDEDwithserver.forward-headers-strategy=nativeon. Tomcat's valve takes the entries it read out ofX-Forwarded-For, so the last one left is what the visitor wrote.- Calling
/v1/decidefrom the browser. The API key would reach every visitor; the call belongs on your server. - A line in your log that starts
korzen: the visitor's IP sent to Bot Protection. The client writes it once, at the first decision whose IP is your own network's (a load balancer's, a proxy's, a container's), which Bot Protection judges as no IP: setipto the visitor's address (the visitor's IP). In Docker on your machine it is the bridge's address, and harmless there.