If you just want to see how the authentication looks like in Spring Boot, this blog is for you.
Basic Authentication
-
Create any controller which you want to secure.
@GetMapping("/") public String helloWorld() { return "Hello World"; } -
Add the
spring-boot-starter-securitydependency in pom.xml file. It will automatically add the security on all the endpoints.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> Now, start the app and you will see a random generated string on the console.
Go to any API client (like Postman) and test the endpoint.
Add
Basic Authsecurity for your endpoint.Copy the string and use it as a password (Default username is
user).Now, test the endpoint, it will give you the desired result.
Thank you!
Top comments (0)