自定义设置登录页面
1.在配置类实现相关的配置,某些路径不需要认证也可以访问
@Override
protected void configure(HttpSecurity http) throws Exception {
//自定义自己编写的登录页面
http.formLogin()
.loginPage("/login.html")//登录页面设置
.loginProcessingUrl("/user/login")//登录访问路径
.defaultSuccessUrl("/test/index")//登录成功,跳转的路径
.permitAll()//允许所有请求
//可以定义某些方法不需要认证即可访问
.and().authorizeRequests()
.antMatchers("/", "/test/hello", "/user/login").permitAll()//设置哪些路径可以直接访问,不需要认证
.anyRequest().authenticated()//所有请求都可以访问
.and().csrf().disable();//关闭csrf防护
}
2.创建相关页面,Controller
创建login.html登录页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/user/login" method="post">
用户名:<input type="text" name="username"/>
<br/>
密码:<input type="text" name="password"/>
<br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
通过源码分析这里的name必须叫username,password,以及post请求,否则Security是得不到你提交过来的用户名和密码
创建controller
@GetMapping("index")
public String index() {
return "hello index";
}
不需要可以直接访问的路径
需要认证的路径,以及自定义的登录界面