Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Aplicação com Spring Security retornando não encontrado para Logout

Estou tentando, seguindo o projeto, Spring MVC Security, realizar logout da aplicação com uso do método HttpSecurity.logout sem sucesso. O método de login funciona, apenas o logout, que não realiza o logout e redireciona para página de erro.

WebSecurityConfig.java

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    DataSource datasource;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().anyRequest().authenticated().
        and().formLogin( form -> form.loginPage("/login").permitAll()).logout(logout -> {
            logout.logoutUrl("/logout")
            .logoutSuccessUrl("/home");
    });        
    }    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception 
    {        
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();        

        UserDetails user = User.builder()
        .username("admin")
        .password(encoder.encode("admin"))
        .roles("USER")
        .build();

        auth.jdbcAuthentication().dataSource(datasource).passwordEncoder(encoder).withUser(user);        
    }

base.html

    <head th:fragment="head">
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">    
    <link href="https://fonts.googleapis.com/css2?family=Handlee&display=swap" rel="stylesheet">
    <style>
        .logo-container {
            background-color: #47bcff;
            color: #FFF;
        }
        .logo {
            font-family: 'Handlee', cursive;
            font-size: 2.5rem;
        }
    </style>

<head th:fragment="head">
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">    
    <link href="https://fonts.googleapis.com/css2?family=Handlee&display=swap" rel="stylesheet">
    <style>
        .logo-container {
            background-color: #47bcff;
            color: #FFF;
        }
        .logo {
            font-family: 'Handlee', cursive;
            font-size: 2.5rem;
        }
    </style>
</head>

<div th:fragment="logo" class="logo-container mb-3 p-3 d-flex justify-content-between">
    <span class="logo">mudi</span>
    <span class="mt-3">
    <a class="text-light" sec:authorize="!isAuthenticated()" href="/login">login</a>    
    <a onclick="document.querySelector('#form-login').submit(); alert('Teste');" class="text-light" sec:authorize="isAuthenticated()" href="/logout">logout</a>    
    <form id="form-login" th:action:"@{/logout}" method="POST"/>
    </span>
</div>    

<div th:fragment="titulo(valor)" class="jumbotron mb-0">
    <h1 class="display-4" th:text="${valor}"></h1>
</div>

index.html

<html>
<meta charset="UTF-8" />
<head th:replace="~{base :: head}"></head>
<body>
    <div th:replace="~{base :: logo}"></div>

    <div class="container">
        <div th:replace="~{base :: titulo('Login')}"></div>


        <div class="card mb-3">
            <form th:action="@{/login}" class="card-body" method="post">
                <div class="form-group">
                    <label for="username"> Usuário </label> <input name="username"
                        class="form-control" placeholder="usuário" />
                </div>
                <div class="form-group">
                    <label for="senha"> Senha </label> <input name="password"
                        class="form-control" placeholder="senha" />

                    <button class="btn btn-primary" type="submit">Login</button>
                </div>
            </form>
        </div>
    </div>
</body>
</html>

Retornando no navegador:

*Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Nov 24 02:24:05 BRT 2021 There was an unexpected error (type=Not Found, status=404). No message available

1 resposta
solução!

Resolvido, a sintaxe estava incorreta. Favor excluir tópido.