Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo DemoVerifier

From Leeroopedia
Knowledge Sources
Domains Security, Authorization, Validation
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for verifying request permissions and validating field formats provided by the APIJSON framework, using regex patterns and role-based access control.

Description

The DemoVerifier class extends APIJSONVerifier<Long> and serves as the security gatekeeper in the APIJSON pipeline. It inherits role-based access verification from the framework's AbstractVerifier and adds field-level regex validation via the COMPILE_MAP static map.

Key features:

  • Regex field validation: Registers patterns for PHONE, QQ, EMAIL, IDCARD, TEL fields in a static block. When a request contains these fields, the framework validates values against the patterns.
  • Update condition relaxation: Sets IS_UPDATE_MUST_HAVE_ID_CONDITION = false, allowing batch updates without requiring an ID condition (useful for administrative operations).
  • Role verification: Inherits verify() methods from APIJSONVerifier, which checks the user's role against @MethodAccess annotations or the Access table.

Usage

  • Automatic verification: Called by the APIJSON framework during request parsing — no explicit invocation needed.
  • Custom regex patterns: Add entries to COMPILE_MAP in the static block to register new field validators.
  • Get current user: Static methods getVisitor(session) and getVisitorId(session) retrieve the authenticated user from the session.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoVerifier.java

Signature

public class DemoVerifier extends APIJSONVerifier<Long> {
    public static final String TAG = "DemoVerifier";

    static {
        // Register regex patterns for field validation
        COMPILE_MAP.put("PHONE", Pattern.compile(
            "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$"
        ));
        COMPILE_MAP.put("QQ", Pattern.compile("[1-9][0-9]{4,}"));
        COMPILE_MAP.put("EMAIL", Pattern.compile(
            "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"
        ));
        COMPILE_MAP.put("IDCARD", Pattern.compile(
            "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)"
        ));
        COMPILE_MAP.put("TEL", Pattern.compile(
            "(^\\(\\d{3,4}-)|\\d{3,4}-\\)?\\d{7,8}$"
        ));

        IS_UPDATE_MUST_HAVE_ID_CONDITION = false;
    }

    // Inherited from APIJSONVerifier<Long>:
    // public void verify(...) throws IllegalAccessException;
    // public static Visitor<Long> getVisitor(HttpSession session);
    // public static Long getVisitorId(HttpSession session);
}

Import

import apijson.demo.DemoVerifier;

I/O Contract

Inputs

Name Type Required Description
session HttpSession Yes User session containing role and visitor info
method RequestMethod Yes HTTP method being verified
table String Yes Target table name
field values Various No Values for fields registered in COMPILE_MAP

Outputs

Name Type Description
(verification) void Passes silently if permitted; throws IllegalAccessException if denied
(validation) void Passes if regex matches; throws IllegalArgumentException if mismatch

Usage Examples

Adding a Custom Regex Pattern

public class DemoVerifier extends APIJSONVerifier<Long> {
    static {
        // Existing patterns...
        COMPILE_MAP.put("ZIPCODE", Pattern.compile("^\\d{6}$"));
        COMPILE_MAP.put("WECHAT", Pattern.compile("^[a-zA-Z][-_a-zA-Z0-9]{5,19}$"));
    }
}

Getting the Current User

// In a remote function
public Visitor<Long> getCurrentUser(@NotNull JSONObject curObj) {
    return DemoVerifier.getVisitor(getSession());
}

public Long getCurrentUserId(@NotNull JSONObject curObj) {
    return DemoVerifier.getVisitorId(getSession());
}

Strict Update Mode

// To require ID condition for updates (revert the default):
// In the static block, change:
IS_UPDATE_MUST_HAVE_ID_CONDITION = true;
// Now UPDATE requests must include an id condition

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment