3、如何配置http的属性?

本文介绍了如何在Spring Security框架中创建自定义配置类WebSecurityConfig,覆盖默认的登录认证设置。通过子类化WebSecurityConfigurerAdapter,设置所有请求需要认证,修改登录表单的用户名和密码参数,禁用CSRF保护,并启用HTTP Basic认证。读者可以学习到如何精细化控制Spring Security的安全设置以适应特定需求。

创建自定义 security 配置类 WebSecurityConfig 如下:

//@EnableWebSecurity(debug = false)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
    }
}

super.configure(http);中使用的默认配置为父类中的

protected void configure(HttpSecurity http) throws Exception {
    this.logger.debug("Using default configure(HttpSecurity). "
                      + "If subclassed this will potentially override subclass configure(HttpSecurity).");
    http.authorizeRequests((requests) -> requests.anyRequest().authenticated());//所有请求需要进行认证
    http.formLogin();//开启form表单登录
    http.httpBasic();//开启 Basic Auth 登录
}

那我们可以覆盖父类的配置,比如这样

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
        http.formLogin().usernameParameter("loginName").passwordParameter("passwd");//将登录用户名和密码改成我么需要的
        http.httpBasic();
        http.csrf().disable();//关闭csrf认证
    }
}

使用post登录
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值