• Spring Boot_1【配置环境&&项目结构&&Spring Security相关】


    一:配置环境

    环境配置springboot.jpg

    二:项目开发

    A.项目结构

    spring结构.jpg

    B.注解

    注解1.jpg
    注解2.jpg
    注解3.jpg
    注解4.jpg
    注解5.jpg
    注解6.jpg

    C.项目各层实现

    C1:pojo层

    pojo层实现.jpg

    C2:mapper层

    mapper层实现.jpg

    C3:controller层

    @RestController
    public class UserController {
    
        @Autowired
        UserMapper userMapper;
    
        /**
         * 查询所有用户
         */
        @GetMapping("/user/all/")
        public List<User> getAll() {
            return userMapper.selectList(null);
        }
    
        /**
         * 查询单个用户
         */
        @GetMapping("/user/{userId}/")
        public User getUser(@PathVariable int userId) {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",userId);
    
            return userMapper.selectOne(queryWrapper);
    
            // 范围遍历
            // public List getUser(int userId)
            // queryWrapper.ge("id", 2).le("id", 3);
            // return userMapper.selectList(queryWrapper);
        }
    
        /**
         * 添加某个用户 直接输入 id name password
         * @param userId
         * @param username
         * @param password
         * @return Add User Sucessfully
         */
        @GetMapping("/user/add/{userId}/{username}/{password}/")
        public String addUser (@PathVariable int userId,
                               @PathVariable String username,
                               @PathVariable String password) {
    
            User user = new User(userId, username, password);
            userMapper.insert(user);
            return "Add User Sucessfully";
        }
    
        /**
         * 删除某个用户,直接输入 id
         * @param userId
         * @return Delete User Successfully
         */
        @GetMapping("/user/delete/{userId}/")
        public String deleteUser(@PathVariable int userId) {
            userMapper.deleteById(userId);
            return "Delete User Successfully";
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    1.user+生成的密码

    spring security机制:用户认证
    用户认证机制+依赖.jpg

    C4:service层

    2.明文密码(数据库) [注数据库存密码要{noop}]

    333333.jpg
    1111.jpg

    第一步

    用户认证+数据库1.jpg

    @Service
    public class UserDetailsServiceImpl implements UserDetailsService {
        //快捷键 windowsalt + insert / mac是option + enter
        @Autowired
        private UserMapper userMapper;
        // 传入 username 返回对应的信息,在这里也就是id name pwd
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", username);
            User user = userMapper.selectOne(queryWrapper);
            if (user == null) {
                throw new RuntimeException("用户不存在");
            }
    
            return new UserDetailsImpl(user);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    第二步

    用户认证+数据库2.jpg

    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class UserDetailsImpl implements UserDetails {
    
        private User user;
    
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return null;
        }
    
        @Override
        public String getPassword() {
            return user.getPassword();
        }
    
        @Override
        public String getUsername() {
            return user.getUsername();
        }
    
        @Override
        public boolean isAccountNonExpired() {
            return true;
        }
    
        @Override
        public boolean isAccountNonLocked() {
            return true;
        }
    
        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }
    
        @Override
        public boolean isEnabled() {
            return true;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    3.密文密码(数据库)

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig {
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    222222.jpg

  • 相关阅读:
    DNS协议、ICMP协议、NAT技术
    A Cooperative Approach to Particle Swarm Optimization
    QGis软件 —— 9、QGis - 由点绘制热力图来模拟人流量(绘制点方式、excel导入数据方式)
    Go基础——指针、结构体
    深入理解 Python 虚拟机:字节(bytes)的实现原理及源码剖析
    使用C语言实现静态顺序表
    QT中的inherits
    kafka环境配置以及Java代码测试
    大龄、零基础,想转行做网络安全。怎样比较可行?
    网络、网络协议模型、UDP编程——计算机网络——day01
  • 原文地址:https://blog.csdn.net/qq_52384627/article/details/126226036