Skip to content

Commit c41b35e

Browse files
authored
Merge pull request #14 from Throyer/development
Development
2 parents c70a5f8 + 024ff38 commit c41b35e

File tree

14 files changed

+194
-391
lines changed

14 files changed

+194
-391
lines changed

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
3535
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
3636
import org.springframework.stereotype.Component;
37+
import org.springframework.web.cors.CorsConfiguration;
3738

3839
@Component
3940
@EnableWebSecurity
@@ -70,28 +71,34 @@ protected void configure(HttpSecurity http) throws Exception {
7071
http
7172
.antMatcher("/api/**")
7273
.authorizeRequests()
73-
.antMatchers(GET, "/api")
74+
.antMatchers(
75+
GET,
76+
"/api",
77+
"/api/documentation/**"
78+
)
7479
.permitAll()
75-
.antMatchers(POST, "/api/users")
76-
.permitAll()
77-
.antMatchers(POST, "/api/sessions/**")
78-
.permitAll()
79-
.antMatchers(POST, "/api/recoveries/**")
80-
.permitAll()
81-
.antMatchers(GET, "/api/documentation/**")
80+
.antMatchers(
81+
POST,
82+
"/api/users",
83+
"/api/sessions/**",
84+
"/api/recoveries/**",
85+
"/api/documentation/**"
86+
)
8287
.permitAll()
8388
.anyRequest()
8489
.authenticated()
8590
.and()
8691
.csrf()
87-
.disable()
92+
.disable()
8893
.exceptionHandling()
89-
.authenticationEntryPoint((request, response, exception) -> forbidden(response))
94+
.authenticationEntryPoint((request, response, exception) -> forbidden(response))
9095
.and()
9196
.sessionManagement()
92-
.sessionCreationPolicy(STATELESS)
97+
.sessionCreationPolicy(STATELESS)
9398
.and()
94-
.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);
99+
.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class)
100+
.cors()
101+
.configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues());
95102
}
96103

97104
@Override
@@ -124,29 +131,31 @@ protected void configure(HttpSecurity http) throws Exception {
124131
http
125132
.antMatcher("/app/**")
126133
.authorizeRequests()
127-
.antMatchers(GET, LOGIN_URL)
128-
.permitAll()
129-
.antMatchers(GET, "/app")
130-
.permitAll()
131-
.antMatchers(GET, "/app/register")
134+
.antMatchers(
135+
GET,
136+
LOGIN_URL,
137+
"/app",
138+
"/app/register",
139+
"/app/recovery/**"
140+
)
132141
.permitAll()
133-
.antMatchers(POST, "/app/register")
134-
.permitAll()
135-
.antMatchers(GET, "/app/recovery/**")
136-
.permitAll()
137-
.antMatchers(POST, "/app/recovery/**")
142+
.antMatchers(
143+
POST,
144+
"/app/register",
145+
"/app/recovery/**"
146+
)
138147
.permitAll()
139148
.anyRequest()
140149
.authenticated()
141-
.and()
142-
.csrf()
143-
.disable()
144-
.formLogin()
145-
.loginPage(LOGIN_URL)
146-
.failureUrl(LOGIN_ERROR_URL)
147-
.defaultSuccessUrl(HOME_URL)
148-
.usernameParameter(USERNAME_PARAMETER)
149-
.passwordParameter(PASSWORD_PARAMETER)
150+
.and()
151+
.csrf()
152+
.disable()
153+
.formLogin()
154+
.loginPage(LOGIN_URL)
155+
.failureUrl(LOGIN_ERROR_URL)
156+
.defaultSuccessUrl(HOME_URL)
157+
.usernameParameter(USERNAME_PARAMETER)
158+
.passwordParameter(PASSWORD_PARAMETER)
150159
.and()
151160
.rememberMe()
152161
.key(SECRET)

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,30 @@
1010
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
1111
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
1212
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
13+
import io.swagger.v3.oas.annotations.info.Contact;
1314
import io.swagger.v3.oas.annotations.info.Info;
15+
import io.swagger.v3.oas.annotations.info.License;
1416
import io.swagger.v3.oas.annotations.security.SecurityScheme;
1517

1618
@Configuration
17-
@OpenAPIDefinition(info = @Info(title = "Common API", version = "v1"))
19+
@OpenAPIDefinition(info = @Info(
20+
title = "Common CRUD API",
21+
version = "v3.0.4",
22+
description = """
23+
A complete user registry, with access permissions,
24+
JWT token, integration and unit tests, using
25+
the RESTful API pattern.
26+
""",
27+
license = @License(
28+
name = "GNU General Public License v3.0",
29+
url = "https://github.com/Throyer/springboot-api-crud/blob/master/LICENSE"
30+
),
31+
contact = @Contact(
32+
name = "Throyer",
33+
email = "[email protected]",
34+
url = "https://github.com/Throyer"
35+
)
36+
))
1837
@SecurityScheme(
1938
name = "token",
2039
type = SecuritySchemeType.HTTP,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import com.github.throyer.common.springboot.domain.models.entity.Role;
88
import com.github.throyer.common.springboot.domain.repositories.RoleRepository;
9+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
910

1011
import org.springframework.beans.factory.annotation.Autowired;
1112
import org.springframework.http.ResponseEntity;
@@ -16,6 +17,7 @@
1617

1718
@RestController
1819
@RequestMapping("/api/roles")
20+
@SecurityRequirement(name = "token")
1921
@PreAuthorize("hasAnyAuthority('ADM')")
2022
public class RolesController {
2123

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55

66
import com.github.throyer.common.springboot.domain.models.entity.User;
77
import com.github.throyer.common.springboot.domain.models.pagination.Page;
8-
import com.github.throyer.common.springboot.domain.models.pagination.Pagination;
8+
99
import com.github.throyer.common.springboot.domain.services.user.CreateUserService;
1010
import com.github.throyer.common.springboot.domain.services.user.FindUserService;
1111
import com.github.throyer.common.springboot.domain.services.user.RemoveUserService;
1212
import com.github.throyer.common.springboot.domain.services.user.UpdateUserService;
1313
import com.github.throyer.common.springboot.domain.services.user.dto.CreateUserApi;
14-
import com.github.throyer.common.springboot.domain.services.user.dto.SearchUser;
1514
import com.github.throyer.common.springboot.domain.services.user.dto.UpdateUser;
1615
import com.github.throyer.common.springboot.domain.services.user.dto.UserDetails;
16+
import static com.github.throyer.common.springboot.utils.Responses.ok;
17+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
18+
import java.util.Optional;
1719

1820
import org.springframework.beans.factory.annotation.Autowired;
19-
import org.springframework.data.domain.Sort;
2021
import org.springframework.http.ResponseEntity;
2122
import org.springframework.security.access.prepost.PreAuthorize;
2223
import org.springframework.validation.annotation.Validated;
@@ -47,12 +48,18 @@ public class UsersController {
4748
private FindUserService findService;
4849

4950
@GetMapping
51+
@SecurityRequirement(name = "token")
5052
@PreAuthorize("hasAnyAuthority('ADM')")
51-
public ResponseEntity<Page<UserDetails>> index(Pagination pagination, Sort sort, SearchUser search) {
52-
return findService.find(pagination, sort, search);
53+
public ResponseEntity<Page<UserDetails>> index(
54+
Optional<Integer> page,
55+
Optional<Integer> size
56+
) {
57+
var result = findService.findAll(page, size);
58+
return ok(result);
5359
}
5460

5561
@GetMapping("/{id}")
62+
@SecurityRequirement(name = "token")
5663
@PreAuthorize("hasAnyAuthority('ADM', 'USER')")
5764
public ResponseEntity<UserDetails> show(@PathVariable Long id) {
5865
return findService.find(id);
@@ -65,6 +72,7 @@ public ResponseEntity<UserDetails> save(@Validated @RequestBody CreateUserApi bo
6572
}
6673

6774
@PutMapping("/{id}")
75+
@SecurityRequirement(name = "token")
6876
@PreAuthorize("hasAnyAuthority('ADM', 'USER')")
6977
public ResponseEntity<UserDetails> update(
7078
@PathVariable Long id,
@@ -75,6 +83,7 @@ public ResponseEntity<UserDetails> update(
7583

7684
@DeleteMapping("/{id}")
7785
@ResponseStatus(NO_CONTENT)
86+
@SecurityRequirement(name = "token")
7887
@PreAuthorize("hasAnyAuthority('ADM')")
7988
public ResponseEntity<User> destroy(@PathVariable Long id) {
8089
return removeService.remove(id);

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

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

3-
import com.github.throyer.common.springboot.domain.models.pagination.Page;
4-
import com.github.throyer.common.springboot.domain.models.pagination.Pagination;
53
import com.github.throyer.common.springboot.domain.models.shared.Type;
6-
import com.github.throyer.common.springboot.domain.repositories.UserRepository;
4+
import com.github.throyer.common.springboot.domain.services.user.FindUserService;
75
import com.github.throyer.common.springboot.domain.services.user.RemoveUserService;
8-
import com.github.throyer.common.springboot.domain.services.user.dto.SearchUser;
96
import com.github.throyer.common.springboot.utils.Toasts;
7+
import java.util.Optional;
108
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.data.domain.Sort;
129
import org.springframework.security.access.prepost.PreAuthorize;
1310
import org.springframework.stereotype.Controller;
1411
import org.springframework.ui.Model;
@@ -24,17 +21,21 @@
2421
public class UserController {
2522

2623
@Autowired
27-
private UserRepository repository;
24+
private FindUserService findService;
2825

2926
@Autowired
3027
private RemoveUserService removeService;
3128

3229
@GetMapping
33-
public String index(Model model, Pagination pagination, Sort sort, SearchUser search) {
30+
public String index(
31+
Model model,
32+
Optional<Integer> page,
33+
Optional<Integer> size
34+
) {
3435

35-
var page = Page.of(repository.findSimplifiedUsers(pagination.build()));
36+
var result = findService.findAll(page, size);
3637

37-
model.addAttribute("page", page);
38+
model.addAttribute("page", result);
3839

3940
return "app/users/index";
4041
}

src/main/java/com/github/throyer/common/springboot/domain/models/entity/Recovery.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public Recovery(User user, Integer minutesToExpire) {
4444
this.code = code();
4545
}
4646

47-
public Recovery(String email, String password_recovery_code, String code) {
48-
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
49-
}
50-
5147
public Long getId() {
5248
return id;
5349
}

src/main/java/com/github/throyer/common/springboot/domain/models/entity/Role.java

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import javax.persistence.Table;
1111

1212
import com.fasterxml.jackson.annotation.JsonIgnore;
13+
import lombok.Data;
1314

1415
import org.hibernate.annotations.Where;
1516
import org.springframework.security.core.GrantedAuthority;
1617

18+
@Data
1719
@Entity
1820
@Table(name = "role")
1921
@Where(clause = Auditable.NON_DELETED_CLAUSE)
@@ -57,55 +59,6 @@ public Role(Long id, String initials) {
5759
this.initials = initials;
5860
}
5961

60-
public Long getId() {
61-
return id;
62-
}
63-
64-
public void setId(Long id) {
65-
this.id = id;
66-
}
67-
68-
public String getName() {
69-
return name;
70-
}
71-
72-
public void setName(String name) {
73-
this.name = name;
74-
}
75-
76-
public String getDeletedName() {
77-
return deletedName;
78-
}
79-
80-
public String getInitials() {
81-
return initials;
82-
}
83-
84-
public void setInitials(String initials) {
85-
this.initials = initials;
86-
}
87-
88-
public String getDeletedInitials() {
89-
return deletedInitials;
90-
}
91-
92-
public String getDescription() {
93-
return description;
94-
}
95-
96-
public void setDescription(String description) {
97-
this.description = description;
98-
}
99-
100-
public Boolean compare(String search) {
101-
if (Objects.nonNull(search)) {
102-
return
103-
getName().toLowerCase().equals(search.toLowerCase()) ||
104-
getInitials().toLowerCase().equals(search.toLowerCase());
105-
}
106-
return false;
107-
}
108-
10962
@Override
11063
public boolean equals(Object object) {
11164
if (object == this)

0 commit comments

Comments
 (0)