Jwt extraction via base controller
Skill kjuhwa/skills-hub/skills/backend/jwt-extraction-via-base-controller
Self-correcting knowledge corpus for Claude Code — 9 stable shape clusters, bias-correction pipeline baked into contribution flow. 47 papers, 45 techniques, 1.1k skills.
npx -y skills add kjuhwa/skills-hub --skill jwt-extraction-via-base-controllerAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Consolidate repeated `JwtTokenService.getXxxFromBearerToken(headerAuthorization)` calls across Spring controllers by moving the service and helper methods onto a shared `BaseController`. Subclass controllers call `getOrganizationId(h)` / `getLoginId(h)` / `getRoleIds(h)` directly.
SKILL.md
2.2 KB, 357 tokens by cl100k_base, as published. Nobody here has run it
JWT Claim Extraction via BaseController
Put JwtTokenService + protected helper methods (getOrganizationId, getLoginId, getRoleIds) on a shared BaseController. Concrete controllers extend and call helpers directly, eliminating per-controller field declarations and reducing Authorization header handling to one line per use.
When to use
- Multiple Spring controllers share the same bearer-token claim extraction.
- You don't yet need a full
@AuthenticationPrincipal/HandlerMethodArgumentResolverlayer. - Refactor target: repeated
jwtTokenService.getXxxFromBearerToken(headerAuthorization)calls.
Steps
- Introduce or reuse
abstract class BaseController. - Add
@Autowired protected JwtTokenService jwtTokenService;—protectedis intentional so subclasses see it directly while DI still works. - Add helpers:
protected String getOrganizationId(String h) { return jwtTokenService.getOrganizationIdFromBearerToken(h); } protected String getLoginId(String h) { return jwtTokenService.getLoginIdFromBearerToken(h); } protected List<String> getRoleIds(String h) { return jwtTokenService.getRoleIdsFromBearerToken(h); } - Subclass controllers: remove
private final JwtTokenService jwtTokenService, drop its import, replace calls with the helpers. - Skip for controllers that don't consume JWT (file upload, dev-only token minting).
Counter / Caveats
- Prefer
HandlerMethodArgumentResolver+@AuthPrincipalif you have many controllers or need strong test isolation. - Inheritance-based sharing couples controllers to
BaseController; keepBaseControllerthin (exception handling + these helpers only).