Implementation:APIJSON APIJSON MethodAccess Annotation
| Knowledge Sources | |
|---|---|
| Domains | Security, Access_Control, Annotations |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Declarative Java annotation that specifies which user roles are allowed to access each HTTP method on a model class, provided by the APIJSON framework.
Description
The @MethodAccess annotation is a compile-time declarative access control mechanism. When placed on a model class, it declares the allowed roles for each HTTP method (GET, HEAD, GETS, HEADS, POST, PUT, DELETE). At startup, AbstractVerifier scans model classes for this annotation and populates SYSTEM_ACCESS_MAP.
Default values:
- GET() / HEAD(): All roles (UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN)
- GETS() / HEADS(): Authenticated roles (LOGIN, CONTACT, CIRCLE, OWNER, ADMIN)
- POST() / PUT() / DELETE(): Owner and Admin only (OWNER, ADMIN)
This is a Pattern Doc: the annotation is a user-defined interface applied to model classes. The actual enforcement is performed by AbstractVerifier.verifyAllowRole() which checks the populated SYSTEM_ACCESS_MAP.
Usage
Apply this annotation to model classes to declaratively specify access permissions. The annotation is retained at runtime (@Retention(RUNTIME)) and inherited by subclasses (@Inherited).
Code Reference
Source Location
- Repository: APIJSON
- File: src/main/java/apijson/MethodAccess.java
Signature
@Documented
@Retention(RUNTIME)
@Target(TYPE)
@Inherited
public @interface MethodAccess {
String[] GET() default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
String[] HEAD() default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
String[] GETS() default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
String[] HEADS() default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
String[] POST() default {OWNER, ADMIN};
String[] PUT() default {OWNER, ADMIN};
String[] DELETE() default {OWNER, ADMIN};
}
Import
import apijson.MethodAccess;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| GET | String[] | No | Allowed roles for GET method (default: all) |
| HEAD | String[] | No | Allowed roles for HEAD method (default: all) |
| GETS | String[] | No | Allowed roles for GETS method (default: LOGIN+) |
| HEADS | String[] | No | Allowed roles for HEADS method (default: LOGIN+) |
| POST | String[] | No | Allowed roles for POST method (default: OWNER, ADMIN) |
| PUT | String[] | No | Allowed roles for PUT method (default: OWNER, ADMIN) |
| DELETE | String[] | No | Allowed roles for DELETE method (default: OWNER, ADMIN) |
Outputs
| Name | Type | Description |
|---|---|---|
| SYSTEM_ACCESS_MAP | Map<String,Map<RequestMethod,String[]>> | Populated at startup from annotations |
Usage Examples
Restricting Access on a Model Class
import apijson.MethodAccess;
@MethodAccess(
GET = {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN}, // anyone can read
POST = {LOGIN, OWNER, ADMIN}, // logged-in users can create
PUT = {OWNER, ADMIN}, // only owner/admin can update
DELETE = {ADMIN} // only admin can delete
)
public class Moment {
private Long id;
private Long userId;
private String content;
// ... getters/setters
}
System Table Access (Built-in)
// AbstractVerifier initializes SYSTEM_ACCESS_MAP with built-in models:
// SYSTEM_ACCESS_MAP.put("Access", getAccessMap(Access.class.getAnnotation(MethodAccess.class)));
// SYSTEM_ACCESS_MAP.put("Function", getAccessMap(Function.class.getAnnotation(MethodAccess.class)));
// SYSTEM_ACCESS_MAP.put("Script", getAccessMap(Script.class.getAnnotation(MethodAccess.class)));
// SYSTEM_ACCESS_MAP.put("Request", getAccessMap(Request.class.getAnnotation(MethodAccess.class)));