parent
997e5e6532
commit
8018aa025c
18 changed files with 279 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,20 @@ |
||||
package com.kob.backend.config; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
||||
import org.springframework.security.crypto.password.PasswordEncoder; |
||||
|
||||
/** |
||||
* @author new |
||||
*/ |
||||
@Configuration |
||||
@EnableWebSecurity |
||||
public class SecurityConfig { |
||||
|
||||
@Bean |
||||
public PasswordEncoder passwordEncoder() { |
||||
return new BCryptPasswordEncoder(); |
||||
} |
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.kob.backend.controller.pojo; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
import lombok.NoArgsConstructor; |
||||
|
||||
/** |
||||
* @author new |
||||
*/ |
||||
|
||||
@Data |
||||
@NoArgsConstructor |
||||
@AllArgsConstructor |
||||
|
||||
public class User { |
||||
// 相当于User表中的一行数据
|
||||
private Integer id; |
||||
private String username; |
||||
private String password; |
||||
} |
@ -0,0 +1,35 @@ |
||||
package com.kob.backend.controller.service.impl; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.kob.backend.controller.pojo.User; |
||||
import com.kob.backend.controller.service.impl.utils.UserDetailsImpl; |
||||
import com.kob.backend.mapper.UserMapper; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.security.core.userdetails.UserDetails; |
||||
import org.springframework.security.core.userdetails.UserDetailsService; |
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
/** |
||||
* @author new |
||||
*/ |
||||
@Service |
||||
public class UserDetailsServiceImpl implements UserDetailsService { |
||||
|
||||
@Autowired |
||||
private UserMapper userMapper; |
||||
|
||||
// 传入username,返回对应的username和password
|
||||
@Override |
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
||||
QueryWrapper<User> qw = new QueryWrapper<>(); |
||||
qw.eq("username",username); |
||||
User user = userMapper.selectOne(qw); |
||||
if (user == null) { |
||||
throw new RuntimeException("用户不存在!"); |
||||
} |
||||
|
||||
|
||||
return new UserDetailsImpl(user); |
||||
} |
||||
} |
@ -0,0 +1,56 @@ |
||||
package com.kob.backend.controller.service.impl.utils; |
||||
|
||||
import com.kob.backend.controller.pojo.User; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
import lombok.NoArgsConstructor; |
||||
import org.springframework.security.core.GrantedAuthority; |
||||
import org.springframework.security.core.userdetails.UserDetails; |
||||
import org.springframework.security.core.userdetails.UserDetailsService; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
/** |
||||
* @author new |
||||
*/ |
||||
|
||||
@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; |
||||
} |
||||
} |
@ -0,0 +1,73 @@ |
||||
package com.kob.backend.controller.user; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
import com.kob.backend.controller.pojo.User; |
||||
import com.kob.backend.mapper.UserMapper; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
||||
import org.springframework.security.crypto.password.PasswordEncoder; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
import org.springframework.web.bind.annotation.PathVariable; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author new |
||||
*/ |
||||
|
||||
@RestController |
||||
public class UserController { |
||||
|
||||
@Autowired |
||||
UserMapper userMapper; |
||||
|
||||
// 返回user表的所有数据
|
||||
@GetMapping("/user/all/") |
||||
public List<User> getAll() { |
||||
|
||||
return userMapper.selectList(null); |
||||
} |
||||
|
||||
// 返回指定id的用户信息
|
||||
@GetMapping("/user/{userId}/") |
||||
public User getUser(@PathVariable int userId) { |
||||
|
||||
QueryWrapper<User> qw = new QueryWrapper<>(); |
||||
// 返回指定id的用户信息
|
||||
// return userMapper.selectById(userId);
|
||||
|
||||
// 等价于上面的查询语句
|
||||
qw.eq("id",userId); |
||||
|
||||
// 返回编号大于等于2小于等于3的所有用户信息
|
||||
// qw.ge("id",2).le("id",3);
|
||||
return userMapper.selectOne(qw); |
||||
} |
||||
|
||||
// 添加用户
|
||||
@GetMapping("/user/add/{userid}/{username}/{password}/") |
||||
public String addUser(@PathVariable int userid, |
||||
@PathVariable String username, |
||||
@PathVariable String password) { |
||||
// 存入数据库直接存入的是密文,不存明文密码
|
||||
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); |
||||
String encodedPassword = passwordEncoder.encode(password); |
||||
User user = new User(userid,username,encodedPassword); |
||||
userMapper.insert(user); |
||||
return "Add User Successfully!"; |
||||
|
||||
} |
||||
|
||||
// 删除用户
|
||||
|
||||
@GetMapping("/user/delete/{userid}") |
||||
public String deleteUser(@PathVariable int userid) { |
||||
userMapper.deleteById(userid); |
||||
return "Delete User Successfully!"; |
||||
} |
||||
|
||||
|
||||
} |
||||
|
@ -0,0 +1,13 @@ |
||||
package com.kob.backend.mapper; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import com.kob.backend.controller.pojo.User; |
||||
import org.apache.ibatis.annotations.Mapper; |
||||
|
||||
|
||||
/** |
||||
* @author new |
||||
*/ |
||||
@Mapper |
||||
public interface UserMapper extends BaseMapper<User> { |
||||
} |
@ -1 +1,5 @@ |
||||
server.port=3000 |
||||
spring.datasource.username=root |
||||
spring.datasource.password=zfp251217 |
||||
spring.datasource.url=jdbc:mysql://101.33.213.197:3307/kob?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8 |
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver |
Loading…
Reference in new issue