Skip to content

Commit c9a777d

Browse files
committed
chore: constants package
1 parent 300252a commit c9a777d

30 files changed

+275
-270
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.springframework.stereotype.Component;
1919
import org.springframework.web.cors.CorsConfiguration;
2020

21-
import static com.github.throyer.common.springboot.utils.Constants.SECURITY.*;
21+
import static com.github.throyer.common.springboot.constants.SECURITY.*;
2222
import static com.github.throyer.common.springboot.utils.Responses.forbidden;
2323
import static org.springframework.http.HttpMethod.GET;
2424
import static org.springframework.http.HttpMethod.POST;
@@ -53,7 +53,7 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
5353

5454
@Override
5555
protected void configure(HttpSecurity http) throws Exception {
56-
PUBLIC_API_ROUTES.configure(http);
56+
PUBLIC_API_ROUTES.injectOn(http);
5757

5858
http
5959
.antMatcher("/api/**")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.throyer.common.springboot.constants;
2+
3+
public class MAIL {
4+
public static final Boolean CONTENT_IS_HTML = true;
5+
public static final String ERROR_SENDING_EMAIL_MESSAGE = "Error sending email.";
6+
public static final String EMAIL_SUCCESSFULLY_SENT_TO = "Email successfully sent to: {}";
7+
public static final String EMAIL_SENT_SUCCESSFULLY_MESSAGE_LOG_TEMPLATE = "email sent successfully to: %s";
8+
public static final String UNABLE_TO_SEND_EMAIL_MESSAGE_TEMPLATE = "Unable to send email to: %s";
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.throyer.common.springboot.constants;
2+
3+
/**
4+
* Validation messages.
5+
* @see "resources/messages.properties"
6+
*/
7+
public class MESSAGES {
8+
public static final String NOT_AUTHORIZED_TO_LIST = "not.authorized.list";
9+
public static final String NOT_AUTHORIZED_TO_READ = "not.authorized.read";
10+
public static final String NOT_AUTHORIZED_TO_CREATE = "not.authorized.create";
11+
public static final String NOT_AUTHORIZED_TO_MODIFY = "not.authorized.modify";
12+
13+
public static String EMAIL_ALREADY_USED_MESSAGE = "email.already-used.error.message";
14+
15+
public static final String TYPE_MISMATCH_ERROR_MESSAGE = "type.mismatch.message";
16+
public static final String TOKEN_EXPIRED_OR_INVALID = "token.expired-or-invalid";
17+
public static final String TOKEN_HEADER_MISSING_MESSAGE = "token.header.missing";
18+
public static final String INVALID_USERNAME = "invalid.username.error.message";
19+
20+
public static final String CREATE_SESSION_ERROR_MESSAGE = "create.session.error.message";
21+
public static final String REFRESH_SESSION_ERROR_MESSAGE = "refresh.session.error.message";
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.throyer.common.springboot.constants;
2+
3+
public class PASSWORD_RECOVERY {
4+
public static Integer MINUTES_TO_EXPIRE_RECOVERY_CODE = 20;
5+
public static final String SUBJECT_PASSWORD_RECOVERY_CODE = "Password recovery code";
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.throyer.common.springboot.constants;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class RATE_LIMIT {
9+
10+
@Autowired
11+
public RATE_LIMIT(
12+
@Value("${bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity}") Integer maxRequestsPerMinute
13+
) {
14+
RATE_LIMIT.MAX_REQUESTS_PER_MINUTE = maxRequestsPerMinute;
15+
}
16+
17+
public static Integer MAX_REQUESTS_PER_MINUTE;
18+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.throyer.common.springboot.constants;
2+
3+
import com.github.throyer.common.springboot.domain.session.service.JsonWebToken;
4+
import com.github.throyer.common.springboot.domain.shared.PublicRoutes;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
8+
import org.springframework.stereotype.Component;
9+
10+
import static com.github.throyer.common.springboot.domain.shared.PublicRoutes.create;
11+
import static org.springframework.http.HttpMethod.GET;
12+
import static org.springframework.http.HttpMethod.POST;
13+
14+
@Component
15+
public class SECURITY {
16+
17+
@Autowired
18+
public SECURITY(
19+
@Value("${token.secret}") String tokenSecret,
20+
@Value("${token.expiration-in-hours}") Integer tokenExpirationInHours,
21+
@Value("${token.refresh.expiration-in-days}") Integer refreshTokenExpirationInDays
22+
) {
23+
SECURITY.TOKEN_SECRET = tokenSecret;
24+
SECURITY.TOKEN_EXPIRATION_IN_HOURS = tokenExpirationInHours;
25+
SECURITY.REFRESH_TOKEN_EXPIRATION_IN_DAYS = refreshTokenExpirationInDays;
26+
}
27+
28+
public static final PublicRoutes PUBLIC_API_ROUTES = create()
29+
.add(GET, "/api", "/api/documentation/**")
30+
.add(POST, "/api/users", "/api/sessions/**", "/api/recoveries/**", "/api/documentation/**");
31+
32+
public static final Integer DAY_MILLISECONDS = 86400;
33+
public static final JsonWebToken JWT = new JsonWebToken();
34+
35+
public static final Integer PASSWORD_STRENGTH = 10;
36+
public static final BCryptPasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder(PASSWORD_STRENGTH);
37+
38+
public static final String ROLES_KEY_ON_JWT = "roles";
39+
40+
public static String TOKEN_SECRET;
41+
public static Integer TOKEN_EXPIRATION_IN_HOURS;
42+
public static Integer REFRESH_TOKEN_EXPIRATION_IN_DAYS;
43+
44+
public static final String USERNAME_PARAMETER = "email";
45+
public static final String PASSWORD_PARAMETER = "password";
46+
47+
public static final String HOME_URL = "/app";
48+
public static final String LOGIN_URL = "/app/login";
49+
public static final String LOGIN_ERROR_URL = LOGIN_URL + "?error=true";
50+
public static final String ACESSO_NEGADO_URL = LOGIN_URL + "?denied=true";
51+
public static final String LOGOUT_URL = "/app/logout";
52+
53+
public static final String SESSION_COOKIE_NAME = "JSESSIONID";
54+
55+
public static final String SECURITY_TYPE = "Bearer";
56+
public static final String AUTHORIZATION_HEADER = "Authorization";
57+
public static final String ACCEPTABLE_TOKEN_TYPE = SECURITY_TYPE + " ";
58+
public static final String CAN_T_WRITE_RESPONSE_ERROR = "can't write response error.";
59+
public static final Integer BEARER_WORD_LENGTH = SECURITY_TYPE.length();
60+
61+
public static final String[] STATIC_FILES = {
62+
"/robots.txt",
63+
"/font/**",
64+
"/css/**",
65+
"/webjars/**",
66+
"/webjars/",
67+
"/js/**",
68+
"/favicon.ico",
69+
"/**.html",
70+
"/documentation/**"
71+
};
72+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.throyer.common.springboot.constants;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class TOAST_MESSAGES {
7+
private TOAST_MESSAGES() { }
8+
9+
public static final String TOAST_SUCCESS_MESSAGE = "registration.success";
10+
}

src/main/java/com/github/throyer/common/springboot/controllers/api/UsersController.java

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

33
import com.github.throyer.common.springboot.domain.pagination.model.Page;
4-
import com.github.throyer.common.springboot.domain.pagination.service.Pagination;
54
import com.github.throyer.common.springboot.domain.user.form.CreateUserProps;
65
import com.github.throyer.common.springboot.domain.user.form.UpdateUserProps;
76
import com.github.throyer.common.springboot.domain.user.model.UserDetails;
8-
import com.github.throyer.common.springboot.domain.user.repository.UserRepository;
97
import com.github.throyer.common.springboot.domain.user.service.CreateUserService;
10-
import com.github.throyer.common.springboot.domain.user.service.FindUserByIdService;
8+
import com.github.throyer.common.springboot.domain.user.service.FindUserService;
119
import com.github.throyer.common.springboot.domain.user.service.RemoveUserService;
1210
import com.github.throyer.common.springboot.domain.user.service.UpdateUserService;
1311
import io.swagger.v3.oas.annotations.Operation;
@@ -21,7 +19,8 @@
2119

2220
import java.util.Optional;
2321

24-
import static com.github.throyer.common.springboot.utils.Responses.*;
22+
import static com.github.throyer.common.springboot.utils.Responses.created;
23+
import static com.github.throyer.common.springboot.utils.Responses.ok;
2524
import static org.springframework.http.HttpStatus.CREATED;
2625
import static org.springframework.http.HttpStatus.NO_CONTENT;
2726

@@ -33,23 +32,19 @@ public class UsersController {
3332
private final CreateUserService createService;
3433
private final UpdateUserService updateService;
3534
private final RemoveUserService removeService;
36-
private final FindUserByIdService findByIdService;
37-
38-
private final UserRepository repository;
35+
private final FindUserService findService;
3936

4037
@Autowired
4138
public UsersController(
4239
CreateUserService createService,
4340
UpdateUserService updateService,
4441
RemoveUserService removeService,
45-
FindUserByIdService findByIdService,
46-
UserRepository repository
42+
FindUserService findService
4743
) {
4844
this.createService = createService;
4945
this.updateService = updateService;
5046
this.removeService = removeService;
51-
this.findByIdService = findByIdService;
52-
this.repository = repository;
47+
this.findService = findService;
5348
}
5449

5550
@GetMapping
@@ -60,8 +55,7 @@ public ResponseEntity<Page<UserDetails>> index(
6055
Optional<Integer> page,
6156
Optional<Integer> size
6257
) {
63-
var pageable = Pagination.of(page, size);
64-
var content = repository.findAll(pageable);
58+
var content = findService.find(page, size);
6559
return ok(content.map(UserDetails::new));
6660
}
6761

@@ -70,8 +64,7 @@ public ResponseEntity<Page<UserDetails>> index(
7064
@PreAuthorize("hasAnyAuthority('ADM', 'USER')")
7165
@Operation(summary = "Show user info")
7266
public ResponseEntity<UserDetails> show(@PathVariable Long id) {
73-
var user = repository.findById(id)
74-
.orElseThrow(() -> notFound("user not found"));
67+
var user = findService.find(id);
7568
return ok(new UserDetails(user));
7669
}
7770

@@ -81,9 +74,8 @@ public ResponseEntity<UserDetails> show(@PathVariable Long id) {
8174
public ResponseEntity<UserDetails> save(
8275
@Validated @RequestBody CreateUserProps props
8376
) {
84-
props.validate();
8577
var user = createService.create(props);
86-
return created(user, "api/users");
78+
return created(new UserDetails(user), "api/users");
8779
}
8880

8981
@PutMapping("/{id}")
@@ -95,7 +87,7 @@ public ResponseEntity<UserDetails> update(
9587
@RequestBody @Validated UpdateUserProps body
9688
) {
9789
var user = updateService.update(id, body);
98-
return ok(user);
90+
return ok(new UserDetails(user));
9991
}
10092

10193
@DeleteMapping("/{id}")
Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package com.github.throyer.common.springboot.controllers.app;
22

3-
import static com.github.throyer.common.springboot.domain.toast.Type.SUCCESS;
4-
import static com.github.throyer.common.springboot.utils.Responses.validateAndUpdateModel;
5-
63
import com.github.throyer.common.springboot.domain.user.form.CreateUserProps;
74
import com.github.throyer.common.springboot.domain.user.service.CreateUserService;
8-
import com.github.throyer.common.springboot.domain.toast.Toasts;
9-
10-
import javax.validation.Valid;
11-
125
import org.springframework.beans.factory.annotation.Autowired;
136
import org.springframework.stereotype.Controller;
147
import org.springframework.ui.Model;
@@ -18,12 +11,18 @@
1811
import org.springframework.web.bind.annotation.RequestMapping;
1912
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
2013

14+
import javax.validation.Valid;
15+
2116
@Controller
2217
@RequestMapping("/app/register")
2318
public class RegisterController {
2419

20+
private final CreateUserService service;
21+
2522
@Autowired
26-
private CreateUserService service;
23+
public RegisterController(CreateUserService service) {
24+
this.service = service;
25+
}
2726

2827
@GetMapping(produces = "text/html")
2928
public String index(Model model) {
@@ -34,21 +33,16 @@ public String index(Model model) {
3433
@PostMapping(produces = "text/html")
3534
public String create(
3635
@Valid CreateUserProps props,
37-
BindingResult result,
36+
BindingResult validations,
3837
RedirectAttributes redirect,
3938
Model model
4039
) {
40+
service.create(props, validations, redirect, model);
4141

42-
props.validate(result);
43-
44-
if (validateAndUpdateModel(model, props, "user", result)) {
42+
if (validations.hasErrors()) {
4543
return "app/register/index";
4644
}
47-
48-
service.create(props);
49-
50-
Toasts.add(redirect, "Cadastro realizado com sucesso.", SUCCESS);
51-
45+
5246
return "redirect:/app/login";
5347
}
5448
}

src/main/java/com/github/throyer/common/springboot/domain/mail/service/MailService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.springframework.web.server.ResponseStatusException;
1414
import org.thymeleaf.TemplateEngine;
1515

16-
import static com.github.throyer.common.springboot.utils.Constants.MAIL.*;
16+
import static com.github.throyer.common.springboot.constants.MAIL.*;
1717
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
1818

1919
@Service

0 commit comments

Comments
 (0)