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

How to view and configure password expiration on Linux

With the right settings, you can force Linux user...

Vue uses three methods to refresh the page

When we are writing projects, we often encounter ...

Summary of MySql import and export methods using mysqldump

Export database data: First open cmd and enter th...

MySQL Optimization: Cache Optimization

I am happy that some bloggers marked my article. ...

Implementation steps of vue-element-admin to build a backend management system

Recently, when I was working on a conference heal...

Detailed explanation of mysql record time-consuming sql example

mysql records time-consuming sql MySQL can record...

Mysql error: Too many connections solution

MySQL database too many connections This error ob...

XHTML Getting Started Tutorial: XHTML Web Page Image Application

<br />Adding pictures reasonably can make a ...

Example of how to configure cross-domain failure repair in nginx

Nginx cross-domain configuration does not take ef...

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

html opens a new window with a hyperlink and can control window properties

1. The window size opened by the HTML hyperlink C...

How to bind Docker container to external IP and port

Docker allows network services to be provided by ...