Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 具有基本身份验证的安全Spring Boot REST应用程序_Java_Spring_Spring Mvc_Authentication_Spring Security - Fatal编程技术网

Java 具有基本身份验证的安全Spring Boot REST应用程序

Java 具有基本身份验证的安全Spring Boot REST应用程序,java,spring,spring-mvc,authentication,spring-security,Java,Spring,Spring Mvc,Authentication,Spring Security,我已经查阅了关于使用SpringSecurity的文章,但它们并不能完全满足我的需要。它们通常处理内存用户和明文密码 我想要实现的目标: 客户端使用基本身份验证进行身份验证 安全控制器方法如下所示: @RequestMapping(value = "/test") public ResponseEntity test(@AuthenticationPrincipal MyUser user){ // Logic } 如果给定用户经过身份验证,我希望控制器方法在其参数中获

我已经查阅了关于使用SpringSecurity的文章,但它们并不能完全满足我的需要。它们通常处理内存用户和明文密码

我想要实现的目标:

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

安全控制器方法如下所示:

  @RequestMapping(value = "/test")
  public ResponseEntity test(@AuthenticationPrincipal MyUser user){
      // Logic
  }
如果给定用户经过身份验证,我希望控制器方法在其参数中获取MyUser对象,如果没有,则获取null

我将散列密码和用户的salt存储在数据库中,我有一个服务,它对给定的用户名-密码对进行身份验证,如果身份验证成功或失败,则分别返回用户对象或null

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


我读过使用custom
UserDetailsService
,但这似乎只处理从数据库获取用户数据,而不处理任何身份验证。我想不出如何把所有的部件都插在一起

你能为我指出正确的方向吗?如何让这个相当简单的工作流程工作


编辑

我没有使用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安全maven依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
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();
    }
}
仍然有一些疑问,然后点击下面的链接并观看此视频


基本上,您需要的是将
添加到
标记中(如果您使用的是XML样式配置)。要获得更具体的答案,您能否在帖子中添加一些有关配置的详细信息?例如,配置文件/类和
web.xml
(如果有)我没有使用xml配置,而是在代码中使用它。不过没什么大不了的。我把它添加到了问题中,看起来确实很有希望,我会看一看。请不要只回答带有链接的问题:为了提高你答案的质量,请在你的答案中包含链接的相关部分。这部分有帮助,但是导致了另一个问题-在第一个代码块中,
private AuthenticationEntryPoint authEntryPoint
甚至用于什么?
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}