Pessoal, não estou sendo redirecionado automaticamente para a página home após efetuar login. É retornado que há um erro no mapeamento. Tive que colocar "defaultSuccessUrl("/home")" para conseguir direcionar para a home.
Além disso, o sec:authorize="isAuthenticated()" não está funcionando. Aparece no topo da página o "login" e "logout".
Minha WebSecurityConfig:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin(form -> form
.loginPage("/login")
.permitAll().defaultSuccessUrl("/home")
);
}
@Bean
@Override
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("vitor")
.password("vitor")
.roles("ADM")
.build();
return new InMemoryUserDetailsManager(user);
}
}
Meu pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.alura.mvc</groupId>
<artifactId>mudi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mudi</name>
<description>Projeto Spring MVC da Alura</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
meu LoginController
@Controller
public class LoginController {
@GetMapping
@RequestMapping("/login")
public String login() {
return "login";
}
}E meu base.html:
<head th:fragment="head">
<meta charset="UTF8" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link
href="https://fonts.googleapis.com/css2?family=Handlee&display=swap"
rel="stylesheet">
<style>
.logoContainer {
background-color: #47bcff;
color: #fff
}
.fonteLogo {
font-family: 'Handlee', cursive;
font-size: 2.5rem;
}
</style>
</head>
<div th:fragment="cabecalho" class="logoContainer mb-3 p-3 d-flex justify-content-between">
<span class="fonteLogo">mudi</span>
<span class="mt-3">
<a class="text-light" sec:authorize="!isAuthenticated()"href="/login">login</a>
<a class="text-light" sec:authorize="isAuthenticated()" href="/login">logout</a>
</span>
</div>
<div th:fragment="titulo(valor)" class="jumbotron">
<h1 class="display-4" th:text="${valor}"></h1>
</div>