Search code examples
javaspring-boothttpnetworkinglocalhost

error accessing spring boot application via localhost:8080


I try to access localhost:8080/hello but appeals localhost:8080/login the only things that shows is status code 302 on network status and shows that hello.html has no initiator and initiator hello is on login

     package com.example.biblioteca;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

     @SpringBootApplication
     public class BibliotecaApplication {

    public static void main(String[] args) {
        SpringApplication.run(BibliotecaApplication.class, args);
      }

    }
Controller class

      package com.example.biblioteca.controller;

     import javax.servlet.http.HttpServletRequest;
     import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;

     @Controller
     public class HelloControler {
    
      @GetMapping("/hello")
       public String hello() {
         return "hello";
     }
     }

html file:
  <html>
        <head>
          <meta charset ="UTF-8"/>
       
        </head>
       <body>
         hello working
        </body>
    </html>

Solution

  • if you don't want to use security, just delete or comment this dependency from your pom.xml file:

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

    when you use spring-security you have to pass the authentication before any action.

    if you want to use security, add this security configs to the application.properties file:

    spring.security.user.name=custom-username
    spring.security.user.password=custom-password