Implement user registration function, the user needs to be stored encrypted password using MD5 encryption, the encryption process using a random code salt as salt. Also the need for short codes input by the user for verification.
1. Interfaces
(1) Path Interface:
POST /register
(2) returns the result:
-
201: Registration successful
-
400: The parameter is incorrect, the registration fails
-
-
1) check codes SMS
-
2) salt formation
-
3) to encrypt the password
-
4) into the database
-
/ ** * Register * @param User receives user object information (form submission form, user object reception) * @param code received form parameters - phone code * @return * / @PostMapping ( "Register" ) public ResponseEntity < void> Register (the User User, @RequestParam ( "code" ) String code) { Boolean Boo = the this .userService.register (User, code); IF (Boo == null ! || Boo) { return ResponseEntity.status (HttpStatus .BAD_REQUEST) .build (); } return new new ResponseEntity <>(HttpStatus.CREATED); }
3.UserService
/ ** * Function Register * @param User * @param code * @return * / public Boolean Register (the User User, String code) { // check message authentication code String = cacheCode the this .redisTemplate.opsForValue (). GET (+ key_prefix user.getPhone ()); IF (! StringUtils.equals (code, cacheCode)) { return to false ; } // salt formation String salt = CodecUtils.generateSalt (); user.setSalt (salt); // for password encryption user.setPassword (CodecUtils.md5Hex (user.getPassword (), Salt)); // mandatory parameter settings are not specified null user.setId ( null ); user.setCreated ( new new a Date ()); // add to the database Boolean B = the this .userMapper.insertSelective (User) ==. 1 ; IF (B) { // registered, redis delete records in the this .redisTemplate.delete (key_prefix + user.getPhone ()); } return B; }
As used herein the CodeUtils:
The encryption tools requires apache Kit:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency>
4. Test
We PostMan test:
http://api.leyou.com/api/user/register?username=lucky&password=plj824&phone=17826828544&code=984971
View database: