-
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.
Merge pull request #1 from jpcchaves/SECURITY-IMPL
Security impl
- Loading branch information
Showing
43 changed files
with
1,314 additions
and
322 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
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,14 @@ | ||
package com.ws.taskmanager.common; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
|
||
public class DateUtils { | ||
public static String convertDateToString(Date date) { | ||
SimpleDateFormat formatter = new SimpleDateFormat( | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | ||
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
return formatter.format(date); | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...com/ws/taskmanager/configs/AppConfig.java → .../com/ws/taskmanager/config/AppConfig.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
.../taskmanager/configs/date/DateConfig.java → ...s/taskmanager/config/date/DateConfig.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
73 changes: 73 additions & 0 deletions
73
src/main/java/com/ws/taskmanager/config/security/SecurityConfig.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,73 @@ | ||
package com.ws.taskmanager.config.security; | ||
|
||
import com.ws.taskmanager.security.CustomAccessDeniedHandler; | ||
import com.ws.taskmanager.security.JwtAuthenticationEntrypoint; | ||
import com.ws.taskmanager.security.JwtAuthenticationFilter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@Configuration | ||
@EnableMethodSecurity | ||
public class SecurityConfig { | ||
|
||
private final UserDetailsService userDetailsService; | ||
private final JwtAuthenticationEntrypoint authenticationEntryPoint; | ||
private final CustomAccessDeniedHandler customAccessDeniedHandler; | ||
private JwtAuthenticationFilter authenticationFilter; | ||
|
||
public SecurityConfig(JwtAuthenticationEntrypoint authenticationEntryPoint, | ||
UserDetailsService userDetailsService, | ||
JwtAuthenticationFilter authenticationFilter, | ||
CustomAccessDeniedHandler customAccessDeniedHandler) { | ||
this.authenticationEntryPoint = authenticationEntryPoint; | ||
this.userDetailsService = userDetailsService; | ||
this.authenticationFilter = authenticationFilter; | ||
this.customAccessDeniedHandler = customAccessDeniedHandler; | ||
} | ||
|
||
@Bean | ||
public static PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { | ||
return configuration.getAuthenticationManager(); | ||
} | ||
|
||
@Bean | ||
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
|
||
http.csrf().disable() | ||
.authorizeHttpRequests(authorize -> | ||
authorize | ||
.requestMatchers("/api/v1/auth/**", "/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
) | ||
.exceptionHandling(exception -> | ||
exception | ||
.authenticationEntryPoint(authenticationEntryPoint) | ||
.accessDeniedHandler(customAccessDeniedHandler) | ||
) | ||
.sessionManagement(session -> | ||
session | ||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
); | ||
|
||
http.addFilterBefore(authenticationFilter, UsernamePasswordAuthenticationFilter.class); | ||
|
||
return http.build(); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...anager/configs/swagger/OpenApiConfig.java → ...manager/config/swagger/OpenApiConfig.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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/ws/taskmanager/controller/AuthController.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,33 @@ | ||
package com.ws.taskmanager.controller; | ||
|
||
import com.ws.taskmanager.data.DTO.JWTAuthResponseDto; | ||
import com.ws.taskmanager.data.DTO.LoginDto; | ||
import com.ws.taskmanager.data.DTO.RegisterDto; | ||
import com.ws.taskmanager.data.DTO.RegisterResponseDto; | ||
import com.ws.taskmanager.services.AuthService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/auth") | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
public AuthController(AuthService authService) { | ||
this.authService = authService; | ||
} | ||
|
||
@PostMapping(value = {"/login", "/signin"}) | ||
public ResponseEntity<JWTAuthResponseDto> login(@RequestBody LoginDto loginDto) { | ||
return ResponseEntity.ok(authService.login(loginDto)); | ||
} | ||
|
||
@PostMapping(value = {"/register", "/signup"}) | ||
public ResponseEntity<RegisterResponseDto> register(@RequestBody RegisterDto registerDto) { | ||
return ResponseEntity.status(HttpStatus.CREATED).body(authService.register(registerDto)); | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/main/java/com/ws/taskmanager/data/DTO/JWTAuthResponseDto.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,41 @@ | ||
package com.ws.taskmanager.data.DTO; | ||
|
||
|
||
public class JWTAuthResponseDto { | ||
private String accessToken; | ||
private String tokenType = "Bearer"; | ||
private UserDto user; | ||
|
||
public JWTAuthResponseDto() { | ||
} | ||
|
||
public JWTAuthResponseDto(String accessToken, String tokenType, UserDto user) { | ||
this.accessToken = accessToken; | ||
this.tokenType = tokenType; | ||
this.user = user; | ||
} | ||
|
||
public String getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public void setAccessToken(String accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
|
||
public String getTokenType() { | ||
return tokenType; | ||
} | ||
|
||
public void setTokenType(String tokenType) { | ||
this.tokenType = tokenType; | ||
} | ||
|
||
public UserDto getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(UserDto user) { | ||
this.user = user; | ||
} | ||
} |
Oops, something went wrong.
05bb111
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
task-manager – ./
task-manager-jpcchaves.vercel.app
task-manager-git-main-jpcchaves.vercel.app
task-manager-five-brown.vercel.app