-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add configuration for security and jwt validation
- Loading branch information
1 parent
4671bbe
commit c094429
Showing
14 changed files
with
277 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/online/buildit/commons/annotation/Factory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package online.buildit.commons.annotation; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.lang.annotation.Documented; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Component | ||
public @interface Factory { | ||
|
||
/** | ||
* Alias for component annotation. | ||
* @return default value | ||
*/ | ||
@AliasFor(annotation = Component.class) | ||
String value() default ""; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/online/buildit/commons/annotation/Handler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package online.buildit.commons.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Component | ||
public @interface Handler { | ||
|
||
/** | ||
* Alias for component annotation. | ||
* @return default value | ||
*/ | ||
@AliasFor(annotation = Component.class) | ||
String value() default ""; | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/online/buildit/commons/annotation/Provider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package online.buildit.commons.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Component | ||
public @interface Provider { | ||
|
||
/** | ||
* Alias for component annotation. | ||
* @return default value | ||
*/ | ||
@AliasFor(annotation = Component.class) | ||
String value() default ""; | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...ine/buildit/commons/enums/Characters.java → ...ldit/commons/locale/enums/Characters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...AcceptLanguageHeaderIsBlankException.java → ...AcceptLanguageHeaderIsBlankException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...mmons/filters/LocaleProcessingFilter.java → ...ocale/filters/LocaleProcessingFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...mmons/resolvers/CommonLocaleResolver.java → ...ocale/resolvers/CommonLocaleResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/online/buildit/commons/security/UserDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package online.buildit.commons.security; | ||
|
||
import java.util.UUID; | ||
|
||
public interface UserDetails extends org.springframework.security.core.userdetails.UserDetails { | ||
|
||
UUID getUUID(); | ||
String getEmail(); | ||
|
||
@Override | ||
default String getUsername() { | ||
return getEmail(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/online/buildit/commons/security/UserPrincipal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package online.buildit.commons.security; | ||
|
||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
public class UserPrincipal { | ||
|
||
private UUID uuid; | ||
private String email; | ||
private String password; | ||
|
||
private Collection<? extends GrantedAuthority> authorities; | ||
private Map<String, Object> attributes; | ||
|
||
public static <USER extends UserDetails> UserPrincipal create(final USER user) { | ||
return create(user, Collections.emptyList()); | ||
} | ||
|
||
/** | ||
* Creating user principal object. | ||
* @param user model user | ||
* @param authorities user's roles | ||
* @return {@link UserPrincipal} | ||
*/ | ||
public static <USER extends UserDetails> UserPrincipal create(final USER user, | ||
final List<? extends GrantedAuthority> authorities) { | ||
return UserPrincipal.builder() | ||
.uuid(user.getUUID()) | ||
.email(user.getEmail()) | ||
.password(user.getPassword()) | ||
.authorities(authorities) | ||
.build(); | ||
} | ||
|
||
} | ||
|
12 changes: 12 additions & 0 deletions
12
src/main/java/online/buildit/commons/security/configuration/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package online.buildit.commons.security.configuration; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties | ||
@SpringBootApplication | ||
public class SecurityConfiguration { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/online/buildit/commons/security/factory/AuthPropertiesFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package online.buildit.commons.security.factory; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@Data | ||
@ConfigurationProperties(prefix = "app.security") | ||
public class AuthPropertiesFactory { | ||
|
||
private String tokenSecret; | ||
private long tokenExpirationMSec; | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/online/buildit/commons/security/providers/TokenProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package online.buildit.commons.security.providers; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.security.Keys; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import online.buildit.commons.annotation.Provider; | ||
import online.buildit.commons.security.UserPrincipal; | ||
import online.buildit.commons.security.factory.AuthPropertiesFactory; | ||
import org.springframework.security.core.Authentication; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.Key; | ||
import java.time.Clock; | ||
import java.time.ZonedDateTime; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
/** | ||
* JWT Token Generate. | ||
*/ | ||
@Slf4j | ||
@Provider | ||
@RequiredArgsConstructor | ||
public class TokenProvider { | ||
|
||
private final AuthPropertiesFactory authPropertiesFactory; | ||
|
||
/** | ||
* Generating JWT token. | ||
* @param authentication authentication's data | ||
* @return JWT | ||
*/ | ||
public String generate(final Authentication authentication) { | ||
final UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal(); | ||
|
||
return Jwts.builder() | ||
.setSubject(String.valueOf(userPrincipal.getUuid())) | ||
.setIssuedAt(Date.from(ZonedDateTime.now(Clock.systemUTC()).toInstant())) | ||
.setExpiration( | ||
new Date(Date.from(ZonedDateTime.now(Clock.systemUTC()).toInstant()).getTime() | ||
+ authPropertiesFactory.getTokenExpirationMSec()) | ||
) | ||
.signWith(getKey()) | ||
.compact(); | ||
} | ||
|
||
/** | ||
* Getting user uuid by token. | ||
* @param token token | ||
* @return user UUID | ||
*/ | ||
public UUID getUserIdFromToken(final String token) { | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(getKey()) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
|
||
return UUID.fromString(claims.getSubject()); | ||
} | ||
|
||
/** | ||
* JWT Token Validation. | ||
* @param authToken token | ||
* @return true/false | ||
*/ | ||
public boolean validateToken(final String authToken) { | ||
Jwts.parser().setSigningKey(getKey()).parseClaimsJws(authToken); | ||
return true; | ||
} | ||
|
||
private Key getKey() { | ||
return Keys.hmacShaKeyFor(authPropertiesFactory.getTokenSecret().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/online/buildit/commons/security/validators/TokenValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package online.buildit.commons.security.validators; | ||
|
||
import java.time.Clock; | ||
import java.time.ZonedDateTime; | ||
|
||
public class TokenValidator { | ||
|
||
public static boolean validate(final VerificationToken token, final VerificationType verificationType) { | ||
return verificationType.getName().equals(token.getName()) | ||
&& ZonedDateTime.now(Clock.systemUTC()).isBefore(token.getExpiresIn()); | ||
} | ||
} |