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

You may not know these things about Mysql auto-increment id

Introduction: When using MySQL to create a table,...

Sample code for programmatically processing CSS styles

Benefits of a programmatic approach 1. Global con...

Use Angular CDK to implement a Service pop-up Toast component function

Table of contents 1. Environmental Installation 2...

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...

Summary of how JS operates on pages inside and outside Iframe

Table of contents Get the content of the iframe o...

How to use IDEA to create a web project and publish it to tomcat

Table of contents Web Development 1. Overview of ...

Implementation of whack-a-mole game in JavaScript

This article shares the specific code for JavaScr...

Three Discussions on Iframe Adaptive Height Code

When building a B/S system interface, you often en...

Shell script settings to prevent brute force ssh

The shell script sets access control, and the IP ...

A brief discussion on JavaScript throttling and anti-shake

Table of contents Throttling and anti-shake conce...

Docker Compose installation and usage steps

Table of contents 1. What is Docker Compose? 2. D...

What are the drawbacks of deploying the database in a Docker container?

Preface Docker has been very popular in the past ...

Solution to the problem of failure to insert emoji expressions into MySQL

Preface I always thought that UTF-8 was a univers...