Em chào mọi người, em mới làm quen với spring security . Em bị vướng một chổ là khi em .loginPage("/login") nó không trả tới trang login của em làm mà lại trả sang trang login khác, em cảm ơn mọi người
Đây là WebSecurityConfig của em:
package com.giadat.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService userDetailsService;
@Bean
public PasswordEncoder passwordEncoder()
{
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth ) throws Exception
{
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/").hasRole("MEMBER")
.antMatchers("/admin").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.usernameParameter("email")
.passwordParameter("password")
.defaultSuccessUrl("/")
.failureUrl("/login?error")
.and()
.exceptionHandling()
.accessDeniedPage("/403");
}
}
Đây là trang login do em làm:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Login Page</title>
<link href="../static/css/style.css" th:href="@{/css/style.css}" rel="stylesheet" />
</head>
<body>
<div class="main-content">
<p th:if="${param.error}" class="error">Invalid email or password</p>
<p th:if="${param.logout}" class="success">You have been logged out</p>
<h3>Login to continue</h3>
<form th:action="@{/login}" method="POST">
<input type="text" name="user" placeholder="Your email" /><br />
<input type="password" name="password" placeholder="Your password" /><br />
<button type="submit">Login</button> <br />
</form>
</div>
</body>
</html>
Đây là form login mà nó trả sang: