第一次代码提交
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package com.gw.test.controller;
|
||||
|
||||
|
||||
import com.gw.test.domain.Result;
|
||||
import com.gw.test.domain.vo.LoginVo;
|
||||
import com.gw.test.entity.GuoWei;
|
||||
import com.gw.test.service.IGuoWeiService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author gw
|
||||
* @since 2024-09-04
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/guowei")
|
||||
@Tag(name = "用户管理",description = "用户管理接口")
|
||||
public class GuoWeiController {
|
||||
|
||||
private final IGuoWeiService service;
|
||||
|
||||
@Operation(summary = "用户列表",description = "用户列表接口")
|
||||
@PostMapping("/list")
|
||||
public Result<List<GuoWei>> list(){
|
||||
return Result.ok(service.list());
|
||||
}
|
||||
|
||||
@Operation(summary = "添加用户",description = "添加用户接口")
|
||||
@PostMapping("/save")
|
||||
public Result<Void> save(GuoWei guoWei) {
|
||||
service.save(guoWei);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "删除用户",description = "删除用户接口")
|
||||
@PostMapping("/delete")
|
||||
public Result<Void> delete(Integer id) {
|
||||
service.removeById(id);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "用户修改",description = "用户列修改口")
|
||||
@PostMapping("/update")
|
||||
public Result<Void> update(GuoWei guoWei) {
|
||||
service.updateById(guoWei);
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
@Operation(summary = "用户登录",description = "用户登录接口")
|
||||
@PostMapping("/login")
|
||||
public Result<LoginVo> login(String username, String password) {
|
||||
return Result.ok(service.login(username, password));
|
||||
}
|
||||
|
||||
@Operation(summary = "用户注册",description = "用户注册接口")
|
||||
@PostMapping("/register")
|
||||
public Result<Void> register(GuoWei guoWei) {
|
||||
boolean re = service.register(guoWei);
|
||||
if (re)
|
||||
return Result.ok();
|
||||
else
|
||||
return Result.error("注册失败");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.gw.test.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.io.Serializable;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author gw
|
||||
* @since 2024-09-04
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("guo_wei")
|
||||
@Schema($schema = "GuoWei对象",description = "")
|
||||
public class GuoWei implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema($schema = "主键")
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
@Schema($schema = "用户名")
|
||||
private String username;
|
||||
|
||||
@Schema($schema = "密码")
|
||||
private String password;
|
||||
|
||||
@Schema($schema = "创建时间")
|
||||
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||
private String createTime;
|
||||
|
||||
@Schema($schema = "手机号")
|
||||
private String phoneNumber;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.gw.test.mapper;
|
||||
|
||||
import com.gw.test.entity.GuoWei;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author gw
|
||||
* @since 2024-09-04
|
||||
*/
|
||||
//@Mapper
|
||||
public interface GuoWeiMapper extends BaseMapper<GuoWei> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.gw.test.service;
|
||||
|
||||
import com.gw.test.domain.vo.LoginVo;
|
||||
import com.gw.test.entity.GuoWei;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author gw
|
||||
* @since 2024-09-04
|
||||
*/
|
||||
public interface IGuoWeiService extends IService<GuoWei> {
|
||||
|
||||
UserDetails loadUserByUsername(String s);
|
||||
|
||||
LoginVo login(String username, String password);
|
||||
|
||||
boolean register(GuoWei guoWei);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.gw.test.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.gw.test.domain.vo.LoginVo;
|
||||
import com.gw.test.entity.GuoWei;
|
||||
import com.gw.test.entity.User;
|
||||
import com.gw.test.mapper.GuoWeiMapper;
|
||||
import com.gw.test.service.IGuoWeiService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.gw.test.utils.JWTUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author gw
|
||||
* @since 2024-09-04
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class GuoWeiServiceImpl extends ServiceImpl<GuoWeiMapper, GuoWei> implements IGuoWeiService {
|
||||
private final JWTUtil JWTUtil;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String s) {
|
||||
QueryWrapper<GuoWei> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("username", s);
|
||||
List<GuoWei> list = list(wrapper);
|
||||
if (list.isEmpty()) return null;
|
||||
GuoWei guoWei = list.get(0);
|
||||
return new User(guoWei, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginVo login(String username, String password) {
|
||||
User user = (User) loadUserByUsername(username);
|
||||
if (user == null) return null;
|
||||
|
||||
if (!passwordEncoder.matches(password, user.getPassword())) return null;
|
||||
|
||||
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
LoginVo re = new LoginVo();
|
||||
|
||||
re.setId(user.getEntity().getId());
|
||||
re.setUsername(user.getUsername());
|
||||
re.setToken(JWTUtil.generateToken(user));
|
||||
|
||||
return re;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean register(GuoWei guoWei) {
|
||||
Long count = lambdaQuery()
|
||||
.eq(GuoWei::getUsername, guoWei.getUsername())
|
||||
.count();
|
||||
|
||||
if (count > 0) return false;
|
||||
|
||||
guoWei.setPassword(passwordEncoder.encode(guoWei.getPassword()));
|
||||
return save(guoWei);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.gw.test.mapper.GuoWeiMapper">
|
||||
|
||||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="com.gw.test.entity.GuoWei">
|
||||
<id column="id" property="id" />
|
||||
<result column="username" property="username" />
|
||||
<result column="password" property="password" />
|
||||
<result column="create_time" property="createTime" />
|
||||
<result column="phone_number" property="phoneNumber" />
|
||||
</resultMap>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user