Skip to content

Commit

Permalink
recombee and auth response error fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
mehedikhan72 committed Aug 29, 2024
1 parent d9abff6 commit 125f8a8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.amplifiers.pathfinder.config;

import com.amplifiers.pathfinder.auth.CustomAuthenticationEntryPoint;
import com.amplifiers.pathfinder.exception.GlobalExceptionHandler;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -17,6 +21,8 @@
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static com.amplifiers.pathfinder.entity.user.Permission.*;
import static com.amplifiers.pathfinder.entity.user.Role.ADMIN;
Expand Down Expand Up @@ -47,13 +53,13 @@ public class SecurityConfiguration {
private final AuthenticationProvider authenticationProvider;
private final LogoutService logoutService;
private final CustomAuthenticationEntryPoint customAuthenticationEntryPoint;

private final GlobalExceptionHandler globalExceptionHandler;

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOriginPatterns(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET","POST","PATCH", "PUT", "DELETE", "OPTIONS", "HEAD"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS", "HEAD"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setExposedHeaders(Arrays.asList("X-Get-Header"));
Expand All @@ -65,7 +71,7 @@ public CorsConfigurationSource corsConfigurationSource() {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors((cors)->cors.configurationSource(corsConfigurationSource()))
.cors((cors) -> cors.configurationSource(corsConfigurationSource()))
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(req ->
req.requestMatchers(WHITE_LIST_URL)
Expand All @@ -88,7 +94,17 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
)
.exceptionHandling(exceptionHandling ->
exceptionHandling
.defaultAuthenticationEntryPointFor(customAuthenticationEntryPoint, request -> true)
.authenticationEntryPoint(customAuthenticationEntryPoint) // 401 Unauthorized
.accessDeniedHandler((request, response, accessDeniedException) -> { // 403 Forbidden
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setStatus(HttpStatus.FORBIDDEN.value());
Map<String, Object> body = new HashMap<>();
body.put("status_code", HttpStatus.FORBIDDEN.value());
body.put("error", "Forbidden");
body.put("message", "Access denied. You do not have permission to access this resource.");
body.put("path", request.getServletPath());
new ObjectMapper().writeValue(response.getOutputStream(), body);
})
);
return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String deleteGig(
@GetMapping("/recommendations")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<?> getRecommendationsForUser() {
return ResponseEntity.ok(service.getRecommendationsForUser(""));
return ResponseEntity.ok(service.getRecommendationsForUser(null));
}

@GetMapping("/recommendations/popular-gigs")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,17 @@
import lombok.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.context.support.DefaultMessageSourceResolvable;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ spring:
multipart:
max-file-size: 100MB
max-request-size: 100MB
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false

application:
security:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.amplifiers.pathfinder.entity.enrollment.Enrollment;
import com.amplifiers.pathfinder.entity.enrollment.EnrollmentRepository;
import com.amplifiers.pathfinder.entity.gig.Gig;
import com.amplifiers.pathfinder.entity.gig.GigRepository;
import com.amplifiers.pathfinder.entity.notification.NotificationService;
import com.amplifiers.pathfinder.sslcommerz.TransactionResponseValidator;
import com.amplifiers.pathfinder.entity.transaction.Transaction;
import com.amplifiers.pathfinder.entity.transaction.TransactionRepository;
import com.amplifiers.pathfinder.entity.user.User;
import com.amplifiers.pathfinder.sslcommerz.TransactionResponseValidator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -32,6 +33,8 @@ class PaymentServiceTest {
private NotificationService notificationService;
@Mock
private EnrollmentRepository enrollmentRepository;
@Mock
private GigRepository gigRepository;


private PaymentService paymentService;
Expand All @@ -41,7 +44,7 @@ class PaymentServiceTest {

@BeforeEach
void setUp() {
paymentService = new PaymentService(transactionRepository, transactionResponseValidator, notificationService, enrollmentRepository);
paymentService = new PaymentService(transactionRepository, transactionResponseValidator, notificationService, enrollmentRepository, gigRepository);

User buyer = new User();
buyer.setFirstName("John");
Expand Down

0 comments on commit 125f8a8

Please sign in to comment.