【问题标题】:Spring Boot 401 Unauthorized PostmanSpring Boot 401 未经授权的邮递员
【发布时间】:2021-01-24 23:54:39
【问题描述】:

我是 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);
    }
} 

Postman

【问题讨论】:

  • 您可以在 Postman 中显示“授权”选项卡吗?
  • @HarryCoder i.stack.imgur.com/4bpAo.jpg .authorizeRequests().antMatchers("/api/register").permitAll()

标签: java spring-boot hibernate jpa spring-security


【解决方案1】:

很可能您没有在 Postman 中设置 Authorization 标头。您可以通过“授权”选项卡执行此操作。从下拉列表中选择基本身份验证,然后提供尝试执行该操作的用户的用户凭据。

注意 Postman 如何自动添加 Authorization 标头。

最后,在body标签上,添加json请求数据。

同样,Postman 会自动为您添加内容类型标头。

现在您可以触发您的发帖请求了。

【讨论】:

  • 问题是发布请求已获得授权,因此不需要 Authorization 标头 .authorizeRequests().antMatchers("/api/register").permitAll() 而我没有做任何默认登录
【解决方案2】:

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

并替换为

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

它成功了。

【讨论】:

    猜你喜欢
    • 2019-11-09
    • 2018-07-01
    • 1970-01-01
    • 2021-03-19
    • 2023-03-07
    • 2021-04-07
    • 2022-01-21
    • 2020-10-22
    • 2018-03-30
    相关资源
    最近更新 更多