Detailed explanation and examples of database account password encryption

Detailed explanation and examples of database account password encryption

Detailed explanation and examples of database account password encryption

Database accounts and passwords are often encrypted in the database, but there is a problem. When using UserService to encrypt the password, spring security also needs to be configured synchronously, because the encryption method verified in spring security is configured separately. as follows:

<authentication-manager>
  <authentication-provider user-service-ref="userDetailService">
    <password-encoder ref="passwordEncoder" />
  </authentication-provider>
</authentication-manager>

<beans:bean class="com.sapphire.security.MyPasswordEncoder" id="passwordEncoder">
  <beans:constructor-arg value="md5"></beans:constructor-arg>
</beans:bean>

As shown in the above configuration file, passwordEncoder is where spring security encrypts and verifies the account.

After interception, spring security will first look up the user, find the corresponding user through the userDetailService defined by itself, and then the framework will perform password matching verification.

After getting the user from userDetailService, it will enter DaoAuthenticationProvider, which is defined in the framework, and then jump into the authenticate method.

This method performs two checks:

* preAuthenticationChecks: It mainly verifies whether the user information is expired, etc. The calling method is defined in userDetail.
* additionalAuthenticationChecks: This is the process of username and password verification.

PasswordEncoder is the bean injected in our xml, so we call the passwordEncoder we have completed ourselves.

public class MyPasswordEncoder extends MessageDigestPasswordEncoder {
  public MyPasswordEncoder(String algorithm) {
   super(algorithm);
  }

  @Override
  public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
   return encPass.equals(DigestUtils.md5DigestAsHex(rawPass.getBytes()));
  }
}

This is a simple version of my implementation. It calls the encryption algorithm that comes with spring. It is very simple. Of course, you can also use complex encryption methods. This depends on yourself.

Thank you for reading, I hope it can help you, thank you for your support of this site!

<<:  Detailed tutorial on how to delete Linux users using userdel command

>>:  6 ways to view the port numbers occupied by Linux processes

Recommend

A Deep Dive into JavaScript Promises

Table of contents 1. What is Promise? 2. Why is t...

Example of how to install nginx to a specified directory

Due to company requirements, two nginx servers in...

Implementation steps for installing Redis container in Docker

Table of contents Install Redis on Docker 1. Find...

JavaScript uses canvas to draw coordinates and lines

This article shares the specific code of using ca...

A brief analysis of MySQL's lru linked list

1. Briefly describe the traditional LRU linked li...

How to run multiple MySQL instances in Windows

Preface In Windows, you can start multiple MySQL ...

Simple principles for web page layout design

This article summarizes some simple principles of...

Super detailed basic JavaScript syntax rules

Table of contents 01 JavaScript (abbreviated as: ...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

CSS margin overlap and how to prevent it

The vertically adjacent edges of two or more bloc...

Completely uninstall MySQL database in Windows system to reinstall MySQL

1. In the control panel, uninstall all components...

Telnet is moved to busybox-extras in Alpine image

The telnet in the Alpine image has been moved to ...

Specific usage of textarea's disabled and readonly attributes

disabled definition and usage The disabled attrib...