Перенаправление Spring MVC с помощью POST

#spring-mvc #redirect #post

#spring-mvc #перенаправление #Публикация

Вопрос:

 I am trying to redirect a user to external url to complete the transaction and this website is based SAML based authentication and accepts SAMLResponse as a post parameter. Below is my controller able to get the values in the logs
  

—— Контроллер —————————————————

 @RequestMapping(value="/redirect", method = { RequestMethod.GET, RequestMethod.POST })
       
public String redirect(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttrs) {
    
  String samlRes =  String.valueOf(request.getSession().getAttribute("STRRESPONSE"));
    
  byte[] base64samlRes = Base64.encode(samlRes.getBytes());
    
           
  redirectAttrs.addAttribute("SAMLResponse", base64samlRes);
  L.debug("Redirecting "    samlRes);
  L.debug("Redirecting  Base64 Encoder  "      new String(base64samlRes, Charset.forName("UTF-8")));
  return "redirect";
 
  

—————- Контроллер завершается ———————-

 and using the below html to post the external website.. I am seeing the browser the html title but it is not loading the external portal and values in the html is also null
  

—- Автоматическая отправка HTML ————————

   <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Redirect Payment Portal</title>
    </head>
    <body>
    <ul>
        <li>SamlResponse: "${SAMLResponse}"</li>
    </ul>
    <form name="myRedirectForm" action="<external url>" method="post">
        <input name="SAMLResponse" type="hidden" value="${SAMLResponse}" />
        <noscript>
            <input type="submit" value="Click here to continue" />
        </noscript>
    </form>
    <script type="text/javascript">
    
            $(document).ready(function() {
                document.myRedirectForm.submit();
            });
    
        </script>
    </body>
    </html>
  

===================== Конечный HTML ———————-
Я что-то пропустил

Ответ №1:

Я могу получить значение в html, изменив значение [[${SAMLResponse}]], но форма не отправляется

Ответ №2:

Наконец, проблема решена с помощью следующих обновлений

—— HTML — изменена следующая строка

 <input name="SAMLResponse" type="hidden" value="${SAMLResponse}" />

to

 <input name="SAMLResponse" type="hidden" th:value="${SAMLResponse}" />
  

— Конец HTML —-

—— Изменения контроллера —-

 @RequestMapping(value="/redirect", method = { RequestMethod.GET, RequestMethod.POST })
       
public ModelAndView redirect(HttpServletRequest request, HttpServletResponse response, Model model) {
    
  String samlRes =  String.valueOf(request.getSession().getAttribute("STRRESPONSE"));
    
  byte[] base64samlRes = Base64.encode(samlRes.getBytes());
    
           
  model.addAttribute("SAMLResponse", new String(base64samlRes, Charset.forName("UTF-8"));
  L.debug("Redirecting "    samlRes);
  L.debug("Redirecting  Base64 Encoder  "      new String(base64samlRes, Charset.forName("UTF-8")));
  return new ModelAndView ("redirect");
  

—- Контроллер изменяет конец ——