//链式编程-- 授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的页面
http.authorizeRequests()
.antMatchers("/").permitAll() //首页所有人访问
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认到登陆页,开启登陆页
http.formLogin();
http.httpBasic();
//防止网站攻击工具:get易受攻击且是csrf攻击,改为post
//新版本不用关闭
//http.csrf().disable(); //springboot默认开启csrf是正确的防止跨站请求攻击,
// 低版本的springboot需要关闭csrf,登陆失败存在的原因 或者设置为post方式注销
//开启注销功能,注销跳到首页
http.logout().logoutSuccessUrl("/");
//开启记住我功能,将登陆信息存客户端是cookie,默认2周,服务端是session
//要清除的话浏览器的cookie、session都要清除
http.rememberMe();
}

//没有权限默认到登陆页,开启登陆页
//loginPage自定义login页面
//安全框架默认登陆传输的是username、password,若前端登陆页面的表单元素的name不是username、password,
// 就无法给安全框架传值,导致登陆失败,所以要用usernameParameter、passwordParameter自定义表单元素的name http.formLogin().loginPage(“/toLogin”).usernameParameter(“user”).passwordParameter(“pwd”).loginProcessingUrl(“/login”);
//链式编程-- 授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的页面
http.authorizeRequests()
.antMatchers("/").permitAll() //首页所有人访问
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认到登陆页,开启登陆页
//loginPage自定义login页面
//安全框架默认登陆传输的是username、password,若前端登陆页面的表单元素的name不是username、password,
// 就无法给安全框架传值,导致登陆失败,所以要用usernameParameter、passwordParameter自定义表单元素的name
http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");
http.httpBasic();
//防止网站攻击工具:get易受攻击且是csrf攻击,改为post
//新版本不用关闭
//http.csrf().disable(); //springboot默认开启csrf是正确的防止跨站请求攻击,
// 低版本的springboot需要关闭csrf,登陆失败存在的原因 或者设置为post方式注销
//开启注销功能,注销跳到首页
http.logout().logoutSuccessUrl("/");
//开启记住我功能,将登陆信息存客户端是cookie,默认2周,服务端是session
//要清除的话浏览器的cookie、session都要清除
http.rememberMe();
}
<·form th:action=“@{/login}” method=“post”>
要走安全框架的login授权认证,需要在http.formLogin().loginPage(“/toLogin”).loginProcessingUrl(“/login”);配置安全框架登陆的映射路径
登录
登录
注册
...
Spring Security Study by zk
//安全框架默认登陆传输的是username、password,若前端登陆页面的表单元素的name不是username、password, // 就无法给安全框架传值,导致登陆失败,所以要用usernameParameter、passwordParameter自定义表单元素的name
- 1
- 2
可以用http.formLogin().loginPage(“/toLogin”).usernameParameter(“user”).passwordParameter(“pwd”)来自定义登陆控件name
记住我设置,在SecurityConfig配置类的授权方法中configure中给http.rememberMe()补充用rememberMeParameter自定义接受前端参数
默认记住我选择控件的name是remember-me
//开启记住我功能,将登陆信息存客户端是cookie,默认2周,服务端是session
//要清除的话浏览器的cookie、session都要清除
//用rememberMeParameter("remember-me")自定义接受前端参数
//默认remember-me
http.rememberMe().rememberMeParameter("remember-me");
记住我
