Skip to content

Commit e80c0bd

Browse files
committed
Organize project
1 parent b13ee70 commit e80c0bd

21 files changed

+163
-176
lines changed

src/main/java/com/github/throyer/common/springboot/configurations/SpringSecurityConfiguration.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
package com.github.throyer.common.springboot.configurations;
22

3-
import static com.github.throyer.common.springboot.errors.ValidationHandlers.forbidden;
4-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.ACESSO_NEGADO_URL;
5-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.DAY_MILLISECONDS;
6-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.HOME_URL;
7-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.LOGIN_ERROR_URL;
8-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.LOGIN_URL;
9-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.LOGOUT_URL;
10-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.PASSWORD_PARAMETER;
11-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.SESSION_COOKIE_NAME;
12-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.STATIC_FILES;
13-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.USERNAME_PARAMETER;
14-
import static org.springframework.http.HttpMethod.GET;
15-
import static org.springframework.http.HttpMethod.POST;
16-
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
17-
183
import com.github.throyer.common.springboot.domain.session.service.SessionService;
194
import com.github.throyer.common.springboot.middlewares.AuthorizationMiddleware;
20-
215
import org.springframework.beans.factory.annotation.Autowired;
226
import org.springframework.beans.factory.annotation.Value;
237
import org.springframework.context.annotation.Bean;
@@ -36,6 +20,12 @@
3620
import org.springframework.stereotype.Component;
3721
import org.springframework.web.cors.CorsConfiguration;
3822

23+
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.*;
24+
import static com.github.throyer.common.springboot.utils.Responses.forbidden;
25+
import static org.springframework.http.HttpMethod.GET;
26+
import static org.springframework.http.HttpMethod.POST;
27+
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
28+
3929
@Component
4030
@EnableWebSecurity
4131
@EnableGlobalMethodSecurity(prePostEnabled = true)

src/main/java/com/github/throyer/common/springboot/controllers/app/RecoveryController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import com.github.throyer.common.springboot.domain.recovery.model.Codes;
99
import com.github.throyer.common.springboot.domain.recovery.model.RecoveryRequest;
1010
import com.github.throyer.common.springboot.domain.recovery.model.Update;
11-
import com.github.throyer.common.springboot.domain.shared.Type;
12-
import com.github.throyer.common.springboot.utils.Toasts;
11+
import com.github.throyer.common.springboot.domain.toast.Type;
12+
import com.github.throyer.common.springboot.domain.toast.Toasts;
1313

1414
import javax.validation.Valid;
1515

src/main/java/com/github/throyer/common/springboot/controllers/app/RegisterController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

3-
import static com.github.throyer.common.springboot.domain.shared.Type.SUCCESS;
3+
import static com.github.throyer.common.springboot.domain.toast.Type.SUCCESS;
44
import static com.github.throyer.common.springboot.utils.Responses.validateAndUpdateModel;
55

66
import com.github.throyer.common.springboot.domain.user.model.CreateUserProps;
77
import com.github.throyer.common.springboot.domain.user.service.CreateUserService;
8-
import com.github.throyer.common.springboot.utils.Toasts;
8+
import com.github.throyer.common.springboot.domain.toast.Toasts;
99

1010
import javax.validation.Valid;
1111

src/main/java/com/github/throyer/common/springboot/controllers/app/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

3-
import com.github.throyer.common.springboot.domain.shared.Type;
3+
import com.github.throyer.common.springboot.domain.toast.Type;
44
import com.github.throyer.common.springboot.domain.user.service.FindUserService;
55
import com.github.throyer.common.springboot.domain.user.service.RemoveUserService;
6-
import com.github.throyer.common.springboot.utils.Toasts;
6+
import com.github.throyer.common.springboot.domain.toast.Toasts;
77
import java.util.Optional;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.security.access.prepost.PreAuthorize;

src/main/java/com/github/throyer/common/springboot/domain/mail/exceptions/EmailNotUniqueException.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package com.github.throyer.common.springboot.domain.mail.exceptions;
22

3-
import com.github.throyer.common.springboot.domain.shared.SimpleError;
3+
import com.github.throyer.common.springboot.errors.Error;
44
import java.util.List;
55

66
public class EmailNotUniqueException extends RuntimeException {
77

88
private static final long serialVersionUID = 1L;
99

10-
private List<SimpleError> errors;
10+
private List<Error> errors;
1111

12-
public EmailNotUniqueException(List<SimpleError> errors) {
12+
public EmailNotUniqueException(List<Error> errors) {
1313
super();
1414
this.errors = errors;
1515
}
1616

17-
public EmailNotUniqueException(String message, List<SimpleError> errors) {
17+
public EmailNotUniqueException(String message, List<Error> errors) {
1818
super(message);
1919
this.errors = errors;
2020
}
2121

22-
public EmailNotUniqueException(String message, Throwable cause, List<SimpleError> errors) {
22+
public EmailNotUniqueException(String message, Throwable cause, List<Error> errors) {
2323
super(message, cause);
2424
this.errors = errors;
2525
}
2626

27-
public EmailNotUniqueException(Throwable cause, List<SimpleError> errors) {
27+
public EmailNotUniqueException(Throwable cause, List<Error> errors) {
2828
super(cause);
2929
this.errors = errors;
3030
}
3131

32-
public List<SimpleError> getErrors() {
32+
public List<Error> getErrors() {
3333
return errors;
3434
}
3535

src/main/java/com/github/throyer/common/springboot/domain/mail/exceptions/EmailValidations.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.springframework.validation.BindingResult;
1010
import org.springframework.validation.ObjectError;
1111
import com.github.throyer.common.springboot.domain.management.model.Addressable;
12-
import com.github.throyer.common.springboot.domain.shared.SimpleError;
12+
import com.github.throyer.common.springboot.errors.Error;
1313

1414
@Component
1515
public class EmailValidations {
@@ -19,7 +19,7 @@ public class EmailValidations {
1919
private static String FIELD = "email";
2020
private static String MESSAGE = "This email has already been used by another user. Please use a different email.";
2121

22-
private static final List<SimpleError> EMAIL_ERROR = List.of(new SimpleError(FIELD, MESSAGE));
22+
private static final List<Error> EMAIL_ERROR = List.of(new Error(FIELD, MESSAGE));
2323

2424
@Autowired
2525
public EmailValidations(UserRepository repository) {

src/main/java/com/github/throyer/common/springboot/domain/session/service/CreateTokenService.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import com.github.throyer.common.springboot.domain.session.repository.RefreshTokenRepository;
88
import com.github.throyer.common.springboot.domain.session.model.TokenRequest;
99
import com.github.throyer.common.springboot.domain.session.model.TokenResponse;
10-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.CREATE_SESSION_ERROR_MESSAGE;
1110

12-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.JWT;
11+
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.*;
1312
import static com.github.throyer.common.springboot.utils.Responses.forbidden;
1413

1514
import java.time.LocalDateTime;
@@ -20,21 +19,29 @@
2019

2120
@Service
2221
public class CreateTokenService {
23-
24-
@Value("${token.secret}")
25-
private String TOKEN_SECRET;
2622

27-
@Value("${token.expiration-in-hours}")
28-
private Integer TOKEN_EXPIRATION_IN_HOURS;
23+
private final String TOKEN_SECRET;
24+
private final Integer TOKEN_EXPIRATION_IN_HOURS;
25+
private final Integer REFRESH_TOKEN_EXPIRATION_IN_DAYS;
26+
27+
private final RefreshTokenRepository refreshTokenRepository;
28+
private final UserRepository userRepository;
2929

30-
@Value("${token.refresh.expiration-in-days}")
31-
private Integer REFRESH_TOKEN_EXPIRATION_IN_DAYS;
32-
33-
@Autowired
34-
private RefreshTokenRepository refreshTokenRepository;
3530
@Autowired
36-
private UserRepository userRepository;
37-
31+
public CreateTokenService(
32+
@Value(TOKEN_SECRET_ENV_PROPERTY) String tokenSecret,
33+
@Value(EXPIRATION_TOKEN_ENV_PROPERTY) Integer tokenExpirationInHours,
34+
@Value(REFRESH_TOKEN_ENV_PROPERTY) Integer refreshTokenExpirationInDays,
35+
RefreshTokenRepository refreshTokenRepository,
36+
UserRepository userRepository
37+
) {
38+
this.TOKEN_SECRET = tokenSecret;
39+
this.TOKEN_EXPIRATION_IN_HOURS = tokenExpirationInHours;
40+
this.REFRESH_TOKEN_EXPIRATION_IN_DAYS = refreshTokenExpirationInDays;
41+
this.refreshTokenRepository = refreshTokenRepository;
42+
this.userRepository = userRepository;
43+
}
44+
3845
public TokenResponse create(TokenRequest request) {
3946
var user = userRepository.findOptionalByEmailFetchRoles(request.getEmail())
4047
.filter(session -> session.validatePassword(request.getPassword()))

src/main/java/com/github/throyer/common/springboot/domain/session/service/JsonWebToken.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
import com.github.throyer.common.springboot.domain.session.model.Authorized;
1414

1515
import io.jsonwebtoken.Jwts;
16+
17+
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.ROLES_KEY_ON_JWT;
1618
import static io.jsonwebtoken.SignatureAlgorithm.HS256;
19+
import static java.time.ZoneId.systemDefault;
1720
import static java.util.Arrays.stream;
21+
import static java.util.Date.from;
1822

1923

2024
public class JsonWebToken {
21-
22-
public static final String ROLES_KEY_ON_JWT = "roles";
23-
2425
public String encode(User user, LocalDateTime expiration, String secret) {
25-
var roles = user.getRoles().stream().map(role -> role.getAuthority()).toList();
26+
var roles = user.getRoles().stream().map(Role::getAuthority).toList();
2627
return encode(user.getId(), roles, expiration, secret);
2728
}
2829

2930
public String encode(UserDetails user, LocalDateTime expiration, String secret) {
3031
return encode(user.getId(), user.getRoles(), expiration, secret);
3132
}
3233

33-
3434
public String encode(
3535
Long id,
3636
List<String> authorities,
@@ -39,12 +39,8 @@ public String encode(
3939
) {
4040
return Jwts.builder()
4141
.setSubject(id.toString())
42-
.claim(ROLES_KEY_ON_JWT, authorities
43-
.stream()
44-
.collect(Collectors.joining(",")))
45-
.setExpiration(Date.from(expiration
46-
.atZone(ZoneId.systemDefault())
47-
.toInstant()))
42+
.claim(ROLES_KEY_ON_JWT, String.join(",", authorities))
43+
.setExpiration(from(expiration.atZone(systemDefault()).toInstant()))
4844
.signWith(HS256, secret)
4945
.compact();
5046
}

src/main/java/com/github/throyer/common/springboot/domain/session/service/RefreshTokenService.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@
44
import com.github.throyer.common.springboot.domain.session.model.RefreshTokenRequest;
55
import com.github.throyer.common.springboot.domain.session.model.RefreshTokenResponse;
66
import com.github.throyer.common.springboot.domain.session.repository.RefreshTokenRepository;
7-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.JWT;
7+
8+
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.*;
89
import static com.github.throyer.common.springboot.utils.Responses.forbidden;
10+
import static java.time.LocalDateTime.now;
11+
912
import java.time.LocalDateTime;
1013
import org.springframework.beans.factory.annotation.Autowired;
1114
import org.springframework.beans.factory.annotation.Value;
1215
import org.springframework.stereotype.Service;
1316

1417
@Service
1518
public class RefreshTokenService {
19+
private final String TOKEN_SECRET;
20+
private final Integer TOKEN_EXPIRATION_IN_HOURS;
21+
private final Integer REFRESH_TOKEN_EXPIRATION_IN_DAYS;
1622

17-
public static final String REFRESH_SESSION_ERROR_MESSAGE = "Refresh token expirado ou inválido.";
18-
19-
@Value("${token.secret}")
20-
private String TOKEN_SECRET;
21-
22-
@Value("${token.expiration-in-hours}")
23-
private Integer TOKEN_EXPIRATION_IN_HOURS;
24-
25-
@Value("${token.refresh.expiration-in-days}")
26-
private Integer REFRESH_TOKEN_EXPIRATION_IN_DAYS;
23+
private final RefreshTokenRepository refreshTokenRepository;
2724

2825
@Autowired
29-
private RefreshTokenRepository refreshTokenRepository;
26+
public RefreshTokenService(
27+
@Value(TOKEN_SECRET_ENV_PROPERTY) String tokenSecret,
28+
@Value(EXPIRATION_TOKEN_ENV_PROPERTY) Integer tokenExpirationInHours,
29+
@Value(REFRESH_TOKEN_ENV_PROPERTY) Integer refreshTokenExpirationInDays,
30+
RefreshTokenRepository refreshTokenRepository
31+
) {
32+
this.TOKEN_SECRET = tokenSecret;
33+
this.TOKEN_EXPIRATION_IN_HOURS = tokenExpirationInHours;
34+
this.REFRESH_TOKEN_EXPIRATION_IN_DAYS = refreshTokenExpirationInDays;
35+
this.refreshTokenRepository = refreshTokenRepository;
36+
}
3037

3138
public RefreshTokenResponse refresh(RefreshTokenRequest request) {
3239
var old = refreshTokenRepository.findOptionalByCodeAndAvailableIsTrue(request.getRefresh())
33-
.filter(token -> token.nonExpired())
40+
.filter(RefreshToken::nonExpired)
3441
.orElseThrow(() -> forbidden(REFRESH_SESSION_ERROR_MESSAGE));
3542

36-
var now = LocalDateTime.now();
43+
var now = now();
3744
var expiresIn = now.plusHours(TOKEN_EXPIRATION_IN_HOURS);
3845
var token = JWT.encode(old.getUser(), expiresIn, TOKEN_SECRET);
3946

src/main/java/com/github/throyer/common/springboot/domain/session/service/SessionService.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.github.throyer.common.springboot.domain.user.repository.UserRepository;
44
import com.github.throyer.common.springboot.domain.session.model.Authorized;
5-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.JWT;
5+
6+
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.*;
67
import static java.util.Objects.nonNull;
78
import java.util.Optional;
89
import static java.util.Optional.empty;
910
import static java.util.Optional.of;
11+
import static java.util.logging.Level.WARNING;
12+
1013
import java.util.logging.Level;
1114
import java.util.logging.Logger;
1215
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,17 +22,18 @@
1922

2023
@Service
2124
public class SessionService implements UserDetailsService {
22-
23-
@Autowired
24-
UserRepository repository;
2525
private static final Logger LOGGER = Logger.getLogger(SessionService.class.getName());
26-
27-
public static final String INVALID_USERNAME = "Invalid user name.";
26+
27+
private final UserRepository repository;
2828

2929
private static String SECRET;
3030

31-
public SessionService(@Value("${token.secret}") String secret) {
31+
public SessionService(
32+
@Value(TOKEN_SECRET_ENV_PROPERTY) String secret,
33+
UserRepository repository
34+
) {
3235
SessionService.SECRET = secret;
36+
this.repository = repository;
3337
}
3438

3539
@Override
@@ -47,7 +51,7 @@ public static void authorize(String token) {
4751
.getContext()
4852
.setAuthentication(authorized.getAuthentication());
4953
} catch (Exception exception) {
50-
LOGGER.log(Level.WARNING, "Token expired or invalid");
54+
LOGGER.log(WARNING, TOKEN_EXPIRED_MESSAGE);
5155
}
5256
}
5357

0 commit comments

Comments
 (0)