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"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值