Spring
Spring

Install

Add the dependency to the project configuration:

Maven

1
2
3
4
5
<dependency>
    <groupId>com.botbye</groupId>
    <artifactId>java-module</artifactId>
    <version>0.0.3</version>
</dependency>

or

Gradle

1
implementation("com.botbye:java-module:0.0.3")

Configuration

Create a configuration class using your server-key (available inside your Project):

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
public class AppConfig {

    @Bean
    public Botbye botbye() {
        BotbyeConfig config = new BotbyeConfig.Builder()
                .serverKey("00000000-0000-0000-0000-000000000000") // Use your project server-key 
                .build();

        return new Botbye(config);
    }
}

Usage

Add validateRequest in your controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@RestController
@RequestMapping("/api/demo")
public class DemoController {
    private final Botbye botbye;

    @Autowired
    public DemoController(Botbye botbye) {
        this.botbye = botbye;
    }

    // Get token from header or any place you store it.
    // For example in "x-botbye-token" header
    @PostMapping
    public ResponseEntity<Object> post(@RequestHeader(name = "x-botbye-token") String token) {
        HttpServletRequest request = (
                (ServletRequestAttributes) Objects.requireNonNull(
                        RequestContextHolder.getRequestAttributes()
                )
        ).getRequest();

        BotbyeResponse botbyeResponse = botbye.validateRequest(
                token,
                new ConnectionDetails(request.getServerPort(), request.getRemoteAddr(), request.getServerName(), request.getMethod(), request.getRequestURI()),
                extractHeaders(request)
        );

        if (!botbyeResponse.getResult().isAllowed()) {
            return ResponseEntity
                    .status(403)
                    .body(botbyeResponse.getError().getMessage());
        }

        return ResponseEntity.ok().body("hello world!");
    }

    private Headers extractHeaders(HttpServletRequest request) {
        Map<String, List<String>> headers = new HashMap<>();
        Enumeration<String> headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();

            List<String> valuesList = Collections.list(request.getHeaders(headerName));

            headers.put(headerName, valuesList);
        }

        return new Headers(headers);
    }
}

Sample of this code on GitHub

Settings

BotbyeConfig contains next configurable parameters:

Setting Description Required Default Value
botbyeEndpoint Host of the API Server no https://verify.botbye.com
serverKey Your BotBye server-key yes -
contentType Content type for API requests no application/json
readTimeout Read timeout for HTTP client no Duration.ofSeconds(2)
writeTimeout Write timeout for HTTP client no Duration.ofSeconds(2)
connectionTimeout Connection timeout for HTTP client no Duration.ofSeconds(2)
callTimeout Total call timeout no Duration.ofSeconds(5)
maxIdleConnections Max idle connections in the pool no 250
keepAliveDuration Keep-alive duration no Duration.ofSeconds(300)
maxRequestsPerHost Max requests per host no 1500
maxRequests Max requests total no 1500

Examples of BotBye API responses

Bot detected:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": false
  },
  "error": "Automation tool used"
}

Bot not detected:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": true
  },
  "error": null
}

Request banned by custom rule:

1
2
3
4
5
6
7
8
9
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": {
    "isAllowed": false
  },
  "error": {
    "message": "Banned by rule: MY_CUSTOM_RULE"
  }
}

Invalid server-key:

1
2
3
4
5
6
7
{
  "reqId": "f77b2abd-c5d7-44f0-be4f-174b04876583",
  "result": null,
  "error": {
    "message": "[BotBye] Bad Request: Invalid Server Key"
  }
}