SpringSecurity用户认证-自定义登录页面三

自定义设置登录页面不需要认证就可以访问

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<from action="/user/login" method="post">
    用户名:<input type="text" name="username"/>
    <br/>
    密码:<input type="text" name="password"/>
    <br/>
    <input type="submit" value="login"/>
</from>
</body>
</html>

 SpringSecurity用户认证-自定义登录页面三

 

上一篇:SpringSecurity入门案例与基本原理


下一篇:SpringSecurity默认用户名密码从哪来,为什么要写UserDetails...