springsecurity设置登录页

自定义登录页需要满足两点

1.登录页面所对应的html文件中的表单属性中,有username和password。

2.登录页面所对应的html文件中的表单属性的提交地址要与配置类中的loginProcessingUrl相同。

 

 

mylogin.html

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

配置类

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin() //表单登录验证,开启后默认为/login
                .loginProcessingUrl("/login123") //登录提交的url,要与login.html的action对应
                .loginPage("/mylogin") //登录的页面,被拦截时会自动到这里,默认被拦截时回到/login(没有登录的前提下)
                .successForwardUrl("/index"); //登录成功的路径 默认是跳转到你一开始输入的目标页面,如过登录的账号权限不够则403

        http.authorizeRequests()
                //允许login被所有人访问
                .antMatchers("/mylogin").permitAll()
                //其他所有请求要被验证
                .anyRequest().authenticated();
        //跨域问题
        http.csrf().disable();
    }

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
posted @ 2022-04-24 20:19  无极是一种信仰  阅读(823)  评论(0)    收藏  举报