Back to Skills

unit-test-security-authorization

Provides patterns for unit testing Spring Security with `@PreAuthorize`, `@Secured`, `@RolesAllowed`. Validates role-based access control and authorization policies. Use when testing security configurations and access control logic.

314stars37forksUpdated 7/31/2026

Security Assessment

Safe(95/100)
Security Score95/100

About unit-test-security-authorization

This skill provides patterns for unit testing Spring Security authorization logic, helping Java developers verify that role-based access control and expression-based authorization actually behave as intended. It solves a subtle and dangerous problem: security tests can silently pass when method security is misconfigured, giving false confidence that access controls are enforced. The skill focuses on testing @PreAuthorize, @PostAuthorize, @Secured, @RolesAllowed, and custom permission evaluators without needing a full Spring Security context.

The workflow covers adding the spring-security-test dependency, enabling method security in a test configuration with @EnableMethodSecurity, and writing tests with @WithMockUser and @WithUserDetails to simulate authenticated principals with specific roles. It demonstrates asserting both allowed paths (no exception thrown) and denied paths (AccessDeniedException), testing complex SpEL expressions that combine roles and principal properties, and unit-testing custom PermissionEvaluator implementations that check document ownership. A key defensive practice it teaches is adding an explicit assertion that unauthorized access is rejected, so a missing @EnableMethodSecurity annotation is caught rather than silently bypassing every check. Reference files provide basic testing patterns, advanced authorization scenarios, setup guidance, and complete examples with common SpEL expression recipes.

It targets Java and Spring developers and security-conscious teams who need fast, reliable authorization tests as part of their test suite. Typical use cases include validating RBAC on service methods, verifying access-denied scenarios, testing owner-based document permissions, and confirming that method-level security is genuinely active before shipping.

FAQ

What annotations does this skill help me test?

@PreAuthorize, @PostAuthorize, @Secured, and @RolesAllowed, along with custom PermissionEvaluator implementations and expression-based (SpEL) authorization rules.

What dependency is required?

spring-security-test added to your test scope, plus a test configuration annotated with @EnableMethodSecurity to activate method-level security.

How do I simulate different users in tests?

Use @WithMockUser to set roles and username, or @WithUserDetails with a custom UserDetailsService when tests need custom principal properties like an account id.

How do I avoid tests passing when security is not actually enforced?

The skill recommends adding an assertion that unauthorized access throws AccessDeniedException; if that passes unexpectedly, it usually means @EnableMethodSecurity is missing and checks are being bypassed silently.

All Files

5 files
references/advanced-authorization.md8.4 KB
View
SKILL.md6.6 KB
View
references/basic-testing.md5.5 KB
View
references/setup.md2.4 KB
View
references/complete-examples.md9.0 KB
View

Install unit-test-security-authorization

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill