0

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("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>
user14136803
  • 1
  • 1
  • 2
  • I see a Spring Security dependency in your pom.xml. How have you configured Spring Security? What endpoints have you configured to accept all requests, which ones have you configured to require authentication? – JustAnotherDeveloper Aug 20 '20 at 13:07
  • @user14136803 you have added `spring-boot-starter-security` in dependencies list, it by default provides basic authentication. so in POSTMAN, you have to give basic auth details, **default username is user & password will be printed in logs**. something like this: `Using default security password: 78fa095d-3f4c-48b1-ad50-e24c31d5cf35`. This link might help you [fix 401](https://stackoverflow.com/questions/37285016/what-is-username-and-password-when-starting-spring-boot-with-tomcat) – Dinesh Dontha Aug 20 '20 at 13:09
  • thank you, i thought that im going to need it later to only accept HTTPS requests but i have not changed anything in that regard. I use Postman, ill read it and try it later. – user14136803 Aug 20 '20 at 13:29
  • i used the baisc auth in Postman and added "user" and the password from the log. still gives me 401 I also tried to delete the pom section or make a completely new project. still 401 – user14136803 Aug 20 '20 at 14:09
  • Please delete the cookies in chrome & try. – Dinesh Dontha Aug 20 '20 at 14:43
  • i closed firefox, closed Postman, used ccleaner to delete all cookies problem is still there – user14136803 Aug 20 '20 at 15:47

1 Answers1

0

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Manju D
  • 111
  • 2