-
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 #96 from Tech-Harbor/Bezsmertnyi
Bezsmertnyi
- Loading branch information
Showing
22 changed files
with
296 additions
and
39 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
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
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/example/backend/security/exception/AccessDeniedHandlerJwt.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.example.backend.security.exception; | ||
|
||
import com.example.backend.security.models.response.ErrorResponse; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.SneakyThrows; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AccessDeniedHandlerJwt implements AccessDeniedHandler { | ||
@Override | ||
@SneakyThrows | ||
public void handle(final HttpServletRequest httpServletRequest, | ||
final HttpServletResponse httpServletResponse, | ||
final AccessDeniedException e) { | ||
|
||
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN); | ||
|
||
ErrorResponse response = new ErrorResponse( | ||
HttpServletResponse.SC_FORBIDDEN, | ||
"You don't have required role to perform this action." | ||
); | ||
|
||
final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
mapper.writeValue(httpServletResponse.getOutputStream(), response); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/example/backend/security/exception/AuthenticationEntryPointJwt.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.example.backend.security.exception; | ||
|
||
import com.example.backend.security.models.response.ErrorResponse; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.SneakyThrows; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AuthenticationEntryPointJwt implements AuthenticationEntryPoint { | ||
@Override | ||
@SneakyThrows | ||
public void commence(final HttpServletRequest httpServletRequest, | ||
final HttpServletResponse httpServletResponse, | ||
final AuthenticationException authenticationException) { | ||
|
||
httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
|
||
ErrorResponse response = new ErrorResponse( | ||
HttpServletResponse.SC_UNAUTHORIZED, | ||
"You need to login first in order to perform this action." | ||
); | ||
|
||
final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
mapper.writeValue(httpServletResponse.getOutputStream(), response); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/example/backend/security/models/response/ErrorResponse.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,20 @@ | ||
package com.example.backend.security.models.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@AllArgsConstructor | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class ErrorResponse { | ||
@Schema(example = "404", description = "Статус помилки") | ||
private int status; | ||
@Schema(example = "Error message", description = "Опис помилки") | ||
private String message; | ||
} |
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
Oops, something went wrong.