http.formLogin() //自定义自己编写的登录页面
.loginPage("/login.html") //登录页面设置
.loginProcessingUrl("/user/login") //登录访问路径
.defaultSuccessUrl("/main.html").permitAll() //登录成功之后,跳转路径
.and().authorizeRequests()
.antMatchers("/test/hello", "/usr/login").permitAll() //设置那些路径可以直接访问,不需要认证
//.antMatchers("/user/demo","/usr/test").hasAuthority("admins") //方法2
// .antMatchers("/user/demo","/usr/test").hasAnyAuthority("admins","users") //方法3
.antMatchers("/user/demo").hasRole("sale")
.anyRequest().authenticated()
.and().rememberMe().tokenRepository(persistentTokenRepository())
.tokenValiditySeconds(60) //60秒
.userDetailsService(userDetailsService);
// http.and().csrf().disable(); //关闭CSRF防护,也是一种认证方式
http.and().csrf().disable(); 这句直接注释了
html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="_csrf" content="${_csrf.token}"/>
<meta name="_csrf_header" content="${_csrf.headerName}"/>
<title>Title</title>
</head>
<body>
<form action="/user/login" method="post">
<input type="hidden" th::name="${_csrf.parameterName}" th::value="${_csrf.token}"/>
用户名:<input type="text" name="username"/><br/>
密码: <input type="password" name="password"/><br/>
记住我:<input type="checkbox" name="remember-me" value="true"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
如果不注释http.and().csrf().disable(); 并且Html 不加 <input type="hidden" />
就可以成功登录,可是如果按照上面的写法运行后就无法成功跳转main.html, 总是在login.html上