A tela do controller é essa
package br.com.alura.mvc.mudi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@GetMapping
@RequestMapping("/login")
public String login(){
return "login";
}
}
A tela login.html é essa
<html xmlns:th="http://www.w3.org/1999/xhtml">
<!--th:replace="~{}" PARA O THYMELEAF GERENCIAR O ACESSO A FRAGMENTOS DE TELA-->
<head th:replace="~{base :: head}">
</head>
<body>
<div th:replace="~{base :: logo}"></div>
</div>
<div class="container">
<div th:replace="~{base :: titulo('Login')}"></div>
<div class="card mb-3">
<form th:action="@{/login}" 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="password">Senha</label>
<input type="password" name="password" class="form-control" placeholder="senha"/>
</div>
<button class="btn btn-primary" type="submit">Cadastrar</button>
</form>
</div>
</div>
</body>
</html>
O web security é esse
package br.com.alura.mvc.mudi.SECURITY;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
/*TODOS OS REQUESTS DEVEM ESTAR AUTTENTICADOS*/
.anyRequest().authenticated()
.and()
.formLogin(form -> form
.loginPage("/login")
.permitAll());
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("diego")
.password("diego")
.roles("ADM")
.build();
return new InMemoryUserDetailsManager(user);
}
}
O problema é o seguinte: Não importa o que eu faça, quando eu reseto a aplicação, subo de novo, ele simplesmente entra nna /home sem passar pela autenticação. Outro problema que acontece é que na barra /login ele não autentica e da erro
segue o erro
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Apr 14 15:17:17 BRT 2022 There was an unexpected error (type=Not Found, status=404). No message available
Enfim, tudo isso acontece durante a aula Página de Login se alguém conseguir me dar um help.
Toda vez que eu quero fazer o login na pagina eu preciso apagar o cache do navegador, e nao é porque eu não estou utilizando o navegador anonimo, e se eu não faço isso, quando eu faço o logout segue imagem do erro