如何用 Spring 引导安全功能替换基本身份验证?

How to replace Basic auth with Spring Boot security feature?

目前我们使用的是Basic auth,并将加密格式的用户名和密码设置到HttpHeaders。有什么方法可以 remove/replace 使用任何 Springboot 安全性吗?如果是的话,你能帮我在我的项目中实现这个吗?

  • 您能否按照步骤从 spring 中删除现有的基本身份验证 开机
  • 在 spring 引导中,我们可以通过配置实用程序启用 BasicAuth class 哪个 WebSecurityConfigurerAdapter,当你配置它时,你 将允许在访问任何已配置的 URL 之前获得身份验证 (或所有网址)在您的应用程序中。

步骤

  1. 您必须删除扩展的 WebSecurityConfigurerAdapter class

我会附上它的样子


@Configuration
public class EnablingSecutiry extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http
         .csrf().disable()
         .authorizeRequests().anyRequest().authenticated()
         .and()
         .httpBasic();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception
    {
        auth.inMemoryAuthentication()
            .withUser("admin")
            .password("{noop}password")
            .roles("USER");
    }
}

步骤

  1. 从 POM 文件中删除依赖项
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>