This project provides an out of the box working JWT integration into Spring security, using springs auto-configuration features.
- Add this library as a dependency to your build. Since this library is auto-configuration enabled, it will enable itself by default.
elder.security.jwt.realm [string] (optional)
Define the identity name of the realm / audience. Incoming JWT tokens must have a matching realm / audience claim to be accepted.
elder.security.jwt.publicKey.value [string] elder.security.jwt.publicKey.url [string]
Configure the Public Key (or the url to obtain the public key) used to verify incoming JWT Tokens. This property is required by the default RSAPublicKeyProvider
.
If desired, you may overwrite the RSAPublicKeyProvider
with Spring configuration and use your own logic to retrieve it.
You basically use the standard Spring security features.
- For simple cases you should use the standard Spring
@Secured("ROLE_USER"")
annotations. - For more complex cases, you can use the
Acw
utility to build logical expressions:
// Statically import the Acw utility
import static com.elderbyte.security.spring.local.auth.Acw.*;
public void mySecuredMethod(){
requireAll(hasRole(KnownRole.ROLE_OWNER), hasAnyRealm(Realm.Master, realmId)).enforce();
// Execute your critical code
}
While developing your secured Spring Application, the security can be quite an obstacle. If you enable mocking, the spring security context will get mocked for you:
elder.security.jwt:
enableMock: true
mockUsers:
- realm: mycompany
login: foo.bar
fullName: Foo Bar
roles: COOL_ADMIN, USER
JWT Claim | Description | Example |
---|---|---|
iss | The issuer of this token | http://my.auth.com |
aud | The intended audience | [master] |
exp | The expiration date | (date) |
realm | The security realm / tenant | master |
username | The user login name | foo.bar |
sub | The subject, a unique user id | 512342314 |
name | A display friendly name | Foo Bar |
roles | All granted security roles | [USER, FRONT_DESK] |
lang | Preferred user language | en_US |