当前位置:   article > 正文

基于springsecurity、mybatisplus实现注册功能_mabatisplus判断电话号,用户名,身份证号不可重复

mabatisplus判断电话号,用户名,身份证号不可重复

使用springsecurity框架提供了加密算法,方便对密码进行加密

1、新建项目、数据库配置、添加依赖

2、代码生成

3、不使用security框架注册的效果

4、使用security中的PasswordEncoder进行加密

5、测试

 6、补充(判断是否重复、空值

使用springsecurity框架提供了加密算法,方便对密码进行加密

1、新建项目、数据库配置、添加依赖

新建一个spring boot项目,数据库User表。

依赖:web、security、mybatisplus、mysql、lombok

  1. <!--web-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!--mybatisplus-->
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>mybatis-plus-boot-starter</artifactId>
  10. <version>3.4.3</version>
  11. </dependency>
  12. <!--mysql-->
  13. <dependency>
  14. <groupId>mysql</groupId>
  15. <artifactId>mysql-connector-java</artifactId>
  16. </dependency>
  17. <!--security-->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-security</artifactId>
  21. </dependency>
  22. <!--lombok-->
  23. <dependency>
  24. <groupId>org.projectlombok</groupId>
  25. <artifactId>lombok</artifactId>
  26. </dependency>

 application.yml

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/forum?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
  7. username: root
  8. password: 123456
  9. mybatis-plus:
  10. global-config:
  11. db-config:
  12. #数据库主键自增
  13. id-type: auto
  14. #逻辑删除
  15. logic-delete-field: delFlag
  16. #逻辑已删除
  17. logic-delete-value: 1
  18. #逻辑未删除
  19. logic-not-delete-value: 0
  20. configuration:
  21. #是否开启自动驼峰命名规则映射
  22. map-underscore-to-camel-case: true
  23. #开启控制台 SQL 日志打印
  24. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2、代码生成

生成UserService、UserServiceImpl、UserController、User实体类

3、不使用security框架注册的效果

UserController

  1. package com.example.demo.controller;
  2. import com.example.demo.entity.User;
  3. import com.example.demo.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. public class UserController {
  11. @Autowired
  12. private UserService userService;
  13. @PostMapping("/register")
  14. public String register(@RequestBody User user){
  15. return userService.register(user);
  16. }
  17. }

UserSercixeImpl

  1. package com.example.demo.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.example.demo.entity.User;
  4. import com.example.demo.mapper.UserMapper;
  5. import com.example.demo.service.UserService;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  9. @Override
  10. public String register(User user) {
  11. boolean save = save(user);
  12. return String.valueOf(save);
  13. }
  14. }

注释掉security依赖发送请求,我们可以看到存到数据库里的密码是明文密码,未经过加密的

 

 

4、使用security中的PasswordEncoder进行加密

取消security注释

配置SecurityConfig配置类

SecurityConfig

  1. package com.example.demo.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.authentication.AuthenticationManager;
  5. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. import org.springframework.security.config.http.SessionCreationPolicy;
  8. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  9. import org.springframework.security.crypto.password.PasswordEncoder;
  10. import org.springframework.stereotype.Component;
  11. /**
  12. * @BelongsProject: SprngSecurityLogin
  13. * @BelongsPackage: com.example.demo.config
  14. * @Author: tanxudong
  15. * @CreateTime: 2023-04-21 18:49
  16. * @Description: TODO
  17. * @Version: 1.0
  18. */
  19. @Configuration
  20. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  21. @Bean
  22. @Override
  23. public AuthenticationManager authenticationManagerBean() throws Exception {
  24. return super.authenticationManagerBean();
  25. }
  26. @Override
  27. protected void configure(HttpSecurity http) throws Exception {
  28. /*基于spring security,为了防止跨站提交攻击,通常会启用csrf,
  29. 所有http请求都被会CsrfFilter拦截,
  30. 而CsrfFilter中有一个私有类DefaultRequiresCsrfMatcher,
  31. POST方法被排除在外了,也就是说只有GET|HEAD|TRACE|OPTIONS这4类方法会被放行。*/
  32. http.csrf().disable()
  33. .authorizeRequests().mvcMatchers("/register").permitAll();
  34. }
  35. @Bean
  36. public PasswordEncoder passwordEncoder(){
  37. return new BCryptPasswordEncoder();
  38. }
  39. }

UserServiceImpl

  1. package com.example.demo.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.example.demo.entity.User;
  4. import com.example.demo.mapper.UserMapper;
  5. import com.example.demo.service.UserService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.crypto.password.PasswordEncoder;
  8. import org.springframework.stereotype.Service;
  9. /**
  10. * @BelongsProject: SprngSecurityLogin
  11. * @BelongsPackage: com.example.demo.service.impl
  12. * @Author: tanxudong
  13. * @CreateTime: 2023-04-21 17:58
  14. * @Description: TODO
  15. * @Version: 1.0
  16. */
  17. @Service
  18. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  19. @Autowired
  20. private PasswordEncoder passwordEncoder;
  21. @Override
  22. public String register(User user) {
  23. String encodePassword = passwordEncoder.encode(user.getPassword());
  24. user.setPassword(encodePassword);
  25. boolean save = save(user);
  26. return String.valueOf(save);
  27. }
  28. }

5、测试

再次发起post请求

对比两次结果,没使用security前是明文密码,使用后是密文

 

 6、补充(判断是否重复、空值)

有时候会要求用户名唯一,所以要加一次判断,判断是否重复。

同时,不仅需要前端进行某些字段的判空,后端也需要进行一次判断。

  1. package com.example.demo.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.example.demo.entity.User;
  5. import com.example.demo.mapper.UserMapper;
  6. import com.example.demo.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.security.crypto.password.PasswordEncoder;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.util.StringUtils;
  11. /**
  12. * @BelongsProject: SprngSecurityLogin
  13. * @BelongsPackage: com.example.demo.service.impl
  14. * @Author: tanxudong
  15. * @CreateTime: 2023-04-21 17:58
  16. * @Description: TODO
  17. * @Version: 1.0
  18. */
  19. @Service
  20. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  21. @Autowired
  22. private PasswordEncoder passwordEncoder;
  23. @Override
  24. public String register(User user) {
  25. /*对用户名是否重复进行判断,同理其他的也可以这样判断*/
  26. if (ExistUsername(user.getUsername())){
  27. throw new RuntimeException("用户名重复");
  28. }
  29. /*判断用户名是否为空*/
  30. if (!StringUtils.hasText(user.getUsername())){
  31. throw new RuntimeException("用户名不能为空");
  32. }
  33. String encodePassword = passwordEncoder.encode(user.getPassword());
  34. user.setPassword(encodePassword);
  35. boolean save = save(user);
  36. return String.valueOf(save);
  37. }
  38. /*对用户名是否重复进行判断*/
  39. private boolean ExistUsername(String username) {
  40. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
  41. queryWrapper.eq(User::getUsername,username);
  42. //如果查询的条数大于0,说明重复,返回true
  43. return count(queryWrapper)>0;
  44. }
  45. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/922018
推荐阅读
  

闽ICP备14008679号