浏览代码

登录鉴权等接口

1018653686@qq.com 1 年之前
父节点
当前提交
d4a44bc35f

文件差异内容过多而无法显示
+ 0 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/a-json/WebsiteUsersApi.json


+ 13 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/anno/LoginValid.java

@@ -0,0 +1,13 @@
+package com.fjhx.xmhjc.anno;
+
+import java.lang.annotation.*;
+
+/**
+ * @author hj
+ * @date 2023年11月17日 10:19
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+public @interface LoginValid {
+}

+ 47 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/aspect/LoginValidAspect.java

@@ -0,0 +1,47 @@
+package com.fjhx.xmhjc.aspect;
+
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import com.fjhx.xmhjc.anno.LoginValid;
+import com.fjhx.xmhjc.constants.LoginConstant;
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.ServletUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * @author hj
+ * @date 2023年11月17日 10:20
+ */
+@Aspect
+@Component
+public class LoginValidAspect {
+    @Resource
+    private RedisCache redisCache;
+
+    @Pointcut("@annotation(loginValid)")
+    public void loginValidPointcut(LoginValid loginValid) {
+        System.out.println("loginValidPointcut");
+    }
+
+    @Before("@annotation(loginValid)")
+    public void loginValidBefore(LoginValid loginValid) {
+        HttpServletRequest request = ServletUtils.getRequest();
+        String token = request.getHeader(LoginConstant.LONGIN_HEAD);
+        if (StrUtil.isBlank(token)) {
+            throw new RuntimeException("请先登录");
+        }
+        WebsiteUsers websiteUsers = redisCache.getCacheObject(LoginConstant.TOKEN_PREFIX+token);
+        if (ObjectUtil.isNull(websiteUsers)) {
+            throw new RuntimeException("请先登录");
+        }
+    }
+
+}

+ 14 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/constants/LoginConstant.java

@@ -0,0 +1,14 @@
+package com.fjhx.xmhjc.constants;
+
+/**
+ * @author hj
+ * @date 2023年11月17日 9:59
+ */
+public class LoginConstant {
+    public static final String SOLID = "poi123hjkbnmqa@";
+    public static final Integer LOGIN_EXPIRED = 6;
+
+    public static final String LONGIN_HEAD = "website_token";
+
+    public static final String TOKEN_PREFIX = "website_token:";
+}

+ 79 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/controller/open/OpenWesiteUserController.java

@@ -0,0 +1,79 @@
+package com.fjhx.xmhjc.controller.open;
+
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.StrUtil;
+import com.fjhx.xmhjc.anno.LoginValid;
+import com.fjhx.xmhjc.constants.LoginConstant;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersDto;
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import com.fjhx.xmhjc.service.website.WebsiteUsersService;
+import com.fjhx.xmhjc.utils.WebsiteUserUtil;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.sign.Md5Utils;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author hj
+ * @date 2023年11月17日 9:24
+ */
+@RestController
+@RequestMapping("/open/website/user")
+public class OpenWesiteUserController {
+    @Resource
+    private WebsiteUsersService websiteUsersService;
+    @Resource
+    private RedisCache redisCache;
+    /**
+     * 注册
+     * @author hj
+     * @date 2023/11/17 9:25
+     */
+    @PostMapping("/register")
+    public void register(@RequestBody WebsiteUsersDto websiteUsersDto){
+        if(StrUtil.isBlank(websiteUsersDto.getUserName()) || StrUtil.isBlank(websiteUsersDto.getPassword())){
+            throw new ServiceException("参数错误");
+        }
+        if(!websiteUsersDto.getPassword().equals(websiteUsersDto.getConfirmPassword())){
+            throw new ServiceException("两次密码不一致");
+        }
+        websiteUsersService.getByUserName(websiteUsersDto.getUserName()).ifPresent(o->{
+            throw new ServiceException("用户名已存在");
+        });
+        websiteUsersService.register(websiteUsersDto);
+    }
+
+    /**
+     * 登录
+     * @param websiteUsersDto
+     */
+    @PostMapping("/login")
+    public String login(@RequestBody WebsiteUsersDto websiteUsersDto){
+        if(StrUtil.isBlank(websiteUsersDto.getUserName()) || StrUtil.isBlank(websiteUsersDto.getPassword())){
+            throw new ServiceException("参数错误");
+        }
+        WebsiteUsers websiteUsers = websiteUsersService.getByUserName(websiteUsersDto.getUserName()).orElseThrow(()->new ServiceException("用户名不存在"));
+        if(!websiteUsers.getPassword().equals(Md5Utils.hash(LoginConstant.SOLID+websiteUsersDto.getPassword()))){
+            throw new ServiceException("密码错误");
+        }
+        String token = IdUtil.fastSimpleUUID();
+        redisCache.setCacheObject(LoginConstant.TOKEN_PREFIX+token, websiteUsers, LoginConstant.LOGIN_EXPIRED, TimeUnit.HOURS);
+        return token;
+    }
+
+
+    @LoginValid
+    @PostMapping("/logout")
+    public void logout(){
+        String websiteUserToken = WebsiteUserUtil.getWebsiteUserToken();
+        redisCache.deleteObject(websiteUserToken);
+    }
+
+
+}

+ 77 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/controller/website/WebsiteUsersController.java

@@ -0,0 +1,77 @@
+package com.fjhx.xmhjc.controller.website;
+
+import org.springframework.web.bind.annotation.*;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.fjhx.xmhjc.entity.website.vo.WebsiteUsersVo;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersSelectDto;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersDto;
+import com.ruoyi.common.core.domain.BaseSelectDto;
+import com.fjhx.xmhjc.service.website.WebsiteUsersService;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 首页用户 前端控制器
+ * </p>
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+@RestController
+@RequestMapping("/websiteUsers")
+public class WebsiteUsersController {
+
+    @Autowired
+    private WebsiteUsersService websiteUsersService;
+
+    /**
+     * 首页用户列表
+     */
+    @PostMapping("/list")
+    public List<WebsiteUsersVo> list(@RequestBody WebsiteUsersSelectDto dto) {
+        return websiteUsersService.getList(dto);
+    }
+
+    /**
+     * 首页用户分页
+     */
+    @PostMapping("/page")
+    public Page<WebsiteUsersVo> page(@RequestBody WebsiteUsersSelectDto dto) {
+        return websiteUsersService.getPage(dto);
+    }
+
+    /**
+     * 首页用户明细
+     */
+    @PostMapping("/detail")
+    public WebsiteUsersVo detail(@RequestBody BaseSelectDto dto) {
+        return websiteUsersService.detail(dto.getId());
+    }
+
+    /**
+     * 首页用户新增
+     */
+    @PostMapping("/add")
+    public void add(@RequestBody WebsiteUsersDto websiteUsersDto) {
+        websiteUsersService.add(websiteUsersDto);
+    }
+
+    /**
+     * 首页用户编辑
+     */
+    @PostMapping("/edit")
+    public void edit(@RequestBody WebsiteUsersDto websiteUsersDto) {
+        websiteUsersService.edit(websiteUsersDto);
+    }
+
+    /**
+     * 首页用户删除
+     */
+    @PostMapping("/delete")
+    public void delete(@RequestBody BaseSelectDto dto) {
+        websiteUsersService.delete(dto.getId());
+    }
+
+}

+ 17 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/website/dto/WebsiteUsersDto.java

@@ -0,0 +1,17 @@
+package com.fjhx.xmhjc.entity.website.dto;
+
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 首页用户新增编辑入参实体
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+@Getter
+@Setter
+public class WebsiteUsersDto extends WebsiteUsers {
+    private String confirmPassword;
+}

+ 17 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/website/dto/WebsiteUsersSelectDto.java

@@ -0,0 +1,17 @@
+package com.fjhx.xmhjc.entity.website.dto;
+
+import com.ruoyi.common.core.domain.BaseSelectDto;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 首页用户列表查询入参实体
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+@Getter
+@Setter
+public class WebsiteUsersSelectDto extends BaseSelectDto {
+
+}

+ 28 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/website/po/WebsiteUsers.java

@@ -0,0 +1,28 @@
+package com.fjhx.xmhjc.entity.website.po;
+
+import com.ruoyi.common.core.domain.BasePo;
+import com.baomidou.mybatisplus.annotation.TableName;
+import java.util.Date;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * <p>
+ * 首页用户
+ * </p>
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+@Getter
+@Setter
+@TableName("website_users")
+public class WebsiteUsers extends BasePo {
+
+    private String userName;
+
+    private String password;
+
+    private String solid;
+
+}

+ 17 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/entity/website/vo/WebsiteUsersVo.java

@@ -0,0 +1,17 @@
+package com.fjhx.xmhjc.entity.website.vo;
+
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 首页用户列表查询返回值实体
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+@Getter
+@Setter
+public class WebsiteUsersVo extends WebsiteUsers {
+
+}

+ 32 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/mapper/website/WebsiteUsersMapper.java

@@ -0,0 +1,32 @@
+package com.fjhx.xmhjc.mapper.website;
+
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.fjhx.xmhjc.entity.website.vo.WebsiteUsersVo;
+import com.ruoyi.common.utils.wrapper.IWrapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * <p>
+ * 首页用户 Mapper 接口
+ * </p>
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+public interface WebsiteUsersMapper extends BaseMapper<WebsiteUsers> {
+
+    /**
+     * 首页用户列表
+     */
+    List<WebsiteUsersVo> getList(@Param("ew") IWrapper<WebsiteUsers> wrapper);
+
+    /**
+     * 首页用户分页
+     */
+    Page<WebsiteUsersVo> getPage(@Param("page") Page<Object> page, @Param("ew") IWrapper<WebsiteUsers> wrapper);
+
+}

+ 62 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/service/website/WebsiteUsersService.java

@@ -0,0 +1,62 @@
+package com.fjhx.xmhjc.service.website;
+
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import com.ruoyi.common.core.service.BaseService;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.fjhx.xmhjc.entity.website.vo.WebsiteUsersVo;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersSelectDto;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersDto;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * <p>
+ * 首页用户 服务类
+ * </p>
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+public interface WebsiteUsersService extends BaseService<WebsiteUsers> {
+
+    /**
+     * 首页用户列表
+     */
+    List<WebsiteUsersVo> getList(WebsiteUsersSelectDto dto);
+
+    /**
+     * 首页用户分页
+     */
+    Page<WebsiteUsersVo> getPage(WebsiteUsersSelectDto dto);
+
+    /**
+     * 首页用户明细
+     */
+    WebsiteUsersVo detail(Long id);
+
+    /**
+     * 首页用户新增
+     */
+    void add(WebsiteUsersDto websiteUsersDto);
+
+    /**
+     * 首页用户编辑
+     */
+    void edit(WebsiteUsersDto websiteUsersDto);
+
+    /**
+     * 首页用户删除
+     */
+    void delete(Long id);
+
+    /**
+     * 注册用户
+     * @author hj
+     * @date 2023/11/17 9:43
+     * @param websiteUsersDto 
+     */
+    void register(WebsiteUsersDto websiteUsersDto);
+
+    Optional<WebsiteUsers> getByUserName(String userName);
+}

+ 81 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/service/website/impl/WebsiteUsersServiceImpl.java

@@ -0,0 +1,81 @@
+package com.fjhx.xmhjc.service.website.impl;
+
+import com.fjhx.xmhjc.constants.LoginConstant;
+import com.fjhx.xmhjc.entity.website.po.WebsiteUsers;
+import com.fjhx.xmhjc.mapper.website.WebsiteUsersMapper;
+import com.fjhx.xmhjc.service.website.WebsiteUsersService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.sign.Md5Utils;
+import org.springframework.stereotype.Service;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.fjhx.xmhjc.entity.website.vo.WebsiteUsersVo;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersSelectDto;
+import com.ruoyi.common.utils.wrapper.IWrapper;
+import com.fjhx.xmhjc.entity.website.dto.WebsiteUsersDto;
+import cn.hutool.core.bean.BeanUtil;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * <p>
+ * 首页用户 服务实现类
+ * </p>
+ *
+ * @author hj
+ * @since 2023-11-17
+ */
+@Service
+public class WebsiteUsersServiceImpl extends ServiceImpl<WebsiteUsersMapper, WebsiteUsers> implements WebsiteUsersService {
+
+    @Override
+    public List<WebsiteUsersVo> getList(WebsiteUsersSelectDto dto) {
+        IWrapper<WebsiteUsers> wrapper = getWrapper();
+        wrapper.orderByDesc("wu", WebsiteUsers::getId);
+        List<WebsiteUsersVo> list = this.baseMapper.getList(wrapper);
+        return list;
+    }
+
+    @Override
+    public Page<WebsiteUsersVo> getPage(WebsiteUsersSelectDto dto) {
+        IWrapper<WebsiteUsers> wrapper = getWrapper();
+        wrapper.orderByDesc("wu", WebsiteUsers::getId);
+        Page<WebsiteUsersVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
+        return page;
+    }
+
+    @Override
+    public WebsiteUsersVo detail(Long id) {
+        WebsiteUsers WebsiteUsers = this.getById(id);
+        WebsiteUsersVo result = BeanUtil.toBean(WebsiteUsers, WebsiteUsersVo.class);
+        return result;
+    }
+
+    @Override
+    public void add(WebsiteUsersDto websiteUsersDto) {
+        this.save(websiteUsersDto);
+    }
+
+    @Override
+    public void edit(WebsiteUsersDto websiteUsersDto) {
+        this.updateById(websiteUsersDto);
+    }
+
+    @Override
+    public void delete(Long id) {
+        this.removeById(id);
+    }
+
+    @Override
+    public void register(WebsiteUsersDto websiteUsersDto) {
+        websiteUsersDto.setSolid(LoginConstant.SOLID);
+        websiteUsersDto.setPassword(Md5Utils.hash(LoginConstant.SOLID + websiteUsersDto.getPassword()));
+        this.save(websiteUsersDto);
+    }
+
+    @Override
+    public Optional<WebsiteUsers> getByUserName(String userName) {
+        return this.lambdaQuery().eq(WebsiteUsers::getUserName, userName).oneOpt();
+    }
+}

+ 17 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/utils/WebsiteUserUtil.java

@@ -0,0 +1,17 @@
+package com.fjhx.xmhjc.utils;
+
+import com.fjhx.xmhjc.constants.LoginConstant;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.ServletUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
+
+/**
+ * @author hj
+ * @date 2023年11月17日 10:31
+ */
+public class WebsiteUserUtil {
+    public static String getWebsiteUserToken() {
+        String token = ServletUtils.getRequest().getHeader(LoginConstant.LONGIN_HEAD);
+        return token;
+    }
+}

+ 32 - 0
hx-xmhjc/src/main/resources/mapper/website/WebsiteUsersMapper.xml

@@ -0,0 +1,32 @@
+<?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.fjhx.xmhjc.mapper.website.WebsiteUsersMapper">
+    <select id="getList" resultType="com.fjhx.xmhjc.entity.website.vo.WebsiteUsersVo">
+        select
+            wu.id,
+            wu.user_name,
+            wu.password,
+            wu.solid,
+            wu.create_user,
+            wu.create_time,
+            wu.update_user,
+            wu.update_time
+        from website_users wu
+            ${ew.customSqlSegment}
+    </select>
+
+    <select id="getPage" resultType="com.fjhx.xmhjc.entity.website.vo.WebsiteUsersVo">
+        select
+            wu.id,
+            wu.user_name,
+            wu.password,
+            wu.solid,
+            wu.create_user,
+            wu.create_time,
+            wu.update_user,
+            wu.update_time
+        from website_users wu
+            ${ew.customSqlSegment}
+    </select>
+
+</mapper>

部分文件因为文件数量过多而无法显示