In this article, I am going to demostrate how we could access the REST api secured by Spring Security to display in front-end. We are going to use Spring Tool Suite (STS).
Create a new Java Project in STS and add the files into it similar to the following structure.
│ pom.xml
├───src
│ └───main
│ ├───java
│ │ └───com
│ │ └───p1coder
│ │ │ MvcConfiguration.java
│ │ │ SecurityConfig.java
│ │ │ SpringBootWebApplication.java
│ │ ├───controller
│ │ │ ProductController.java
│ │ │ RequestHandler.java
│ │ ├───domain
│ │ │ Product.java
│ │ │ UserPreference.java
│ │ └───service
│ │ ProductService.java
│ │ RestApiService.java
│ └───webapp
│ └───WEB-INF
│ └───views
│ └───product
│ display.jsp
Fill each file with the sample code from each code section.
After maven update the reference packages, we can execute the sample by right click on project and choosing "Run as" -> "Spring Boot App".
In RequestHandler.java, we have REST api routes accessible by either PathVariable or RequestParam, which read the repository layer data.
If we directly access "http://localhost:8080/getAllProducts" api, we will get authentication dialog box as we secure the route ("/getAllProducts**") in "SecurityConfig.java" file. This route is granted only to "admin" role by extending the WebSecurityConfigurerAdapter class.
We will see the sample listing when we access "http://localhost:8080/products" page. We implement the data retrieval logic and pass api credential to REST api.
In RestApiService.java, BasicAuthenticationInterceptor is used to authenticate while retrieving by HTTP GET execute on RestTemplate instance.
In ProductController.java, ObjectMapper instance is used to deserialize the generic return value from data service call instantiated by @Autowired Dependency Injection.
Create a new Java Project in STS and add the files into it similar to the following structure.
│ pom.xml
├───src
│ └───main
│ ├───java
│ │ └───com
│ │ └───p1coder
│ │ │ MvcConfiguration.java
│ │ │ SecurityConfig.java
│ │ │ SpringBootWebApplication.java
│ │ ├───controller
│ │ │ ProductController.java
│ │ │ RequestHandler.java
│ │ ├───domain
│ │ │ Product.java
│ │ │ UserPreference.java
│ │ └───service
│ │ ProductService.java
│ │ RestApiService.java
│ └───webapp
│ └───WEB-INF
│ └───views
│ └───product
│ display.jsp
Fill each file with the sample code from each code section.
MvcConfiguration.java
SecurityConfig.java
SpringBootWebApplication.java
ProductController.java
RequestHandler.java
ProductController.java
RequestHandler.java
Product.java
UserPreference.java
ProductService.java
RestApiService.java
display.jsp
pom.xml
In RequestHandler.java, we have REST api routes accessible by either PathVariable or RequestParam, which read the repository layer data.
If we directly access "http://localhost:8080/getAllProducts" api, we will get authentication dialog box as we secure the route ("/getAllProducts**") in "SecurityConfig.java" file. This route is granted only to "admin" role by extending the WebSecurityConfigurerAdapter class.
We will see the sample listing when we access "http://localhost:8080/products" page. We implement the data retrieval logic and pass api credential to REST api.
In RestApiService.java, BasicAuthenticationInterceptor is used to authenticate while retrieving by HTTP GET execute on RestTemplate instance.
In ProductController.java, ObjectMapper instance is used to deserialize the generic return value from data service call instantiated by @Autowired Dependency Injection.
I read like this type of article only. You mentioned more important points in this article. Thankyou for interesting article.
ReplyDeletecore java training in chennai
c c++ classes
c c++ training
c++ course
c c++ course fee
javascript course in chennai
Appium Training in Chennai
JMeter Training in Chennai
Thank you, Velraj.
DeleteGreat post. You have written a valuable content in a interesting way. Kindly share more updates.
ReplyDeleteIELTS Coaching in Mulund
IELTS Training in Mulund West
IELTS Courses in Mulund
IELTS Coaching Centres in Mulund
Spoken English Class in Chennai
Spoken English in Chennai
IELTS Coaching in Chennai
Thank you, Anbarasan14.
DeleteSuch a wonderful post, Your explanation way is too good and this is very easy to understand with depth content. Kindly updating for me.
ReplyDeleteCorporate Training in Chennai
Corporate Training
Power BI Training in Chennai
Excel Training in Chennai
Linux Training in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Oracle Training in Chennai
Pega Training in Chennai
Oracle DBA Training in Chennai
Thank you, Rathinam.
DeleteThanks, Yuvaprithika.
ReplyDeleteI am glad to know this blog helps the reader, Kiruthika. Thanks.
ReplyDeleteweighing machine for kitchen
ReplyDeleteIt looks deeply awesome article!thanks for sharing such a information with us..
ReplyDeleteAWS Training in Chennai
AWS Certification in Chennai
digital marketing course in chennai
Data Science Course in Chennai
Selenium Course in Chennai
ccna Training in Chennai
Java Course in Chennai
Tally Classes in Chennai
AWS Training in Chennai
AWS Certification in Chennai
I want to thank for sharing this blog, really great and informative. Share more stuff like this. greate work
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Good content. You write beautiful things.
ReplyDeletehacklink
taksi
vbet
mrbahis
sportsbet
korsan taksi
hacklink
sportsbet
mrbahis
Good text Write good content success. Thank you
ReplyDeletekralbet
mobil ödeme bahis
kibris bahis siteleri
tipobet
poker siteleri
slot siteleri
betmatik
betpark
dijital kartvizit
ReplyDeletereferans kimliği nedir
binance referans kodu
referans kimliği nedir
bitcoin nasıl alınır
resimli magnet
33J412
beşiktaş
ReplyDeleteeyüpsultan
gebze
marmaris
tekirdağ
SBAS
Consuming a secured REST API demands proper authentication and authorization. What Are Passthrough Incorporate token-based authentication like OAuth, sending authentication headers with requests.
ReplyDeleteSecuring REST API consumption necessitates robust authentication. Implement methods like OAuth, JWT, or API keys for access control. Proxy Sites Torrenting Incorporate HTTPS encryption to safeguard data transmission.
ReplyDeletehttps://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
4JHJS
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
Y8S
muğla
ReplyDeleteümraniye
manavgat
karşıyaka
didim
FKG2
web postegro
ReplyDeletetakipcimx 1000
techy hit tools
postegro
takipçi satın al
P71BVV
كشف تسربات المياه بالجبيل
ReplyDeletehoiuhji;k