-
Notifications
You must be signed in to change notification settings - Fork 1
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 #555 from bounswe/backend-authorization
authorization logic implemented
- Loading branch information
Showing
11 changed files
with
160 additions
and
61 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
app/backend/src/main/java/com/app/gamereview/config/JwtInterceptor.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,85 @@ | ||
package com.app.gamereview.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.app.gamereview.model.User; | ||
import com.app.gamereview.util.AuthorizationRequired; | ||
import com.app.gamereview.util.JwtUtil; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import com.app.gamereview.repository.UserRepository; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Component | ||
public class JwtInterceptor implements HandlerInterceptor { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public JwtInterceptor(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
throws Exception { | ||
|
||
String uri = request.getRequestURI(); | ||
|
||
// Exclude Swagger URLs | ||
if (uri.startsWith("/v3/api-docs") || uri.contains("/swagger-ui/")) { | ||
return true; | ||
} | ||
|
||
if (handler instanceof HandlerMethod) { | ||
HandlerMethod handlerMethod = (HandlerMethod) handler; | ||
AuthorizationRequired authRequired = handlerMethod.getMethodAnnotation(AuthorizationRequired.class); | ||
String token = request.getHeader("Authorization"); | ||
if (authRequired != null) { | ||
if (token == null || !JwtUtil.validateToken(token)) { | ||
response.setStatus(HttpStatus.UNAUTHORIZED.value()); | ||
Map<String, String> responseMessage = new HashMap<>(); | ||
responseMessage.put("message", "Token is not valid"); | ||
responseMessage.put("code", "401"); | ||
|
||
String jsonResponse = objectMapper.writeValueAsString(responseMessage); | ||
|
||
response.getWriter().write(jsonResponse); | ||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
return false; | ||
} | ||
String email = JwtUtil.extractSubject(token); | ||
Optional<User> user = userRepository.findByEmailAndIsDeletedFalse(email); | ||
if (user.isPresent()) { | ||
request.setAttribute("authenticatedUser", user.get()); | ||
} | ||
else { | ||
response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value()); | ||
Map<String, String> responseMessage = new HashMap<>(); | ||
responseMessage.put("message", "User doesn't exist or account is deleted"); | ||
responseMessage.put("code", "405"); | ||
|
||
String jsonResponse = objectMapper.writeValueAsString(responseMessage); | ||
|
||
response.getWriter().write(jsonResponse); | ||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/backend/src/main/java/com/app/gamereview/config/WebConfig.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.app.gamereview.config; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Autowired | ||
private JwtInterceptor jwtInterceptor; | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(jwtInterceptor); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,6 @@ | |
@Getter | ||
public class ForgotChangeUserPasswordRequestDto { | ||
|
||
private String userId; | ||
|
||
private String newPassword; | ||
|
||
} |
5 changes: 4 additions & 1 deletion
5
app/backend/src/main/java/com/app/gamereview/dto/request/MeRequestDto.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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package com.app.gamereview.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class MeRequestDto { | ||
String token; | ||
|
||
String token; | ||
|
||
} |
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
12 changes: 12 additions & 0 deletions
12
app/backend/src/main/java/com/app/gamereview/util/AuthorizationRequired.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,12 @@ | ||
package com.app.gamereview.util; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AuthorizationRequired { | ||
|
||
} |
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