如何修复在Spring Boot上出现的“Error 401 Unauthorized”。

huangapple go评论139阅读模式
英文:

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

Folder structure

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(&quot;id&quot;) UUID id,
@JsonProperty(&quot;playerName&quot;) 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&lt;Player&gt; 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(&quot;fakeDao&quot;)
public class FakePlayerDataAccessService implements PlayerDao {
private static List&lt;Player&gt; DB = new ArrayList&lt;&gt;();
@Override
public int insertPlayer(UUID id, Player player){
DB.add(
new Player(id,
player.getPlayerName()
//player.getPlayerPassword()
)
);
return 0;
}
@Override
public List&lt;Player&gt; 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(&quot;api/v1/player&quot;)
@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&lt;Player&gt; getAllPlayer() { return playerService.getAllPlayer(); }
}

pom

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.Project_A.Databaseconnection&lt;/groupId&gt;
&lt;artifactId&gt;Artifact&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;LoginServerConnector&lt;/name&gt;
&lt;description&gt;connection with the LoginServer&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
&lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
&lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
&lt;version&gt;2.11.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

答案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

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-oauth2-resource-server&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
&lt;/dependency&gt;

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.

huangapple
  • 本文由 发表于 2020年8月20日 21:00:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63505701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定