Logout link with Spring and Thymeleaf
Asked Answered
D

7

27

According with the official example (Secure Web Content), I have to use a form and a button with the aim to perform a logout with Spring Security. Is there a way to use a link with Thymeleaf instead of a button?

Deafening answered 21/3, 2014 at 11:39 Comment(2)
Style the button as a link. It needs, for security reasons, to be a POST instead of a GET request. You can also configure the logout filter to accept get requests but that is less secure.Jewett
Also see docs.spring.io/spring-security/site/docs/3.2.x/reference/…Snakebird
G
37

You have to use a form for log out. If you really want a link, you can use JavaScript to have the link perform a POST on a hidden form.

<a href="javascript: document.logoutForm.submit()" role="menuitem"> Logout</a>

   <form name="logoutForm" th:action="@{/logout}" method="post" th:hidden="true">
      <input hidden type="submit" value="Sign Out"/>
   </form> 
Greggs answered 6/4, 2016 at 10:52 Comment(1)
Note that I needed to change the 'hidden' attribute of the 'input' element to 'hidden="true"'Slipshod
S
29

I have successfully used <a th:href="@{/logout}">Logout</a>

The relevant Spring Security config I used was

 http
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/login");
Suntan answered 21/3, 2014 at 12:10 Comment(2)
This didn't work for me as <a th:href="@{/logout}">Logout</a> generated get requestChainman
Check out this for the configuration, and then just change the html/thymeleaf anchor to a formSuntan
C
18

With respect to the context of this question I think vdenotaris wants a link not a submit button for the log out functionality. well I think what you can do is create a hyperlink like this :

<a href="#" th:href="@{/logout}">Log Out</a>

and now create a controller with below mapping :

@RequestMapping(value="/logout", method = RequestMethod.GET)
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null){    
        new SecurityContextLogoutHandler().logout(request, response, auth);
    }
    return "redirect:/login?logout";
}
Charissacharisse answered 3/4, 2017 at 6:50 Comment(1)
this block of code is actually working. a little explanation on how it is working might help other viewers.Pirzada
D
10

The solution (deprecated!) is:

       .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/login");

It is recommended to use POST instead of a GET request for security, as mentioned above.

Deafening answered 26/3, 2014 at 10:53 Comment(3)
This worked for me. need to find out the way to do it in postChainman
@Deafening Can you please explain why it is DEPRECATED? And should I use it or not?Martijn
@Martijn Because this implementation handles the logout process via HTTP GET instead of POST, as stated in my answer. See: docs.spring.io/spring-security/site/docs/current/reference/…Deafening
H
8

Thats the right answer.

<form th:action="@{/logout}" method="post">
    <input type="submit">POST LOGOUT</input>
</form>
Heptagonal answered 1/11, 2015 at 18:32 Comment(1)
if "Is there a way to use a link with Thymeleaf instead of a button?" is the question, how is using a button the right answer?Panek
Q
4

"In order to help protect against CSRF attacks, by default, Spring Security Xml Configuration log out requires:

  • the HTTP method must be a POST
  • the CSRF token must be added to the request. You can access it on the ServletRequest using the attribute _csrf as illustrated above."

Hello Spring Security Xml Config

<form th:action="@{/logout}" method="post">
    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" /> 
    <input type="submit">LOGOUT</input>
</form>
Quadruple answered 7/6, 2017 at 19:31 Comment(0)
V
0

Try adding the following snippet of code in your file:

  <!--In case of csrf enabled post logout is working -->

  <form th:action="@{/logout}" method="post">
    <input type="submit" value="POST LOGOUT"></input>
  </form>
Vision answered 3/2, 2022 at 12:23 Comment(1)
Using thymeleafVision

© 2022 - 2024 — McMap. All rights reserved.