【springSecurity6】用户名密码登陆校验,后台只能收到用户名字段,收不到密码pwd字段

文章讲述了在使用SpringSecurity时,由于密码字段名称不规范导致后台无法接收到密码的问题,通过修改`passwordParameter`配置解决了问题,强调了命名规范的重要性。
摘要由CSDN通过智能技术生成

项目场景:

想要实现 前台输入用户名密码,后台进行认证,然后登陆成功跳转功能

前提:用户名字段username,密码是pwd(问题就在这,跟往常password不同)

问题:前台输入用户名和密码之后,后台只能接收到用户名,接收不到前台输入的密码(pwd)字段

血泪教训:取名尽量还是要规范,不然会导致框架识别不到这个字段的值


问题描述

前台输入用户名和密码之后,后台只能接收到用户名,接收不到前台输入的密码(pwd)字段
在这里插入图片描述
在这里插入图片描述
可以看到,前台肯定是已经传入了参数,但是后台就是识别不到。


原因分析:

应该是speingsecurity默认识别的密码,是按照password来识别的,所以识别不到。需要通过springsecurityConfig里面的配置进行修改,覆盖默认的字段名

下面是没设置时候的代码:

    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        //首页所有人都可以访问,功能页只有有权限的人才能访问
        httpSecurity
                .authorizeHttpRequests((requests) -> requests
                                .requestMatchers("/","/index").permitAll()
                                .requestMatchers("/toLogin","/toLoginPage").permitAll()
                                .requestMatchers("/level1/**","/level2/**","/level3/**").hasRole("root")
                                .requestMatchers("/level2/**").hasRole("admin")
                                //对静态资源的过滤
                                .requestMatchers("/css/**","/js/**","/views/**").permitAll()
                                .requestMatchers("/views/login.html").permitAll()
                                .anyRequest().authenticated()
                        //没有权限默认回到登陆页面,也可以不传参数,默认是login.html页面
                ).formLogin(form -> form.loginPage("/toLoginPage")
                //处理前端跳转页面,要跟form表单里的action相同
                        .loginProcessingUrl("/toLogin")
                .csrf(csrf -> csrf.disable());
        return httpSecurity.build();
    }

在这里插入图片描述

可以看到,在FormLoginConfigurer.java里面,默认接收密码的参数是passwordParameter


解决方案:

通过.passwordParameter(“pwd”)通过这个指定了密码对应的参数是pwd,完成设置之后就可以看到,接收到参数了

    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        //首页所有人都可以访问,功能页只有有权限的人才能访问
        httpSecurity
                .authorizeHttpRequests((requests) -> requests
                                .requestMatchers("/","/index").permitAll()
                                .requestMatchers("/toLogin","/toLoginPage").permitAll()
                                .requestMatchers("/level1/**","/level2/**","/level3/**").hasRole("root")
                                .requestMatchers("/level2/**").hasRole("admin")
                                //对静态资源的过滤
                                .requestMatchers("/css/**","/js/**","/views/**").permitAll()
                                .requestMatchers("/views/login.html").permitAll()
                                .anyRequest().authenticated()
                        //没有权限默认回到登陆页面,也可以不传参数,默认是login.html页面
                ).formLogin(form -> form.loginPage("/toLoginPage")
                //处理前端跳转页面,要跟form表单里的action相同
                        .loginProcessingUrl("/toLogin")
                        .passwordParameter("pwd")
                        .usernameParameter("username"))
                .csrf(csrf -> csrf.disable());
        return httpSecurity.build();
    }
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值