https://github.com/phuongbv00/jauth-utils
JAuth Utils is a Java library that simplifies JWT authentication wrap jjwt and supports Spring Boot
https://github.com/phuongbv00/jauth-utils
authentication java spring-boot
Last synced: 6 months ago
JSON representation
JAuth Utils is a Java library that simplifies JWT authentication wrap jjwt and supports Spring Boot
- Host: GitHub
- URL: https://github.com/phuongbv00/jauth-utils
- Owner: phuongbv00
- Created: 2021-05-29T20:56:53.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-08T12:14:26.000Z (almost 4 years ago)
- Last Synced: 2025-07-11T14:46:25.955Z (12 months ago)
- Topics: authentication, java, spring-boot
- Language: Java
- Homepage:
- Size: 56.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JAuth Utils [Deprecated]
**This library is deprecated, recommend using [JAuth Lib](https://github.com/censodev/jauth-lib)**
## 1. Introduction
JAuth Utils is a Java library that simplifies JWT authentication wrap [jjwt](https://github.com/jwtk/jjwt) and supports Spring Boot.
## 2. Prerequisites
* Java 8
* Maven 3
## 3. Installation
### Maven
```
io.github.censodev
jauth-utils
3.0.1
```
### Gradle
```
implementation 'io.github.censodev:jauth-utils:3.0.1'
```
## 4. Usages
### 4.1. Define Authenticatable entity
```java
class User implements Credential {
private String name = "name";
// Getters, setters ...
@Override
public String getSubject() {
return "subject";
}
@Override
public String getUsername() {
return "usn";
}
@Override
public List getAuthorities() {
// must use modifiable list
return new ArrayList<>(Arrays.asList("role_a", "role_b"));
}
}
```
### 4.2. Initiate TokenProvider
```java
// With default config
TokenProvider tokenProvider = new TokenProvider();
// With builder
TokenProvider tokenProvider = TokenProvider.builder()
.expireInMillisecond(3_600_000)
.refreshTokenExpireInMillisecond(86_400_000)
.secret("qwertyuiopasdfghjklzxcvbnm")
.build();
```
### 4.3. Generate tokens
```java
String accessToken = tokenProvider.generateAccessToken(new User());
String refreshToken = tokenProvider.generateRefreshToken(new User());
```
### 4.4. Get credential from token
```java
User u = tokenProvider.getCredential(token, User.class);
```
### 4.5. Validate token
```java
try {
tokenProvider.validateToken(token);
} catch (MalformedJwtException e) {
e.printStackTrace();
} catch (ExpiredJwtException e) {
e.printStackTrace();
} catch (UnsupportedJwtException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SignatureException e) {
e.printStackTrace();
}
```
### 4.6. Spring Boot filter
#### 4.6.1. Initiate filter
```java
SpringAuthenticationFilter filter = new SpringAuthenticationFilter<>(tokenProvider, User.class);
// with filter hook
AuthenticationFilterHook hook = new AuthenticationFilterHook() {
@Override
public void beforeValidate(TokenProvider tokenProvider, String token) {
// do something before validate token
}
@Override
public void afterValidateWell(Credential credential) {
// do something after validate token successfully
}
@Override
public void afterValidateFailed(JwtException ex) {
// do something after validate token failed
}
@Override
public void onError(Exception ex) {
// do something when unexpected errors occur
}
};
SpringAuthenticationFilter filter = new SpringAuthenticationFilter<>(tokenProvider, User.class, hook);
```
#### 4.6.2. Configure security bean
```java
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors()
.and()
.addFilterBefore(springAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers(
"/api/auth/**"
).permitAll()
.anyRequest().authenticated();
}
@Bean
public TokenProvider tokenProvider() {
return TokenProvider.builder()
.expireInMillisecond(3_600_000)
.refreshTokenExpireInMillisecond(86_400_000)
.secret("qwertyuiopasdfghjklzxcvbnm")
.build();
}
}
```
#### 4.6.3. Get credential from security context
```java
Optional u = Optional.ofNullable((User) SecurityContextHolder.getContext().getAuthentication().getCredential());
```