java - 使用基本身份验证保护 Spring Boot REST 应用程序

标签 java spring spring-mvc authentication spring-security

我查阅了有关使用 Spring Security 的文章,但它们并没有完全满足我的需求。它们通常处理内存中的用户和明文密码。

我想要实现的目标:

客户端使用基本身份验证进行身份验证。

安全 Controller 方法如下所示:

  @RequestMapping(value = "/test")
  public ResponseEntity test(@AuthenticationPrincipal MyUser user){
      // Logic
  }

如果给定用户已通过身份验证,我希望 Controller 方法在其参数中获取 MyUser 对象,否则为 null。

我将哈希密码和用户的盐存储在数据库中,并且我有一项服务可以对给定的用户名-密码对进行身份验证,并在身份验证成功或失败时分别返回用户对象或 null。

现在需要做的就是拦截每个请求,查看基本身份验证 header ,将信息传递到我的自定义身份验证服务,然后将其结果传递到 Controller 方法中。不幸的是,我没能做到这一点。


我阅读了有关使用自定义 UserDetailsS​​ervice 的内容,但这似乎只处理从数据库中获取用户数据,而不处理任何身份验证。我不知道如何将所有组件连接在一起。

您能否为我指明正确的方向,如何让这个相当简单的工作流程发挥作用?


编辑

我没有使用 XML 配置,我拥有的唯一与安全相关的配置是:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/**").csrf().disable();

        http.authorizeRequests()
                .antMatchers("/user").permitAll()
                .antMatchers("/user/**").permitAll()
                .anyRequest().authenticated();
    }
}

最佳答案

您可以通过 3 种不同的方式进行基本身份验证 -

1 - 只需添加 spring-security maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

自动启用基本身份验证,如用户名 - “用户”和密码,您可以在控制台中找到 使用默认安全密码 - “bdd42ed9-635d-422b-9430-ce463625fd2e”

2 - 创建一个安全配置类,如下代码

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationEntryPoint authEntryPoint;

    @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("user").password("password").roles("USER");
    }
}

3 - 另一种方法是在 application.properties 文件中添加默认用户名和密码,如下所示

security.user.name=user
security.user.password=password

并像下面的代码一样创建配置类

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

仍有疑问,请点击下面的链接观看此视频

Basic Authentication

关于java - 使用基本身份验证保护 Spring Boot REST 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36514587/

相关文章:

javascript - 如何在三元运算符上告诉 javascript "any number bigger than"?

java - 使用 scribe oauth facebook 的空指针异常

java - 如何在 Spring MVC 列表中显示输入字段

java - 读取 jar 依赖中的资源文件

java - 如何直接使用OSGI捆绑jar文件中的类?

java - 如何在获取Android后对List<Model>数据进行排序

java - 在 spring 中自动将安全标志添加到 JSESSIONID cookie

java - Spring启动Jdbc模板

java - 启动多个 spring boot 实例,但启动一个主机名用于本地开发

java - 将 JSON 映射传递给 Spring MVC Controller