Skip to content

Configuration Reference

CoSec uses Spring Boot's @ConfigurationProperties mechanism for type-safe configuration. All properties are prefixed with cosec. as defined by the CoSec.COSEC_PREFIX constant (cosec-api/src/main/kotlin/me/ahoo/cosec/api/CoSec.kt:22).

Configuration Hierarchy

The following diagram shows the configuration structure and the relationships between property groups:

mermaid
flowchart TD
    ROOT["cosec.*"] --> MAIN["cosec.enabled"]
    ROOT --> JWT["cosec.jwt.*"]
    ROOT --> AUTHN["cosec.authentication.*"]
    ROOT --> AUTHZ["cosec.authorization.*"]
    ROOT --> IP2R["cosec.ip2region.*"]
    ROOT --> OPENAPI["cosec.openapi.*"]
    ROOT --> SOCIAL["cosec.authentication.social.*"]
    ROOT --> INJECT["cosec.inject.*"]

    AUTHZ --> LP["authorization.local-policy.*"]
    AUTHZ --> CACHE["authorization.cache.*"]
    AUTHZ --> GW["authorization.gateway.*"]

    JWT --> TV["jwt.token-validity.*"]
    CACHE --> CP["cache.policy.*"]
    CACHE --> CR["cache.role.*"]

    style ROOT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style MAIN fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style JWT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style AUTHN fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style AUTHZ fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style IP2R fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style OPENAPI fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style SOCIAL fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style INJECT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style LP fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CACHE fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style GW fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style TV fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CP fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CR fill:#2d333b,stroke:#6d5dfc,color:#e6edf3

Core Properties

cosec.enabled

Master switch for the entire CoSec framework. When set to false, all auto-configuration is skipped.

PropertyTypeDefault
cosec.enabledBooleantrue

Defined in CoSecProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/CoSecProperties.kt:31).

JWT Properties (cosec.jwt.*)

Controls JWT token creation and verification.

PropertyTypeDefaultDescription
cosec.jwt.enabledBooleantrueEnable JWT authentication
cosec.jwt.algorithmEnumhmac256Signing algorithm: hmac256, hmac384, hmac512
cosec.jwt.secretStringrequiredSecret key for HMAC signing
cosec.jwt.token-validity.accessDurationPT10MAccess token time-to-live (10 minutes)
cosec.jwt.token-validity.refreshDurationP7DRefresh token time-to-live (7 days)
cosec.jwt.token-revocation.enabledBooleanfalseEnable token revocation for logout; revoked tokens are tracked in the authorization token cache

Defined in JwtProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/jwt/JwtProperties.kt:28). Conditional activation is controlled by @ConditionalOnJwtEnabled (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/jwt/ConditionalOnJwtEnabled.kt).

Authentication Properties (cosec.authentication.*)

PropertyTypeDefaultDescription
cosec.authentication.enabledBooleantrueEnable authentication

Defined in AuthenticationProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/authentication/AuthenticationProperties.kt:26).

Authorization Properties (cosec.authorization.*)

Controls the authorization engine and policy loading behavior.

PropertyTypeDefaultDescription
cosec.authorization.enabledBooleantrueEnable authorization
cosec.authorization.local-policy.enabledBooleanfalseLoad policies from local JSON files
cosec.authorization.local-policy.locationsSet<String>classpath:cosec-policy/*-policy.jsonGlob patterns for policy file locations
cosec.authorization.local-policy.init-repositoryBooleanfalseInitialize the policy repository with local files on startup
cosec.authorization.local-policy.force-refreshBooleanfalseForce refresh of local policies on startup
cosec.authorization.remote-ip.max-trusted-indexInt1X-Forwarded-For trust depth: 0 ignores the header entirely (directly exposed deployments); N trusts N proxy hops and takes the rightmost entry appended by the closest trusted proxy

Defined in AuthorizationProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/authorization/AuthorizationProperties.kt:27).

Authorization Cache Properties (cosec.authorization.cache.*)

Controls Redis-based caching for policies and role permissions via CoCache.

PropertyTypeDefaultDescription
cosec.authorization.cache.enabledBooleantrueEnable caching
cosec.authorization.cache.key-prefixStringcosecRedis key prefix
cosec.authorization.cache.policy.initialCapacityIntunsetGuava cache initial capacity (policy cache)
cosec.authorization.cache.policy.concurrencyLevelIntunsetGuava cache concurrency level (policy cache)
cosec.authorization.cache.policy.maximumSizeLongunsetGuava cache maximum size (policy cache)
cosec.authorization.cache.policy.expireUnitTimeUnitSECONDSTime unit for expireAfterWrite/expireAfterAccess (policy cache)
cosec.authorization.cache.policy.expireAfterWriteLongunsetExpire after write (policy cache)
cosec.authorization.cache.policy.expireAfterAccessLongunsetExpire after access (policy cache)
cosec.authorization.cache.role.initialCapacityIntunsetGuava cache initial capacity (role cache)
cosec.authorization.cache.role.concurrencyLevelIntunsetGuava cache concurrency level (role cache)
cosec.authorization.cache.role.maximumSizeLongunsetGuava cache maximum size (role cache)
cosec.authorization.cache.role.expireUnitTimeUnitSECONDSTime unit for expireAfterWrite/expireAfterAccess (role cache)
cosec.authorization.cache.role.expireAfterWriteLongunsetExpire after write (role cache)
cosec.authorization.cache.role.expireAfterAccessLongunsetExpire after access (role cache)
cosec.authorization.cache.token.initialCapacityIntunsetGuava cache initial capacity (revoked-token cache)
cosec.authorization.cache.token.concurrencyLevelIntunsetGuava cache concurrency level (revoked-token cache)
cosec.authorization.cache.token.maximumSizeLong100000Guava cache maximum size (revoked-token cache)
cosec.authorization.cache.token.expireUnitTimeUnitSECONDSTime unit for expireAfterWrite/expireAfterAccess (revoked-token cache)
cosec.authorization.cache.token.expireAfterWriteLong30Expire after write (revoked-token cache); bounds cross-instance revocation propagation
cosec.authorization.cache.token.expireAfterAccessLongunsetExpire after access (revoked-token cache)

The derived Redis key prefix for revoked tokens is cosec:token:revoked: (i.e. ${key-prefix}:token:revoked:). It backs token revocation (cosec.jwt.token-revocation.enabled) for logout.

Defined in CacheProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/authorization/cache/CacheProperties.kt:34).

Redis Rate Limiter Properties (cosec.limiter.*)

Controls the Redis-backed distributed rate limiter matchers (redisRateLimiter, redisGroupedRateLimiter).

PropertyTypeDefaultDescription
cosec.limiter.enabledBooleantrueEnable the Redis rate limiter matcher factories (registered only when a StringRedisTemplate bean exists)
cosec.limiter.key-prefixStringcosec:rate-limiterRedis key prefix for limiter counters; must be unique per application — identical prefixes on one Redis share a single quota

Defined in LimiterProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/authorization/limiter/LimiterProperties.kt:26).

Audit Log (cosec.audit.*)

Controls audit logging of authorization decisions.

PropertyTypeDefaultDescription
cosec.audit.enabledBooleantrueWhether to audit authorization decisions.
cosec.audit.kafka.enabledBooleantrueUse Kafka instead of the logging sink when kafka-support is present.
cosec.audit.kafka.topicStringcosec-auditKafka topic for audit events.

Defined in AuditProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/audit/AuditProperties.kt:26).

Every authorization decision is published as a structured AuditEvent to the AuditEventSink SPI. The default LoggingAuditEventSink writes single-line JSON to the logger me.ahoo.cosec.audit — denials at WARN, unexpected errors at ERROR, allows at DEBUG (so the default INFO level only surfaces denials). Set logging.level.me.ahoo.cosec.audit=DEBUG for full auditing, or register your own AuditEventSink bean to ship events to Kafka/Elasticsearch/your database. If you replace the default Authorization bean with your own, audit wiring steps aside automatically (the decorator only wraps the auto-configured cosecAuthorization bean).

The kafka-support feature replaces the logging sink with an asynchronous KafkaAuditEventSink. Each record uses a JSON [tenantId, principalId] tuple as its key and a single-line AuditEvent JSON value. Configure the broker with spring.kafka.*; CoSec does not create the topic. To keep authorization non-blocking during an outage, the sink queues at most 1024 events and drops new events when that queue is full, logging once until queue processing resumes. During shutdown it waits up to 10 seconds for accepted events to drain before forcing the scheduler closed.

Gateway Properties (cosec.authorization.gateway.*)

PropertyTypeDefaultDescription
cosec.authorization.gateway.enabledBooleantrueEnable Spring Cloud Gateway integration

Defined in GatewayProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/authorization/gateway/GatewayProperties.kt:26).

IP2Region Properties (cosec.ip2region.*)

Controls IP geolocation for region-based access control.

PropertyTypeDefaultDescription
cosec.ip2region.enabledBooleantrueEnable IP geolocation

Defined in Ip2RegionProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/ip2region/Ip2RegionProperties.kt:26).

OpenAPI Properties (cosec.openapi.*)

Controls Swagger/OpenAPI integration and policy generation endpoints.

PropertyTypeDefaultDescription
cosec.openapi.enabledBooleantrueEnable OpenAPI integration

Defined in OpenAPIProperties (cosec-spring-boot-starter/src/main/kotlin/me/ahoo/cosec/spring/boot/starter/openapi/OpenAPIProperties.kt:26).

Auto-Configuration Activation Flow

The following diagram shows how CoSec auto-configuration activates based on properties:

mermaid
sequenceDiagram
    autonumber
    participant SB as Spring Boot
    participant CA as CoSecAutoConfiguration
    participant JA as CoSecJwtAutoConfiguration
    participant AA as CoSecAuthenticationAutoConfiguration
    participant AZ as CoSecAuthorizationAutoConfiguration
    participant MA as MatcherFactoryRegister

    SB->>CA: Check @ConditionalOnCoSecEnabled
    Note over CA: cosec.enabled = true
    CA->>CA: Register CoSecModule
    CA->>MA: Register SPI Matchers

    SB->>JA: Check @ConditionalOnJwtEnabled
    Note over JA: cosec.jwt.enabled = true
    JA->>JA: Configure JWT Algorithm, Secret, TokenValidity

    SB->>AA: Check @ConditionalOnAuthenticationEnabled
    Note over AA: cosec.authentication.enabled = true
    AA->>AA: Register Authentication Providers

    SB->>AZ: Check @ConditionalOnAuthorizationEnabled
    Note over AZ: cosec.authorization.enabled = true
    AZ->>AZ: Register SimpleAuthorization
    AZ->>AZ: Load Local Policies (if enabled)

Example Application.yaml

yaml
spring:
  kafka:
    bootstrap-servers: "localhost:9092"

cosec:
  # Master switch
  enabled: true

  # JWT Configuration
  jwt:
    enabled: true
    algorithm: hmac256           # hmac256 | hmac384 | hmac512
    secret: "my-super-secret-key-at-least-256-bits-long"
    token-validity:
      access: PT30M              # 30 minutes
      refresh: P14D              # 14 days

  # Authentication
  authentication:
    enabled: true

  # Authorization
  authorization:
    enabled: true
    local-policy:
      enabled: true
      locations:
        - "classpath:cosec-policy/*-policy.json"
      init-repository: true
      force-refresh: false

    # Redis Caching
    cache:
      enabled: true
      key-prefix: "cosec"
      policy:
        maximumSize: 1000
        expireAfterWrite: 300    # 5 minutes
      role:
        maximumSize: 500
        expireAfterWrite: 300

    # Spring Cloud Gateway
    gateway:
      enabled: true

  # Audit
  audit:
    enabled: true
    kafka:
      enabled: true
      topic: "cosec-audit"

  # IP Geolocation
  ip2region:
    enabled: true

  # OpenAPI
  openapi:
    enabled: true

Feature Variants

The cosec-spring-boot-starter module exposes Gradle feature variants that determine which integration modules are included:

mermaid
flowchart LR
    STARTER["cosec-spring-boot-starter"] --> W["webmvc-support"]
    STARTER --> WF["webflux-support"]
    STARTER --> GW["gateway-support"]
    STARTER --> OA["oauth-support"]
    STARTER --> CS["cache-support"]
    STARTER --> IP["ip2region-support"]
    STARTER --> OT["opentelemetry-support"]
    STARTER --> OAPI["openapi-support"]
    STARTER --> K["kafka-support"]

    style STARTER fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style W fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style WF fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style GW fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style OA fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style CS fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style IP fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style OT fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style OAPI fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
    style K fill:#2d333b,stroke:#6d5dfc,color:#e6edf3
Feature VariantIncluded ModuleRequired Dependency
webmvc-supportcosec-webmvcSpring WebMvc
webflux-supportcosec-webfluxSpring WebFlux
gateway-supportcosec-gatewaySpring Cloud Gateway
oauth-supportcosec-socialJustAuth
cache-supportcosec-cocacheSpring Data Redis + CoCache
ip2region-supportcosec-ip2regionip2region library
opentelemetry-supportcosec-opentelemetryOpenTelemetry
openapi-supportcosec-openapiSpringDoc OpenAPI
kafka-supportcosec-kafkaSpring for Apache Kafka

Feature variants are declared in build.gradle.kts (cosec-spring-boot-starter/build.gradle.kts:18).

References

Licensed under the Apache License, Version 2.0.