Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirect:htmx:<url> could not work #163

Open
leeaee opened this issue Feb 11, 2025 · 1 comment
Open

redirect:htmx:<url> could not work #163

leeaee opened this issue Feb 11, 2025 · 1 comment
Labels
Waiting for Response Waiting for response

Comments

@leeaee
Copy link

leeaee commented Feb 11, 2025

Project info:

worked with JTE

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>3.4.2</version>
	<relativePath/>
</parent>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
	<groupId>io.github.wimdeblauwe</groupId>
	<artifactId>htmx-spring-boot</artifactId>
	<version>4.0.1</version>
</dependency>
<dependency>
	<groupId>gg.jte</groupId>
	<artifactId>jte</artifactId>
	<version>${jte.version}</version>
</dependency>
<dependency>
	<groupId>gg.jte</groupId>
	<artifactId>jte-spring-boot-starter-3</artifactId>
	<version>${jte.version}</version>
</dependency>
<form hx-post="/login-process" hx-ext="json-enc" novalidate>
    <div class="space-y-5">
        <div>
            <label for="hs-user" class="block mb-2 text-sm font-medium text-gray-800 dark:text-white">
                Username
            </label>
             <input type="text" id="hs-user" name="username"
                         class="py-2.5 px-3 block w-full border border-gray-200 rounded-lg text-sm placeholder:text-gray-400 focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-transparent dark:border-neutral-700 dark:text-neutral-300 dark:placeholder:text-white/60 dark:focus:ring-neutral-600"
                          placeholder="" autofocus required>
             </div>
             <div>
                  <div class="flex justify-between items-center mb-2">
                       <label for="hs-pwd" class="block text-sm font-medium text-gray-800 dark:text-white">
                            Password
                       </label>
                  </div>
              <div class="relative">
                   <input id="hs-pwd" type="password" name="password"
                               class="py-2.5 px-3 block w-full border border-gray-200 rounded-lg text-sm placeholder:text-gray-400 focus:border-blue-500 focus:ring-blue-500 disabled:opacity-50 disabled:pointer-events-none dark:bg-transparent dark:border-neutral-700 dark:text-neutral-300 dark:placeholder:text-white/60 dark:focus:ring-neutral-600"
                               placeholder="" required>
                  <button type="button" data-hs-toggle-password='{"target": "#hs-pwd"}'
                                class="absolute inset-y-0 end-0 flex items-center z-20 px-3 cursor-pointer text-gray-400 rounded-e-md focus:outline-none focus:text-blue-600 dark:text-neutral-600 dark:focus:text-blue-500">
                      <svg class="shrink-0 size-4" width="24" height="24" viewBox="0 0 24 24" fill="none"
                                stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                stroke-linejoin="round">
                          <path class="hs-password-active:hidden" d="M9.88 9.88a3 3 0 1 0 4.24 4.24"/>
                          <path class="hs-password-active:hidden"
                                     d="M10.73 5.08A10.43 10.43 0 0 1 12 5c7 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68"/>
                          <path class="hs-password-active:hidden"
                                     d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3 7 10 7a9.74 9.74 0 0 0 5.39-1.61"/>
                              <line class="hs-password-active:hidden" x1="2" x2="22" y1="2" y2="22"/>
                                    <path class="hidden hs-password-active:block"
                                          d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/>
                                    <circle class="hidden hs-password-active:block" cx="12" cy="12" r="3"/>
                      </svg>
                  </button>
             </div>
     </div>
     <button type="submit" style="margin-top: 32px"
                   class="py-2.5 px-3 w-full inline-flex justify-center items-center gap-x-2 text-sm font-semibold rounded-lg border border-transparent bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50 disabled:pointer-events-none dark:focus:outline-none dark:focus:ring-1 dark:focus:ring-neutral-600">
            LOGIN
       </button>
    </div>
</form>
    @HxRequest
    @PostMapping("/login-process")
    public String loginProcess(@RequestBody @Validated AdminLoginDto input, HttpServletRequest request, HtmxResponse htmxResponse, Model model) {
        try {
            UserDetails userDetails = loginService.getUserDetails(input);
            Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, userDetails.getPassword(), userDetails.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authentication);
            HttpSession session = request.getSession(true);
            session.setAttribute("SPRING_SECURITY_CONTEXT", SecurityContextHolder.getContext());
            htmxResponse.setReswap(HtmxReswap.none());
            return "redirect:htmx:/home";
        } catch (Exception e) {
            log.warn("[loginProcess] |- {}", e.getMessage());
            String message = "Login Failed";
            if (e instanceof ServiceException) {
                message = e.getMessage();
            }
            htmxResponse.setReswap(HtmxReswap.none());
            htmxResponse.setRetarget("#toast-error");
            model.addAttribute("message", message);
            return "toast/error";
        }
    }

Chrome console

Access to XMLHttpRequest at 'htmx:/home' (redirected from 'http://localhost:8080/login-process') from origin 'http://localhost:8080' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: chrome, chrome-extension, chrome-untrusted, data, http, https, isolated-app.

htmx.min.js:1 htmx:afterRequest
htmx.min.js:1 htmx:sendError
htmx.min.js:1 GET htmx:/home net::ERR_FAILED

It seems "redirect:htmx:/home" not work correctly.
Any suggestion on this?

@xhaggi
Copy link
Collaborator

xhaggi commented Feb 19, 2025

Could you please run your Spring Boot app with --debug and check if the HtmxMvcAutoConfiguration is loaded?

@wimdeblauwe wimdeblauwe added the Waiting for Response Waiting for response label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Waiting for Response Waiting for response
Projects
None yet
Development

No branches or pull requests

3 participants