get请求 不固定参数名_[SpringSecurity] 设置请求账户和密码的参数名

4e07ceab5fbf9fb30e185b7b0187c1c2.png

当进行登录时会执行

UsernamePasswordAuthenticationFilter 过滤器。

usernamePasrameter:账户参数名

passwordParameter:密码参数名

postOnly=true:默认情况下只允许 POST 请求。

默认情况下 登录名/密码和默认请求

438c58d6f003e1f33f7cc01056d802ed.png

表单也必须是同名

ddef740c1526c20c05b2c9844a29257d.png

如果要修改默认的请求参数,就需要这么做

1.修改配置类

549defc42dc191a7fdf768ac6e148f1d.png

2.修改表单name属性

73ab3140c4c5ed9fbfc88e9ed4d1a209.png
package com.lin.springsecurity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author ccgg
 * @Date 2020/8/18 22:56
 * 创建 PasswordEncoder 实例,在spring启动时,注入容器中
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        System.out.println(1);
        //表单认证
        http.formLogin()
                //当发现login时认为是登录需要执行我们自定义的登录逻辑 >里面的url是登录页面表单的提交地址
               .loginProcessingUrl("/login")
                //登录成功后请求地址 请求方法必须是post的 这里是跳转控制器
               .successForwardUrl("/toMain")
                //登录失败后请求访问的地址 >这里访问的是控制器
               .failureForwardUrl("/failLogin")
                //设置登录页面
                .loginPage("/login.html")
                .usernameParameter("uuname")
                .passwordParameter("ppword")
        ;


        //url拦截认证  >所有请求都必须被认证 必须登录后才可以访问
        http.authorizeRequests()
                //设置不需要拦截的页面
                .antMatchers("/login.html").permitAll()
                .antMatchers("/fail.html").permitAll()
                 //所有请求都必须被认真,必须登录后才能访问
                .anyRequest().authenticated();

        //关闭csrf防护 >只有关闭了,来自表单的请求
        http.csrf().disable();
    }

    /**
     * 创建加密对象
     * @return
     */
    @Bean
    public PasswordEncoder getPE(){
        return new BCryptPasswordEncoder();
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值