spring-security-jwt

Configure JWT authentication and role-based authorization in Spring Boot applications.

Updated Apr 18, 2026
One-click install
npx skills add https://github.com/heiherilala-vawd/ompany-Management --skill spring-security-jwt-heiherilala-vawd
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: spring-security-jwt
Source: https://github.com/heiherilala-vawd/ompany-Management/tree/main/.agents/skills/spring-security-jwt
Command: npx skills add https://github.com/heiherilala-vawd/ompany-Management --skill spring-security-jwt-heiherilala-vawd

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Setting up stateless authentication in a Spring Boot REST API requires wiring together many components—JWT token generation, request filters, password encoding, and role-based access rules—which is error-prone when done from scratch. ## Core Features & Use Cases - Complete JWT Setup: Provides ready-to-adapt code for JwtUtils, JwtAuthenticationFilter, JwtAuthenticationEntryPoint, and SecurityConfiguration using jjwt 0.11.5 and BCrypt. - Role-Based Access Control: Implements UserDetails on the User entity, @EnableMethodSecurity, and an optional SelfMatcher so users can access their own resources. - Use Case: You are building a Spring Boot REST API with PostgreSQL and need login, register, and whoami endpoints secured with Bearer tokens, where only ADMIN users can delete entities. ## Quick Start Set up Spring Security with JWT authentication in my Spring Boot project, including login, register, and role-based endpoint protection.

Frequently Asked Questions about spring-security-jwt

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up JWT authentication in Spring Boot?

Add spring-boot-starter-security and jjwt dependencies, create a JwtUtils component to generate and validate HS256 tokens, and register a JwtAuthenticationFilter before UsernamePasswordAuthenticationFilter in your SecurityFilterChain. Configure stateless sessions and permit public endpoints like /auth/login.

How to implement role-based access control with Spring Security?

Make your User entity implement UserDetails and return authorities prefixed with ROLE_ from getAuthorities(). Enable @EnableMethodSecurity, then restrict endpoints in the SecurityFilterChain with hasRole("ADMIN") or use @PreAuthorize annotations on controller methods.

Does Spring Security JWT work with stateless REST APIs?

Yes, JWT authentication is designed for stateless APIs. Set SessionCreationPolicy.STATELESS in the SecurityFilterChain so no HTTP session is created, and authenticate each request via the Bearer token parsed by the JWT filter.

Why does my JWT filter return 401 Unauthorized?

A 401 occurs when the Authorization header is missing, malformed, or the token fails validation in JwtUtils. Check that the header starts with "Bearer ", the secret key matches, the token is not expired, and an AuthenticationEntryPoint is registered to handle the error.

How do I let users access only their own resources in Spring Security?

Implement a custom RequestMatcher like SelfMatcher that compares the authenticated user's ID from the SecurityContext with the ID extracted from the request URI. Register it in the SecurityFilterChain so matching requests are permitted for the resource owner.