Skip to content

Commit 3821166

Browse files
fix(auth): Fix AuthorizationService missing Service annotation
1 parent a636d4c commit 3821166

File tree

13 files changed

+101
-89
lines changed

13 files changed

+101
-89
lines changed

Vava-API.zip

185 KB
Binary file not shown.

pom.xml

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.4.2</version>
8+
<version>3.4.3</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.helper</groupId>
@@ -30,65 +30,62 @@
3030
<java.version>17</java.version>
3131
</properties>
3232
<dependencies>
33+
<!-- Spring Dependencies -->
3334
<dependency>
3435
<groupId>org.springframework.boot</groupId>
35-
<artifactId>spring-boot-starter-web</artifactId>
36+
<artifactId>spring-boot-starter-data-jpa</artifactId>
3637
</dependency>
37-
3838
<dependency>
3939
<groupId>org.springframework.boot</groupId>
40-
<artifactId>spring-boot-starter-security</artifactId>
40+
<artifactId>spring-boot-starter-actuator</artifactId>
4141
</dependency>
42-
4342
<dependency>
4443
<groupId>org.springframework.boot</groupId>
45-
<artifactId>spring-boot-devtools</artifactId>
46-
<scope>runtime</scope>
47-
<optional>true</optional>
44+
<artifactId>spring-boot-starter-validation</artifactId>
4845
</dependency>
49-
5046
<dependency>
51-
<groupId>org.projectlombok</groupId>
52-
<artifactId>lombok</artifactId>
53-
<optional>true</optional>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-security</artifactId>
5449
</dependency>
55-
5650
<dependency>
5751
<groupId>org.springframework.boot</groupId>
5852
<artifactId>spring-boot-starter-test</artifactId>
5953
<scope>test</scope>
6054
</dependency>
61-
6255
<dependency>
6356
<groupId>org.springframework.security</groupId>
6457
<artifactId>spring-security-test</artifactId>
6558
<scope>test</scope>
6659
</dependency>
67-
6860
<dependency>
69-
<groupId>org.springframework.boot</groupId>
70-
<artifactId>spring-boot-starter-data-jpa</artifactId>
71-
</dependency>
72-
73-
<!-- JWT Dependencies -->
74-
<dependency>
75-
<groupId>com.auth0</groupId>
76-
<artifactId>java-jwt</artifactId>
77-
<version>4.5.0</version>
61+
<groupId>org.projectlombok</groupId>
62+
<artifactId>lombok</artifactId>
63+
<version>1.18.36</version>
64+
<scope>provided</scope>
7865
</dependency>
79-
66+
<!-- Spring Boot Azure Dependencies -->
8067
<dependency>
81-
<groupId>org.springframework.boot</groupId>
82-
<artifactId>spring-boot-starter-validation</artifactId>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-web</artifactId>
8370
</dependency>
84-
85-
<!-- Dependência para utilizar API em DESENVOLVIMENTO(dsv) -->
71+
<!-- H2 DataBase Dependencies -->
8672
<dependency>
8773
<groupId>com.h2database</groupId>
8874
<artifactId>h2</artifactId>
89-
<version>2.3.232</version>
9075
<scope>runtime</scope>
9176
</dependency>
77+
<!-- Spring Boot DevTools (*optional* for development, i like it) -->
78+
<dependency>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-devtools</artifactId>
81+
<scope>runtime</scope>
82+
</dependency>
83+
<!-- JWT Dependencies -->
84+
<dependency>
85+
<groupId>com.auth0</groupId>
86+
<artifactId>java-jwt</artifactId>
87+
<version>4.5.0</version>
88+
</dependency>
9289
</dependencies>
9390

9491
<build>
@@ -105,18 +102,19 @@
105102
</annotationProcessorPaths>
106103
</configuration>
107104
</plugin>
108-
<plugin>
109-
<groupId>org.springframework.boot</groupId>
110-
<artifactId>spring-boot-maven-plugin</artifactId>
111-
<configuration>
112-
<excludes>
113-
<exclude>
105+
<plugin>
106+
<groupId>org.apache.maven.plugins</groupId>
107+
<artifactId>maven-compiler-plugin</artifactId>
108+
<configuration>
109+
<annotationProcessorPaths>
110+
<path>
114111
<groupId>org.projectlombok</groupId>
115112
<artifactId>lombok</artifactId>
116-
</exclude>
117-
</excludes>
118-
</configuration>
119-
</plugin>
113+
<version>1.18.36</version>
114+
</path>
115+
</annotationProcessorPaths>
116+
</configuration>
117+
</plugin>
120118
</plugins>
121119
</build>
122120

src/main/java/com/helper/vavahelper/Repositories/UserRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.helper.vavahelper.models.User.User;
77

8+
89
public interface UserRepository extends JpaRepository<User, String> {
910
UserDetails findByLogin(String login);
1011
}
Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
package com.helper.vavahelper.controllers;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.http.ResponseEntity;
5-
import org.springframework.security.authentication.AuthenticationManager;
6-
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
7-
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
8-
import org.springframework.web.bind.annotation.CrossOrigin;
9-
import org.springframework.web.bind.annotation.PostMapping;
10-
import org.springframework.web.bind.annotation.RequestBody;
113
import org.springframework.web.bind.annotation.RequestMapping;
124
import org.springframework.web.bind.annotation.RestController;
135

@@ -19,13 +11,26 @@
1911
import com.helper.vavahelper.models.User.body.UserRegisterDTO;
2012
import com.helper.vavahelper.repositories.UserRepository;
2113

14+
import org.springframework.beans.factory.annotation.Autowired;
15+
16+
17+
import org.springframework.http.ResponseEntity;
18+
import org.springframework.security.authentication.AuthenticationManager;
19+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
20+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
21+
import org.springframework.web.bind.annotation.CrossOrigin;
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RequestBody;
24+
2225
import jakarta.validation.Valid;
2326

27+
2428
@RestController
2529
@RequestMapping("auth")
2630
@CrossOrigin(origins = "*")
2731
public class AuthenticationController {
28-
@Autowired
32+
33+
@Autowired
2934
private AuthenticationManager authenticationManager;
3035

3136
@Autowired
@@ -34,16 +39,6 @@ public class AuthenticationController {
3439
@Autowired
3540
private UserRepository repository;
3641

37-
@PostMapping("/login")
38-
public ResponseEntity<LoginResponseDTO> postMethodLogin(@RequestBody @Valid AuthenticationDTO data) {
39-
var usernamePassword = new UsernamePasswordAuthenticationToken(data.login(), data.password());
40-
var auth = this.authenticationManager.authenticate(usernamePassword);
41-
42-
var token = tokenService.generateToken((User)auth.getPrincipal());
43-
44-
return ResponseEntity.ok(new LoginResponseDTO(token));
45-
}
46-
4742
@PostMapping("/register")
4843
public ResponseEntity<String> postMethodRegister(@RequestBody @Valid UserRegisterDTO data){
4944

@@ -57,4 +52,15 @@ public ResponseEntity<String> postMethodRegister(@RequestBody @Valid UserRegiste
5752
this.repository.save(newUser);
5853
return ResponseEntity.ok("User registered successfully.");
5954
}
60-
}
55+
56+
@PostMapping("/login")
57+
public ResponseEntity<LoginResponseDTO> postMethodLogin(@RequestBody @Valid AuthenticationDTO data) {
58+
var usernamePassword = new UsernamePasswordAuthenticationToken(data.login(), data.password());
59+
var auth = this.authenticationManager.authenticate(usernamePassword);
60+
61+
var token = tokenService.generateToken((User)auth.getPrincipal());
62+
63+
return ResponseEntity.ok(new LoginResponseDTO(token));
64+
}
65+
66+
}

src/main/java/com/helper/vavahelper/infra/security/SecurityConfiguration.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
package com.helper.vavahelper.infra.security;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
6+
57
import org.springframework.http.HttpMethod;
8+
69
import org.springframework.security.authentication.AuthenticationManager;
10+
711
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
812
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
913
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1014
import org.springframework.security.config.http.SessionCreationPolicy;
15+
1116
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1217
import org.springframework.security.crypto.password.PasswordEncoder;
18+
1319
import org.springframework.security.web.SecurityFilterChain;
20+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1421

1522
@Configuration
1623
@EnableWebSecurity
1724
public class SecurityConfiguration {
25+
26+
@Autowired
27+
SecurityFilter securityFilter;
28+
1829
//Filtro de Segurança de Rotas:
1930
@Bean
2031
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception{
@@ -23,10 +34,12 @@ public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws
2334
.csrf(csrf -> csrf.disable())
2435
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
2536
.authorizeHttpRequests(authorize -> authorize
26-
.requestMatchers(HttpMethod.POST, "auth/**").permitAll()
37+
.requestMatchers(HttpMethod.POST, "/auth/register").permitAll()
38+
.requestMatchers(HttpMethod.POST, "/auth/login").permitAll()
2739
.requestMatchers("/h2/**").permitAll()
2840
.anyRequest().authenticated()
2941
)
42+
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)
3043
.headers(headers -> headers
3144
.frameOptions().sameOrigin() // Permite iframes no H2
3245
)

src/main/java/com/helper/vavahelper/infra/security/SecurityFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.servlet.ServletException;
55
import jakarta.servlet.http.HttpServletRequest;
66
import jakarta.servlet.http.HttpServletResponse;
7+
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
910
import org.springframework.security.core.context.SecurityContextHolder;
@@ -15,6 +16,7 @@
1516

1617
import java.io.IOException;
1718

19+
1820
@Component
1921
public class SecurityFilter extends OncePerRequestFilter{
2022

src/main/java/com/helper/vavahelper/infra/security/TokenService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
import com.auth0.jwt.algorithms.Algorithm;
1313
import com.auth0.jwt.exceptions.JWTCreationException;
1414
import com.auth0.jwt.exceptions.JWTVerificationException;
15-
1615
import com.helper.vavahelper.models.User.User;
1716

18-
1917
@Service
2018
public class TokenService {
2119

@@ -46,11 +44,11 @@ public String validateToken(String token){
4644
.verify(token)
4745
.getSubject();
4846
} catch (JWTVerificationException exception){
49-
return "";
47+
throw new RuntimeException("", exception);
5048
}
5149
}
5250

5351
private Instant getExpirationDate(){
5452
return LocalDateTime.now().plusHours(2).toInstant(ZoneOffset.of("-03:00"));
5553
}
56-
}
54+
}

src/main/java/com/helper/vavahelper/models/User/User.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.helper.vavahelper.models.User;
22
import jakarta.persistence.*;
33
import lombok.AllArgsConstructor;
4+
import lombok.Data;
45
import lombok.EqualsAndHashCode;
56
import lombok.Getter;
67
import lombok.NoArgsConstructor;
@@ -13,18 +14,20 @@
1314
import java.util.Collection;
1415
import java.util.List;
1516

16-
@Table(name = "users")
17-
@Entity(name = "users")
18-
@Getter
17+
@Data
1918
@Setter
20-
@NoArgsConstructor
19+
@Getter
2120
@AllArgsConstructor
21+
@NoArgsConstructor
22+
@Table(name= "users")
23+
@Entity(name = "users")
2224
@EqualsAndHashCode(of = "id")
2325
public class User implements UserDetails {
2426
@Id
2527
@GeneratedValue(strategy = GenerationType.UUID)
2628
private String id;
2729
private UserRole role;
30+
@Column(unique=true)
2831
private String login;
2932
private String password;
3033

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
package com.helper.vavahelper.models.User.body;
22

3-
public record AuthenticationDTO(String login, String password) {
4-
5-
6-
}
3+
public record AuthenticationDTO(String login, String password){}

src/main/java/com/helper/vavahelper/models/User/body/UserRegisterDTO.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
import com.helper.vavahelper.models.User.UserRole;
44

5-
public record UserRegisterDTO(String login, String password, UserRole role) {
6-
7-
}
5+
public record UserRegisterDTO(String login, String password, UserRole role) {}

0 commit comments

Comments
 (0)