Skip to content

Commit

Permalink
新增saToken项目的工程
Browse files Browse the repository at this point in the history
  • Loading branch information
pcm committed Nov 25, 2024
1 parent ae7602d commit 9605e6d
Show file tree
Hide file tree
Showing 34 changed files with 2,946 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.pancm.config;


import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.stp.StpUtil;

import com.pancm.interceptors.PermissionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/***
* 登录权限验证
*/
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {


@Resource
private PermissionInterceptor permissionInterceptor;

// 需要排除的路径
private static final String[] EXCLUDE_PATHS = {"/login/**"};

// 注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registerSaTokenInterceptor(registry);
registerPermissionInterceptor(registry);
}

// 注册 Sa-Token 拦截器
private void registerSaTokenInterceptor(InterceptorRegistry registry) {
registry.addInterceptor(new SaRouteInterceptor((r, s, o) -> StpUtil.checkLogin()))
.addPathPatterns("/**")
.excludePathPatterns(EXCLUDE_PATHS);
}

// 注册权限拦截器
private void registerPermissionInterceptor(InterceptorRegistry registry) {
registry.addInterceptor(permissionInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(EXCLUDE_PATHS);
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.pancm.constant;

public interface UserConstants {

String CURRENT_USER = "current_user";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.pancm.controller;


import cn.dev33.satoken.stp.StpUtil;

import com.pancm.constant.UserConstants;
import com.pancm.enums.PcmYesNoEnum;
import com.pancm.service.ISysPermissionService;
import com.pancm.service.ISysUserInfoService;
import com.pancm.service.ISysUserRoleService;
import com.pancm.vo.ApiResult;
import com.pancm.vo.SysPermissionVO;
import com.pancm.vo.SysUserInfoVO;
import com.pancm.vo.SysUserRoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* <p>
* 用户登录 controller
* </p>
*
* @author pcm
* @menu 用户登录
* @since 2024-11-20
*/
@Api(tags = "REST - 用户登录")
@RestController
@RequestMapping("/login")
public class LoginController {

@Resource
private ISysUserInfoService sysUserInfoService;
@Resource
private ISysUserRoleService userRoleService;
@Resource
private ISysPermissionService permissionService;


@PostMapping("/login")
@ApiOperation(value = "登录", notes = "登录")
public ApiResult login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
SysUserInfoVO userInfoVO = sysUserInfoService.queryByUserName(username);
if (userInfoVO == null || !Objects.equals(userInfoVO.getPassword(), password)) {
return ApiResult.error("用户名或密码错误!");
}
if(Objects.equals(userInfoVO.getUserEnable(), PcmYesNoEnum.NO.getIndex())){
return ApiResult.error("该用户已被禁用!请联系管理员!");
}
SysUserRoleVO roleDTO = userRoleService.queryById(userInfoVO.getRoleId());
if(roleDTO == null || Objects.equals(roleDTO.getUserEnable(), PcmYesNoEnum.NO.getIndex())){
return ApiResult.error("该用户所属角色已被禁用或已删除!请联系管理员!");
}
List<SysPermissionVO> list = permissionService.queryRolePermissionByRoleId(userInfoVO.getRoleId());
if(list == null || list.isEmpty()){
return ApiResult.error("该用户所属角色未配置权限!请联系管理员!");
}
userInfoVO.setPermissionDTOLists(list);
userInfoVO.setPassword(null);
StpUtil.login(username);
StpUtil.getSession().set(UserConstants.CURRENT_USER, userInfoVO);
return ApiResult.success(userInfoVO);
}

@ApiOperation(value = "登录校验", notes = "登录校验")
@RequestMapping("/isLogin")
public ApiResult isLogin() {
return ApiResult.success(StpUtil.isLogin());
}

@ApiOperation(value = "登出", notes = "登出")
@PostMapping("/logout")
public ApiResult logout() {
StpUtil.getSession().clear();
StpUtil.logout();
return ApiResult.success();
}


}


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pancm.controller;

import com.pancm.vo.TSysUserVO;
import com.pancm.service.ITSysUserService;
import com.pancm.vo.SysPermissionVO;
import com.pancm.service.ISysPermissionService;
import org.springframework.web.bind.annotation.*;


Expand All @@ -19,34 +19,34 @@


/**
* @Title: 用户表(TSysUser)表控制层
* @Title: 权限表(SysPermission)表控制层
* @Description:
* @Version:1.0.0
* @Since:jdk1.8
* @author pancm
* @date 2024-04-01 09:30:51
* @date 2024-11-23 09:31:42
*/
@Api(tags = "用户表(TSysUser)")
@Api(tags = "权限表(SysPermission)")
@RestController
@RequestMapping("tSysUser")
public class TSysUserController {
@RequestMapping("sysPermission")
public class SysPermissionController {
/**
* 服务对象
*/
@Autowired
private ITSysUserService tSysUserService;
private ISysPermissionService sysPermissionService;


/**
* 新增一条数据
*
* @param tSysUserVO 实体类
* @param sysPermissionVO 实体类
* @return Response对象
*/
@ApiOperation(value = "用户表新增",notes = "用户表新增")
@ApiOperation(value = "权限表新增",notes = "权限表新增")
@RequestMapping(value = "save", method = RequestMethod.POST)
public ApiResult insert(@RequestBody TSysUserVO tSysUserVO, HttpServletRequest httpRequest) {
int result = tSysUserService.insert(tSysUserVO);
public ApiResult insert(@RequestBody SysPermissionVO sysPermissionVO, HttpServletRequest httpRequest) {
int result = sysPermissionService.insert(sysPermissionVO);
if (result > 0) {
return ApiResult.success();
}
Expand All @@ -56,26 +56,26 @@ public ApiResult insert(@RequestBody TSysUserVO tSysUserVO, HttpServletRequest h
/**
* 修改一条数据
*
* @param tSysUserVO 实体类
* @param sysPermissionVO 实体类
* @return Response对象
*/
@ApiOperation(value = "用户表修改",notes = "用户表修改")
@ApiOperation(value = "权限表修改",notes = "权限表修改")
@RequestMapping(value = "edit", method = RequestMethod.POST)
public ApiResult update(@RequestBody TSysUserVO tSysUserVO, HttpServletRequest httpRequest) {
tSysUserService.update(tSysUserVO);
public ApiResult update(@RequestBody SysPermissionVO sysPermissionVO, HttpServletRequest httpRequest) {
sysPermissionService.update(sysPermissionVO);
return ApiResult.success();
}

/**
* 删除一条数据
*
* @param tSysUserVO 参数对象
* @param sysPermissionVO 参数对象
* @return Response对象
*/
@ApiOperation(value = "用户表删除",notes = "用户表删除")
@ApiOperation(value = "权限表删除",notes = "权限表删除")
@RequestMapping(value = "del", method = RequestMethod.POST)
public ApiResult delete(@RequestBody TSysUserVO tSysUserVO, HttpServletRequest httpRequest) {
tSysUserService.deleteById(tSysUserVO.getId());
public ApiResult delete(@RequestBody SysPermissionVO sysPermissionVO, HttpServletRequest httpRequest) {
sysPermissionService.deleteById(sysPermissionVO.getId());
return ApiResult.success();
}

Expand All @@ -85,19 +85,19 @@ public ApiResult delete(@RequestBody TSysUserVO tSysUserVO, HttpServletRequest h
* 分页查询
*
*/
@ApiOperation(value = "用户表查询",notes = "用户表查询")
@ApiOperation(value = "权限表查询",notes = "权限表查询")
@RequestMapping(value = "list", method = RequestMethod.POST)
public ApiResult list(@RequestBody TSysUserVO tSysUserVO) {
return tSysUserService.list(tSysUserVO);
public ApiResult list(@RequestBody SysPermissionVO sysPermissionVO) {
return sysPermissionService.list(sysPermissionVO);
}

/**
* 详情查询
*
*/
@ApiOperation(value = "用户表详情",notes = "用户表详情")
@ApiOperation(value = "权限表详情",notes = "权限表详情")
@RequestMapping(value = "view", method = RequestMethod.GET)
public ApiResult view( @RequestParam("id") String id) {
return ApiResult.success(tSysUserService.queryById(id));
public ApiResult view( @RequestParam("id") Integer id) {
return ApiResult.success(sysPermissionService.queryById(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.pancm.controller;

import com.pancm.vo.SysUserInfoVO;
import com.pancm.service.ISysUserInfoService;
import org.springframework.web.bind.annotation.*;


import com.pancm.vo.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;



/**
* @Title: 系统用户表(SysUserInfo)表控制层
* @Description:
* @Version:1.0.0
* @Since:jdk1.8
* @author pancm
* @date 2024-11-23 09:31:42
*/
@Api(tags = "系统用户表(SysUserInfo)")
@RestController
@RequestMapping("sysUserInfo")
public class SysUserInfoController {
/**
* 服务对象
*/
@Autowired
private ISysUserInfoService sysUserInfoService;


/**
* 新增一条数据
*
* @param sysUserInfoVO 实体类
* @return Response对象
*/
@ApiOperation(value = "系统用户表新增",notes = "系统用户表新增")
@RequestMapping(value = "save", method = RequestMethod.POST)
public ApiResult insert(@RequestBody SysUserInfoVO sysUserInfoVO, HttpServletRequest httpRequest) {
int result = sysUserInfoService.insert(sysUserInfoVO);
if (result > 0) {
return ApiResult.success();
}
return ApiResult.error("新增失败");
}

/**
* 修改一条数据
*
* @param sysUserInfoVO 实体类
* @return Response对象
*/
@ApiOperation(value = "系统用户表修改",notes = "系统用户表修改")
@RequestMapping(value = "edit", method = RequestMethod.POST)
public ApiResult update(@RequestBody SysUserInfoVO sysUserInfoVO, HttpServletRequest httpRequest) {
sysUserInfoService.update(sysUserInfoVO);
return ApiResult.success();
}

/**
* 删除一条数据
*
* @param sysUserInfoVO 参数对象
* @return Response对象
*/
@ApiOperation(value = "系统用户表删除",notes = "系统用户表删除")
@RequestMapping(value = "del", method = RequestMethod.POST)
public ApiResult delete(@RequestBody SysUserInfoVO sysUserInfoVO, HttpServletRequest httpRequest) {
sysUserInfoService.deleteById(sysUserInfoVO.getId());
return ApiResult.success();
}



/**
* 分页查询
*
*/
@ApiOperation(value = "系统用户表查询",notes = "系统用户表查询")
@RequestMapping(value = "list", method = RequestMethod.POST)
public ApiResult list(@RequestBody SysUserInfoVO sysUserInfoVO) {
return sysUserInfoService.list(sysUserInfoVO);
}

/**
* 详情查询
*
*/
@ApiOperation(value = "系统用户表详情",notes = "系统用户表详情")
@RequestMapping(value = "view", method = RequestMethod.GET)
public ApiResult view( @RequestParam("id") Integer id) {
return ApiResult.success(sysUserInfoService.queryById(id));
}
}
Loading

0 comments on commit 9605e6d

Please sign in to comment.