java - 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);
}
}
解决方案
我更改了依赖项,我删除了实现 'org.springframework.boot:spring-boot-starter-security'
并替换为
编译组:'org.springframework.boot',名称:'spring-boot-starter-security',版本:'2.3.1.RELEASE'
它奏效了。
推荐阅读
- javascript - 关于分页符,如何让 Puppeteer PDF 生成与 HTML 文档完全匹配?
- r - 我正在尝试在 devtools 包中使用 install_github 但失败了
- javascript - 在 JavaScript 中使用谓词函数进行匹配
- javascript - 将高阶函数称为“模板”是否正确?
- git - git 强制将本地文件放在原点之上
- c++ - C++ 中闭包的返回类型
- javascript - 在 chrome 扩展中提交表单时弹出刷新
- prolog - 如何让我的迷宫程序创建两个路线列表并将其打印到控制台?
- mysql - 尝试使用 select 语句向存储过程中的变量添加值
- r - 修改 flexdashboard 的 shinyauthr