Spring Security中的表单登录

前言

本文将介绍,在spring security中最基本的form表单登录,以及一些相关的配置。

自定义表单登录

如果我们不进行配置的话,spring security默认的登录页和登录接口都是/login,只不过一个是get请求一个是post请求而已。

get http://localhost:8080/login.html 访问页面
post http://localhost:8080/login.html 提交form数据

我们也可以把它们单独定义出来如下图中:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("xiaoming")
                .password("123456").roles("admin");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");  //这个是用来忽略一些url地址,对其不进行校验,通常用在一些静态文件中。
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")//登录页面地址(如果不指定loginProcessingUrl,它就代表登录页面地址和登录接口地址都是/login.html)
                .loginProcessingUrl("/loginTest")//用于指定登录接口地址(对应的页面form表单中的action对应的值也要修改)
                .permitAll()//相关接口不要被拦截
                .and()//相当于一个标签结束符号,表示上一段配置,之后开启新的配置
                .csrf().disable(); //关闭csrf
    }
}

对应的页面form表单中的action对应的值也要修改

 <form action="/loginTest.html" method="post">

参数配置

如果没有对登录参数进行配置的话,默认的情况下form表单中username和password这两个不能变

		<form action="/loginTest" method="post">
                <label for="name">用户名</label>
                <input type="text" name="username" id="name">
                <label for="pass">密码</label>
                <input type="password" name="password" id="pass">
        </form>

如果想要变更的话我们也许需要在配置文件中进行如下配置:

 @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定义用户名属性值
                .passwordParameter("mima")//自定义密码的属性是
                .permitAll()//相关接口不要被拦截
                .and()//相当于一个标签结束符号,表示上一段配置,之后开启新的配置
                .csrf().disable(); //关闭csrf
    }

前端页面对应的也相应的修改即可

  <form action="/loginTest" method="post">
                <label for="name">用户名</label>
                <input type="text" name="yonghuming" id="name">
                <label for="pass">密码</label>
                <input type="password" name="mima" id="pass">
        </form>

登录后回调方法

成功回调
成功回调的方式有两种:

defaultSuccessUrl
successForwardUrl

上代码:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定义用户名属性值
                .passwordParameter("mima")//自定义密码的属性是
                .successForwardUrl("/index")//设置成功登录跳转的页面,和下面的defaultSuccessUrl作用是相同的,两则在使用的时候选择其中一个就可以了
                .defaultSuccessUrl("/index",true)
                .permitAll()//相关接口不要被拦截
                .and()//相当于一个标签结束符号,表示上一段配置,之后开启新的配置
                .csrf().disable(); //关闭csrf
    }

失败回调

failureForwardUrl
failureUrl

上代码:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定义用户名属性值
                .passwordParameter("mima")//自定义密码的属性是
                .successForwardUrl("/index")//设置成功登录跳转的页面,和下面的defaultSuccessUrl作用是相同的,两则在使用的时候选择其中一个就可以了
                //.defaultSuccessUrl("/index",true)
                //.failureUrl("loginfail")//登录失败跳转页面(重定向)同样和下面的两则选择一个使用即可
                .failureForwardUrl("loginfail")//登录失败跳转页面(服务端跳转)
                .permitAll()//相关接口不要被拦截
                .and()//相当于一个标签结束符号,表示上一段配置,之后开启新的配置
                .csrf().disable(); //关闭csrf
    }

登出

logout
logoutRequestMatcher
logoutSuccessUrl
deleteCookies

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/loginTest")
                .usernameParameter("yonghuming")//自定义用户名属性值
                .passwordParameter("mima")//自定义密码的属性是
                .successForwardUrl("/index")//设置成功登录跳转的页面,和下面的defaultSuccessUrl作用是相同的,两则在使用的时候选择其中一个就可以了
                //.defaultSuccessUrl("/index",true)
                //.failureUrl("loginfail")//登录失败跳转页面(重定向)同样和下面的两则选择一个使用即可
                .failureForwardUrl("/loginfail")//登录失败跳转页面(服务端跳转)
                .and()
                //.logout() //默认的注销url是/logout
                //.logoutUrl("/exit") //可以指定登出的url
                .logoutRequestMatcher(new AntPathRequestMatcher("/exist","post"))  //不仅可以指定登出url还可以指定请求方式
                .logoutSuccessUrl("/index")//登出成功后跳转的页面
                .deleteCookies() //清楚cookie
                .permitAll()//相关接口不要被拦截
                .and()//相当于一个标签结束符号,表示上一段配置,之后开启新的配置
                .csrf().disable(); //关闭csrf
    }

总结

本文介绍了简单的表单登录,以及登录成功后的一些回调方法的使用和配置。

你可能感兴趣的:(Spring,Security,spring,spring,boot,后端,java)