第一步: 在resources/templates下创建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h2>首页</h2>
<a href="openLogin">登录</a>
<a href="userLogout">退出登录</a>
</body>
</html>
第二步:在resources/templates下创建login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="userLogin" method="post">
<input name="username" value="zhangsan"><br>
<input name="password" value="1234"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
第三步:修改SpringSecurity配置文件
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/openLogin")
.loginProcessingUrl("/userLogin")
.and()
.authorizeRequests()
.antMatchers("/openLogin").permitAll()
.antMatchers("/openIndex").permitAll()
.anyRequest().authenticated()
.and()
.logout()
.logoutUrl("/userLogout")
.logoutSuccessUrl("/openIndex")
.and()
.csrf().disable();
}
}
结果
![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/9c697fe9a90079255427c6b94af60f8f.gif)