0

I want to mock authentication using spring aop. In our application we have authentication trough bank, backend redirects to bank, and then after user successfully authenticates via bank client we get a callback and user is authenticated in our system.

@Controller
@RequestMapping("/auth/bank")
public class BankAuth {
  @GetMapping
  public String getBankTemplate() {
    // prepare data
    return "redirect:/bank";
  }

  @PostMapping("/callback")
  public String bankCallback(HttpServletRequest request, HttpServletResponse response,) {
    // parse data from callback request
    return "redirect:/index";
  }
}

What i want to achieve is to intercept getBankTemplate method, add mock data to request and forward that request to bankCallback, so user would be successfully authenticated.

I can't use forwarding cause bankCallback request is POST.

Is there a way to achieve this without using httpClient in interceptor?

@Aspect
public class MockBankInterceptor {
  @Pointcut("execution (* ...getBankTemplate(...))")
  public void bankTemplate() {}

  @Around("bankTemplate()")
  public String authenticateUser(ProceedingJoinPoint pjp) {
    pjp.proceed();

    // populate request with data required to authenticate customer

    return "forward:/auth/bank/callback";
  }
}
Simas.B
  • 724
  • 1
  • 13
  • 31
  • At least to me it is unclear what exactly you want to do, because you seem to have omitted the exact parts of the code you wish to change. Please learn [how to ask a question on SO](https://stackoverflow.com/help/how-to-ask) and provide a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). Thank you. – kriegaex Jul 19 '23 at 02:37
  • The ProceedingJoinPoint contains a link to controller. Why not call the bankCallback method directly? You can get the request by using [this](https://stackoverflow.com/a/26323545/19352128) to add mock data – Islam Khabibullin Jul 19 '23 at 06:56

0 Answers0