首页 > 解决方案 > Spring Boot 401 未经授权的邮递员

问题描述

我是spring boot的新手,正在尝试向邮递员发出授权请求,但我得到401 Unauthorized。我正在尝试使用 jpa 将用户存储在数据库 H2 中,并带有以 json 为主体的 post 请求,我不知道如何解释更多,所以如果有人弄清楚,我将发布代码。



import engine.user.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
@EnableConfigurationProperties
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final UserDetailsServiceImpl userDetailsService;

    @Autowired
    public WebSecurityConfig(UserDetailsServiceImpl userDetailsService) {
        this.userDetailsService = userDetailsService;
    }



   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http
               .csrf().disable().headers().frameOptions().disable().and()
               .authorizeRequests().antMatchers("/api/register").permitAll()
               .antMatchers("/actuator/shutdown").permitAll()
               .and()
               .httpBasic();
   }

    @Override
    public void configure(AuthenticationManagerBuilder builder)
            throws Exception {
        builder.userDetailsService(userDetailsService);
    }

}

依赖关系

    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 11

repositories {
    mavenCentral()
}

sourceSets.main.resources.srcDirs = ["src/resources"]

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile("org.springframework.boot:spring-boot-starter-web")
    runtimeOnly 'com.h2database:h2'

} 

用户控制器


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;

public class UserController {

    private final UserDetailsServiceImpl userDetailsService;

    @Autowired
    public UserController(UserDetailsServiceImpl userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    @PostMapping(value = "/api/register", consumes = "application/json")
    public ResponseEntity<User> saveUser(@Valid @RequestBody  User user) {
        userDetailsService.save(user);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }
} 

邮差

标签: javaspring-boothibernatejpaspring-security

解决方案


我更改了依赖项,我删除了实现 'org.springframework.boot:spring-boot-starter-security'

并替换为

编译组:'org.springframework.boot',名称:'spring-boot-starter-security',版本:'2.3.1.RELEASE'

它奏效了。


推荐阅读