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 MethodAccess Annotation

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

Overview

Concrete tool for declaring role-based CRUD permissions on model classes via Java annotations, provided by the APIJSON ORM framework.

Description

The @MethodAccess annotation is a declarative access control mechanism. Applied to model classes (e.g., User, Privacy, Verify), it specifies which roles are allowed to perform each HTTP method (GET, HEAD, GETS, HEADS, POST, PUT, DELETE) on that model's table. The APIJSONVerifier reads these annotations at runtime to enforce permissions.

This is a Pattern Doc — the annotation is a user-declared interface, not a library API with complex parameters. The pattern is: annotate a model class with @MethodAccess, listing allowed roles per method.

Key roles (ascending hierarchy):

  • UNKNOWN — Unauthenticated visitor
  • LOGIN — Authenticated user
  • CONTACT — In the subject's contact list
  • CIRCLE — In the subject's social circle
  • OWNER — Resource owner (user ID matches record)
  • ADMIN — System administrator

Usage

  • Securing a model: Add @MethodAccess to the model class with role arrays per method.
  • Restricting reads: Set GET = {} to disallow public reads; use GETS = {OWNER, ADMIN} for restricted reads.
  • Allowing public creation: Set POST = {UNKNOWN, ADMIN} to allow anyone (including unauthenticated) to create.
  • Admin-only deletion: Set DELETE = {ADMIN} to restrict deletion to admins.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/model/User.java:L30-34
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/model/Privacy.java:L35-41
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/model/Verify.java:L32-40

Signature

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAccess {
    String[] GET()    default {};
    String[] HEAD()   default {};
    String[] GETS()   default {};
    String[] HEADS()   default {};
    String[] POST()   default {};
    String[] PUT()    default {};
    String[] DELETE() default {};
}

Import

import apijson.MethodAccess;
import static apijson.orm.AbstractVerifier.ADMIN;
import static apijson.orm.AbstractVerifier.OWNER;
import static apijson.orm.AbstractVerifier.UNKNOWN;

I/O Contract

Inputs

Name Type Required Description
GET String[] No Roles allowed to GET (public read)
HEAD String[] No Roles allowed to HEAD (public count)
GETS String[] No Roles allowed to GETS (restricted read)
HEADS String[] No Roles allowed to HEADS (restricted count)
POST String[] No Roles allowed to POST (create)
PUT String[] No Roles allowed to PUT (update)
DELETE String[] No Roles allowed to DELETE (delete)

Outputs

Name Type Description
(permission map) Annotation metadata Consumed by APIJSONVerifier at runtime

Usage Examples

User Model (public profile)

@MethodAccess(
    POST = {UNKNOWN, ADMIN},   // Anyone and admins can create users
    DELETE = {ADMIN}           // Only admins can delete users
)
public class User extends BaseModel<Long, Date> implements Visitor<Long> {
    // sex, name, head, tag, pictureList, contactIdList
}

Privacy Model (sensitive data)

@MethodAccess(
    GET = {},                    // No public reads
    GETS = {OWNER, ADMIN},       // Only owner and admin can read
    POST = {UNKNOWN, ADMIN},     // Anyone and admins can create
    DELETE = {ADMIN}             // Only admins can delete
)
public class Privacy extends BaseModel<Long, Date> {
    // phone, email, password, payPassword, balance
}

Verify Model (auth codes, full hierarchy)

@MethodAccess(
    GET = {},
    HEAD = {},
    GETS = {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN},
    HEADS = {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN},
    POST = {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN},
    PUT = {ADMIN},
    DELETE = {ADMIN}
)
public class Verify extends BaseModel<Long, Date> {
    // phone, verify, type
}

Related Pages

Implements Principle

Requires Environment

Page Connections

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