Creating a secure @RestController that encrypts passwords and transmits data in JSON format using Hibernate

When developing a login/registration form in AngularJS and using Spring Boot, I encountered an issue with encrypting passwords into the database. After registering a user, my code for sending data to the database looked like this:

@RequestMapping(value = "/register",method = RequestMethod.POST)
public User addUser(@RequestBody User user) {
    LOGGER.info("Received request to create the {}", user);
    return repo.saveAndFlush(user);
}

To address the encryption of passwords, how can I ensure that the password is securely stored in the database? Additionally, as I proceed to implement the login functionality, I am unsure how to retrieve data from the server in JSON format to AngularJS. Is this method secure, and what measures can be taken to handle password encryption effectively? Furthermore, is there a way to obscure or mask the password during validation?

Answer №1

Typically, passwords are not encrypted but hashed to ensure that they cannot be reversed back to the original password. When a user wants to access an application, their password is hashed and compared with the stored hashed version in the database.

I rely on the spring security BCrypt password encoder for hashing operations, which is straightforward to implement.

There are plenty of examples available for password hashing in Java. Here's an example to get you started.

To send a User object in JSON format to the client, simply add the @ResponseBody annotation (Spring MVC utilizes Jackson for converting to JSON).

@RequestMapping(value = "/register",method=RequestMethod.POST)
public @ResponseBody User addUser(@RequestBody User user) {

    .....

    return user
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method to retrieve a JSON record by its specific ID using Angular's AJAX functionalities?

I've been working on a function within my service that retrieves data function getSomeData() { return $http .get('/someData.json') .then(itWorked) .catch(onFail); Currently, this function returns all the records from the J ...

Mysterious AngularJS Error: An Unforeseen Encounter with [$injector:modulerr]

While following a tutorial on creating a MEAN stack project with Google Maps integration, I successfully completed the project without encountering any issues. However, when I returned to check on it later, I discovered that it was no longer functioning pr ...

Using AngularJS directives like ng-hide, ng-show, or ng-if to control visibility based on the active state of

Hello, I am currently developing a shopping cart with three different views: menu, options, and order. The list is generated using ng-repeat from JSON data, with each item in the array having an 'active' field set to false. When an item is added ...

The issue of the Angular controller not functioning properly in conjunction with Node Express

After creating an angular app (version 1.5.7), I wanted to deploy it to heroku. To do this, I implemented Node and used express to serve the main index.html file for heroku to build since it doesn't support plain angular apps. However, after making th ...

Challenge with Alignment of Fields in Bootstrap's Horizontal Form

I am working on an Angular project with a horizontal form using Bootstrap. I have encountered a slight alignment issue with one of the date fields. The IsMessedUp datepicker input field is not aligned properly, positioned slightly to the left compared to t ...

What is the functionality of an Angular service that retrieves an

It appears that Angularjs services are constructed by passing in a Contructor function: app.service('serviceName', function(){ this.var1 = 'foo'; this.method1 = function(){ } }); Angular executes this function using the new ...

Ways to merge multiple cells horizontally in a table right from the beginning

Is there a way to start the colspan from the second column (Name)? See image below for reference: https://i.stack.imgur.com/RvX92.png <table width="100%"> <thead style="background-color: lightgray;"> <tr> <td style="width ...

Following the modification of the Context root, the JSP page is no longer functioning as expected

Recently, I developed a JSP page within the webapp that utilizes jquery, CSS, and Angular JS. The files jquery-1.12.0.js, angular.min.js, and TableCSSCode.css are all located in the same directory as the JSP page. Initially, my application context was set ...

Tips for refining the ng-model value through AngularJS directive?

When including the date element in a template, I'm encountering an issue with binding the ng-model value as a string to the date. To address this, I am attempting to convert the string into an object using the directive named date-ob. <input type= ...

AngularJS app is not initialized by ngApp, only by ng-app

Up until now, I had believed that there was no difference at all between using the different formats of Angular directive names - ngApp, ng-app, x-ng-app should all work interchangeably. However, on this particular webpage, Angular doesn't seem to in ...

AngularJS - Embedding input fields within an HTML table within a form

The system is up and running now... I retrieve an array and insert the name into an input field. Then, I update the name in the input field and send it back using a PUT request. Here is the HTML code: <form id="edit"> <table> < ...

Showing validation in a Bootstrap modal after it has been closed

I have implemented a form view in AngularJS with a modal from Angular-ui to showcase my form. I am happy with how everything functions, but there is one issue - when I dismiss the form, validation pop-ups appear if the form is not valid. Here is an overvi ...

New update to Angular Currency Filter - Now including Symbol Â!

While utilizing angular's currency filter, I noticed an unexpected extra symbol being outputted: Â. This is the HTML code: {{totals.subtotal | currency}} {{totals.tax | currency}} {{totals.total | currency}} The 'totals' object looks lik ...

Implementing ng-if with asynchronous functions: A step-by-step guide

The objective here is to display an image in a template only if the ratio of its dimensions is greater than 2. <img class="main-img" ng-if="showImage($index)" ng-src="{{item.img}}"> Implementation: $scope.showImage = function(index) { var img ...

Angular directives leading to extended loading durations

We have developed a substantial application using Angular (1.3) that includes a page with around 20 nested custom directives. The loading time of the application, particularly on Android devices, is concerning. After using Chrome timeline profiling, it was ...

Enabling the acceptance of blank values within an HTML5 date input field

When using an HTML5 date input for a field that corresponds to a nullable datetime column in the database, how can one avoid setting an empty value in the input field? In my AngularJS application, the ng-model is connected to a scope variable with an init ...

Issue with authentication and cross-origin resource sharing (CORS) when implementing Spring Boot, Spring Security, Vue.js,

Running on vue.js 3, Vite 4.0.2, axios 0.25.0, and spring boot (Starter 2.7.2). A backend has been created in spring boot while using vue.js3, vite, and axios for the UI. Now, I simply want to make a call to rest with axios. Before implementing these func ...

What are the benefits of having a service dedicated to interacting with a single entity, while another service is responsible for handling a group of entities?

Imagine we have a User entity. Would it be better to have two smaller services (User and Users) or one larger service that manages both individual Users and collections of Users? If it's the latter, what's the recommended practice for naming the ...

The $q.all() function in angular seems to struggle with resolving properly

Having trouble with 3 $http calls in a factory. Creating 4 promises: var promise = $q.defer(), PBdeferred = $q.defer(), Rdeferred = $q.defer(), Pdeferred = $q.defer(); Making the first call to the API: $http.get('/pendingBills').then(fu ...

Executing a controller method in AngularJS when redirecting a page

I am currently working on an app using Cordova/Phonegap, Ionic, and AngularJS. One challenge I am facing is trying to call a method from a controller inside my app when redirecting to another HTML page (secondPage.html). This particular method (secondMetho ...