英文:
how can i fix "Error 401 Unauthorized" on Spring Boot
问题
我开始观看有关如何制作一个非常基本的Spring Boot项目的教程。https://www.youtube.com/watch?v=vtPkZShrvXQ
无论我做什么,我总是得到401错误,即使我将代码一对一地复制。
提前感谢您花时间帮助我。
我尝试将以下内容放入application.properties文件中:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/LoginDB
spring.datasource.username=root
spring.datasource.password=
我创建了以下文件:
Player类:
package com.Project_A.Databaseconnection.Artifact.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.UUID;
public class Player {
private final UUID id;
private final String PlayerName;
public Player(@JsonProperty("id") UUID id,
@JsonProperty("playerName") String playerName
) {
this.id = id;
PlayerName = playerName;
}
public UUID getId() {
return id;
}
public String getPlayerName() {
return PlayerName;
}
}
PlayerDao接口:
package com.Project_A.Databaseconnection.Artifact.dao;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import java.util.List;
import java.util.UUID;
public interface PlayerDao {
int insertPlayer(UUID id, Player player);
default int insertPlayer(Player player){
UUID id = UUID.randomUUID();
return insertPlayer(id, player);
}
List<Player> selectAllPlayer();
}
FakePlayerDataAccessService类:
package com.Project_A.Databaseconnection.Artifact.dao;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Repository("fakeDao")
public class FakePlayerDataAccessService implements PlayerDao {
private static List<Player> DB = new ArrayList<>();
@Override
public int insertPlayer(UUID id, Player player){
DB.add(
new Player(id,
player.getPlayerName()
//player.getPlayerPassword()
)
);
return 0;
}
@Override
public List<Player> selectAllPlayer() {
return DB;
}
}
PlayerController类:
package com.Project_A.Databaseconnection.Artifact.api;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import com.Project_A.Databaseconnection.Artifact.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("api/v1/player")
@RestController
public class PlayerController {
private final PlayerService playerService;
@Autowired
public PlayerController(PlayerService playerService){
this.playerService = playerService;
}
@PostMapping
public void addPlayer(@RequestBody Player player) { playerService.addPlayer(player); }
@GetMapping
public List<Player> getAllPlayer() { return playerService.getAllPlayer(); }
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Project_A.Databaseconnection</groupId>
<artifactId>Artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LoginServerConnector</name>
<description>connection with the LoginServer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
英文:
I started watching a Tutorial on how to make a very basic Spring Boot Project. https://www.youtube.com/watch?v=vtPkZShrvXQ
No matter what i do i always get error 401, even when i copy the code 1-1.
Thank you in advance for taking your time to help me.
I tried putting this into the application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/LoginDB
spring.datasource.username=root
spring.datasource.password=
I made following Files
Player
package com.Project_A.Databaseconnection.Artifact.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.UUID;
public class Player {
private final UUID id;
private final String PlayerName;
public Player(@JsonProperty("id") UUID id,
@JsonProperty("playerName") String playerName
) {
this.id = id;
PlayerName = playerName;
}
public UUID getId() {
return id;
}
public String getPlayerName() {
return PlayerName;
}
}
PlayerDao
package com.Project_A.Databaseconnection.Artifact.dao;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import java.util.List;
import java.util.UUID;
public interface PlayerDao {
int insertPlayer(UUID id, Player player);
default int insertPlayer(Player player){
UUID id = UUID.randomUUID();
return insertPlayer(id, player);
}
List<Player> selectAllPlayer();
}
FakePlayerDataAccessService
package com.Project_A.Databaseconnection.Artifact.dao;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Repository("fakeDao")
public class FakePlayerDataAccessService implements PlayerDao {
private static List<Player> DB = new ArrayList<>();
@Override
public int insertPlayer(UUID id, Player player){
DB.add(
new Player(id,
player.getPlayerName()
//player.getPlayerPassword()
)
);
return 0;
}
@Override
public List<Player> selectAllPlayer() {
return DB;
}
}
PlayerController
package com.Project_A.Databaseconnection.Artifact.api;
import com.Project_A.Databaseconnection.Artifact.model.Player;
import com.Project_A.Databaseconnection.Artifact.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RequestMapping("api/v1/player")
@RestController
public class PlayerController {
private final PlayerService playerService;
@Autowired
public PlayerController(PlayerService playerService){
this.playerService = playerService;
}
@PostMapping
public void addPlayer(@RequestBody Player player) { playerService.addPlayer(player); }
@GetMapping
public List<Player> getAllPlayer() { return playerService.getAllPlayer(); }
}
pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Project_A.Databaseconnection</groupId>
<artifactId>Artifact</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LoginServerConnector</name>
<description>connection with the LoginServer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案1
得分: 0
如果您的项目不需要Spring Security,那么请从您的pom.xml文件中移除以下2个依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
如果您需要基本安全性,则只需使用第二个依赖项。默认用户名为"user",密码将从控制台日志中获取。
英文:
If you don't need spring security in your project then remove below 2 dependencies from your pom.xml file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
And if you need basic security then only use 2nd dependency And default user name is "user" and password you will get from console log.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论