backend-auth-module-guideline
Skill: backend-auth-module-guideline
Purpose
Ensure authentication and authorization backend code is:
- secure
- layered correctly
- API-contract-safe
- consistent with module-based architecture
- aligned with planner and OpenAPI
Scope
Use this skill when the module is:
auth- or any module that includes:
- login
- register
- logout
- refresh token
- current user
- password change
- role / permission checks
- session / token management
Backend Module Path
Authentication-related backend code must still follow:
backend/src/main/java/com/yourapp/modules/<module_name>/
Standard module structure:
controller/service/service/impl/mapper/entity/dto/vo/convertor/
Do not create a separate architecture just because the module is auth.
Core Auth Flows
Typical auth module flows may include:
- register
- login
- logout
- refresh token
- current authenticated user
- password update/reset
- role / permission retrieval
Generate only the flows supported by:
- planner output
- OpenAPI contract
Do not invent extra business flows without support.
Security Rules
Password handling
- passwords must be hashed
- never store plain text passwords
- never return passwords or hashes in API responses
- password comparison must use secure verification logic
Token handling
- token generation must be centralized
- token validation must be centralized
- token refresh must follow the defined contract
- do not expose internal token claims/storage structure unless required
Error handling
- authentication failure handling must be explicit
- avoid leaking sensitive account details
- keep auth-related exceptions controlled and safe
Role / permission handling
- enforce role/permission checks according to planner/OpenAPI
- keep authorization logic explicit
- do not mix permission checks randomly into unrelated controller code
Controller Rules
Auth controllers must:
- expose auth endpoints only
- delegate all real logic to services
- return standard wrapped responses
Auth controllers must not:
- hash passwords directly
- validate tokens manually in controller logic
- embed business/security policy inline
Service Rules
Auth services must handle:
- registration business logic
- credential verification
- password hashing orchestration
- token issuance
- token refresh
- current user resolution
- role / permission business rules where needed
Keep auth logic centralized rather than scattered.
DTO / VO Rules
Use request DTOs and response VOs consistent with OpenAPI.
Typical request types:
LoginRequestRegisterRequestRefreshTokenRequestChangePasswordRequest
Typical response types:
AuthResponseCurrentUserResponse
If OpenAPI defines different names, follow OpenAPI exactly.
Entity Rules
Typical auth-related entities may include:
- user
- role
- permission
- refresh_token
- user_role
- role_permission
Only generate entities that are actually required by:
- planner
- DB schema
- OpenAPI
Do not overbuild a security model beyond the defined scope.
Mapper Rules
Use MyBatis Plus mappers for auth persistence as usual.
Common mapper responsibilities:
- find user by login identity
- find role/permission associations
- persist refresh/session/token records if schema requires
- update password or auth status fields
Do not put business logic into mappers.
Convertor Rules
Use convertors for:
- request -> entity mapping where applicable
- user/entity -> auth response mapping
- current user response mapping
Do not let services accumulate repetitive mapping noise.
Standard Success Response Rule
All successful auth endpoints must still follow:
{
"code": 0,
"message": "success",
"data": {}
}
Examples:
- login response payload inside
data - register response payload inside
data - current user payload inside
data
Sensitive Data Rule
Never expose:
- password
- password_hash
- internal credential secrets
- hidden token persistence details
- internal security-only flags unless explicitly required
Forbidden Patterns
- password hashing inside controller
- returning entity directly from auth endpoint
- plain text password storage
- token logic duplicated in many places
- random authorization checks spread without structure
- leaking sensitive auth internals in response payloads
Final Goal
Generated auth backend code must be:
- secure
- layered
- explicit
- maintainable
- aligned with OpenAPI and module structure
