JAuth Lib is a set of Java modules that boots up your JWT-based authentication tasks faster.
Versions compatibility table:
JAuth Lib version | Spring Boot version | Spring Cloud version |
---|---|---|
0.0.0 | 2.6.7 | 2021.0.1 |
0.1.0 | 2.7.6 | 2021.0.5 |
Add the core dependency first:
<dependency>
<groupId>io.github.censodev</groupId>
<artifactId>jauth-lib-core</artifactId>
<version>0.0.0</version>
</dependency>
implementation 'io.github.censodev:jauth-lib-core:0.0.0'
If you are handling the authentication tasks with Spring Web, add the below dependency:
<dependency>
<groupId>io.github.censodev</groupId>
<artifactId>jauth-lib-spring-web</artifactId>
<version>0.0.0</version>
</dependency>
implementation 'io.github.censodev:jauth-lib-spring-web:0.0.0'
In opposite, if you are working with Spring Gateway, add dependency:
<dependency>
<groupId>io.github.censodev</groupId>
<artifactId>jauth-lib-spring-gateway</artifactId>
<version>0.0.0</version>
</dependency>
implementation 'io.github.censodev:jauth-lib-spring-gateway:0.0.0'
You need to create an entity implements the interface CanAuth
.
Go to example
At this step, you need to create a configuration with at least 2 beans:
TokenProvider
: Note that, thesecret
property is required and has no default value.SecurityFilterChain
: Create aSpringWebAuthFilter
orSpringGatewayAuthFilter
and register it beforeUsernamePasswordAuthenticationFilter
You can use the SecurityContextHolder
to get the token's credential:
class Test {
void getCredential() {
Optional<User> u = Optional.ofNullable((User) SecurityContextHolder.getContext().getAuthentication().getCredential());
}
}