0% found this document useful (0 votes)
3 views2 pages

Basic Auth in Spring Security Setup

The document outlines the implementation of Spring Security in applications using the 'spring-security-starter' dependency in the pom.xml file. It details the default security settings, including default credentials, and how to override them in the application.properties file. Additionally, it provides a Rest Client example for accessing a secured REST API using both RestTemplate and WebClient with basic authentication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Basic Auth in Spring Security Setup

The document outlines the implementation of Spring Security in applications using the 'spring-security-starter' dependency in the pom.xml file. It details the default security settings, including default credentials, and how to override them in the application.properties file. Additionally, it provides a Rest Client example for accessing a secured REST API using both RestTemplate and WebClient with basic authentication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

##############

Spring Security
###############

-> To implement security for our applications Spring provided 'security' module

-> To use Spring Security in our project 'spring-security-starter' we need to add


in [Link] file

<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

-> By default it will secure all the endpoints of our application

Default uname : user


Default Pwd: Will be printed on the console

-> To override default credentials we can configure credentails in


[Link] file

[Link]=admin
[Link]=admin@123

-> Create Rest Controller class with required method

#####################################
Rest Client To access Secured REST API
#######################################

@Service
public class WelcomeService {

private String apiUrl = "[Link]

public void invokeWelcomeApi() {

RestTemplate rt = new RestTemplate();

HttpHeaders headers = new HttpHeaders();


[Link]("admin", "admin@123");

HttpEntity<String> reqEntity = new HttpEntity<>(headers);

ResponseEntity<String> responseEntity = [Link](apiUrl,


[Link], reqEntity, [Link]);

String body = [Link]();

[Link](body);
}

public void invokeWelcome() {


WebClient webClient = [Link]();

String block = [Link]()


.uri(apiUrl)
.headers(headers ->
[Link]("admin", "admin@123"))
.retrieve()
.bodyToMono([Link])
.block();

[Link](block);
}

You might also like