接上文Springboot+SpringSecurity权限控制学习笔记(二)
把WebSecurityConfig中的@Configuration注解注释掉,相当于删掉此配置类。再启动项目访问home.html,系统会自动跳转到一个登录页面,这是SpringSecurity默认的登录界面。输入application.properties文件中自定义的用户名admin、密码123456,一样可以登录系统,可以访问所有资源。我们也可以使用自定义的登录界面……
详解FormLogin表单登录模式
什么是FormLogin登录模式?
就是自定义登录界面,并配置访问规则。
如何实现
把WebSecurityConfig类中@Configuration行注释打开,改写成如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http);
//以下内容注释掉
/** 开启HttpBasic认证
http.httpBasic()
//链式编程,返回Http
.and()
//认证所有的请求
.authorizeRequests()
//任何请求都必须authenticated
.anyRequest()
.authenticated();*/
//以下为改造部分
//所有请求都被认证,拦截,包括登录和静态文件
http.authorizeRequests()
//添加放行
.antMatchers("/login","/login.html").permitAll()
.anyRequest()
.authenticated()
.and()
//设置登录页面
.formLogin().loginPage("/login.html")
//设置默认登录验证控制方法,默认是login
.loginProcessingUrl("/login")
//用户名和密码表单变量名(name值)
.usernameParameter("username").passwordParameter("password")
//登录成功后的界面
.defaultSuccessUrl("/home");
//关闭跨域攻击
http.csrf().disable();
}
重启项目,访问http://localhost:8080/home就会弹出我们自定义的登录界面,输入用户名和密码就可以访问home页面中的链接。各命名的对应关系