gpt4 book ai didi

Spring Boot获取401未经授权的状态代码以进行简单的获取请求

转载 作者:行者123 更新时间:2023-12-02 11:48:23 25 4
gpt4 key购买 nike

我对 Spring 框架非常陌生。我创建了一个新的 Spring Starter 项目,其中包含以下模块:web、mongo、security。

我创建了一个简单的 Controller

@RestController
@RequestMapping("/users")
public class UserController {

private UserRepository userRepository;

@GetMapping("/all")
public List<User> getAllUsers(){
List<User> users = this.userRepository.findAll();
return users;
}

@PostMapping("/")
public void insert(@RequestBody User user){
this.userRepository.save(user);
}
}

并将一些原始数据植入数据库。当我在 Postman 中向此路由发出请求时,我收到以下响应:

{
"timestamp": 1511113712858,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"path": "/users/all"
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ngt</groupId>
<artifactId>someArtifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>dermaskin</name>
<description>Demo project for Spring Boot with mongodb</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

是什么导致了未经授权的响应以及如何针对 /all 路由禁用它?谢谢!

最佳答案

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers().antMatchers("/users/all").permitAll();
}
}

您需要配置 Spring Security,默认情况下所有路由都受到授权保护。

上面的代码仅禁用“/users/all”URL 的安全性。

关于Spring Boot获取401未经授权的状态代码以进行简单的获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47380068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com