Commit 1ed893a2 authored by liuyang's avatar liuyang

updated

parent 7bbe84e0
package com.qkdata.biz.common;
public class BizConstants {
/**
* 系统管理员
*/
public static final String ROLE_ADMIN = "ADMIN";
/**
* 运营人员
*/
public static final String ROLE_OPERATOR = "OPERATOR";
/**
* 企业管理员
*/
public static final String ROLE_ENTERPRISE_ADMIN = "ENTERPRISE_ADMIN";
/**
* 培训机构管理员
*/
public static final String ROLE_INSTITUTION_ADMIN = "INSTITUTION_ADMIN";
/**
* 普通用户
*/
public static final String ROLE_USER = "USER";
}
package com.qkdata.biz.sys.controller; package com.qkdata.biz.sys.controller;
import com.qkdata.biz.common.BizConstants;
import com.qkdata.biz.sys.entity.SysUserPO; import com.qkdata.biz.sys.entity.SysUserPO;
import com.qkdata.biz.sys.service.SysUserService; import com.qkdata.biz.sys.service.SysUserService;
import com.qkdata.biz.sys.vo.PasswordModel; import com.qkdata.biz.sys.vo.PasswordModel;
...@@ -17,6 +18,7 @@ import io.swagger.annotations.Api; ...@@ -17,6 +18,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions; import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
...@@ -34,7 +36,7 @@ public class SysUserController { ...@@ -34,7 +36,7 @@ public class SysUserController {
@ApiOperation("查询用户列表") @ApiOperation("查询用户列表")
@PostMapping("/list") @PostMapping("/list")
@RequiresPermissions("sys:user:list") @RequiresRoles(value = {"ADMIN"})
public PageResult<SysUserModel> list(@RequestBody QueryUserModel queryUserModel){ public PageResult<SysUserModel> list(@RequestBody QueryUserModel queryUserModel){
return sysUserService.queryPageList(queryUserModel); return sysUserService.queryPageList(queryUserModel);
} }
...@@ -50,7 +52,7 @@ public class SysUserController { ...@@ -50,7 +52,7 @@ public class SysUserController {
} }
@ApiOperation("获取某个用户信息") @ApiOperation("获取某个用户信息")
@GetMapping("/info/{id}") @GetMapping("/info/{id}")
@RequiresPermissions("sys:user:info") @RequiresRoles(value = {"ADMIN"})
public Result<SysUserModel> infoById(@PathVariable Long id){ public Result<SysUserModel> infoById(@PathVariable Long id){
SysUserPO sysUserPO = sysUserService.getById(id); SysUserPO sysUserPO = sysUserService.getById(id);
SysUserModel dto = new SysUserModel(); SysUserModel dto = new SysUserModel();
...@@ -61,7 +63,7 @@ public class SysUserController { ...@@ -61,7 +63,7 @@ public class SysUserController {
@ApiOperation("保存用户信息") @ApiOperation("保存用户信息")
@SysLog("保存用户信息") @SysLog("保存用户信息")
@PostMapping("/save") @PostMapping("/save")
@RequiresPermissions("sys:user:save") @RequiresRoles(value = {"ADMIN"})
public Result<String> save(@RequestBody @Validated(AddGroup.class) SysUserModel sysUserModel){ public Result<String> save(@RequestBody @Validated(AddGroup.class) SysUserModel sysUserModel){
sysUserService.saveUser(sysUserModel); sysUserService.saveUser(sysUserModel);
return Result.succeed("ok"); return Result.succeed("ok");
...@@ -69,7 +71,7 @@ public class SysUserController { ...@@ -69,7 +71,7 @@ public class SysUserController {
@ApiOperation("修改用户信息") @ApiOperation("修改用户信息")
@SysLog("修改用户信息") @SysLog("修改用户信息")
@PostMapping("/update") @PostMapping("/update")
@RequiresPermissions("sys:user:update") @RequiresRoles(value = {BizConstants.ROLE_ADMIN})
public Result<String> update(@RequestBody @Validated(UpdateGroup.class) SysUserModel sysUserModel){ public Result<String> update(@RequestBody @Validated(UpdateGroup.class) SysUserModel sysUserModel){
sysUserService.updateUser(sysUserModel); sysUserService.updateUser(sysUserModel);
return Result.succeed("ok"); return Result.succeed("ok");
...@@ -94,7 +96,7 @@ public class SysUserController { ...@@ -94,7 +96,7 @@ public class SysUserController {
@ApiOperation("删除用户") @ApiOperation("删除用户")
@SysLog("删除用户") @SysLog("删除用户")
@PostMapping("/delete") @PostMapping("/delete")
@RequiresPermissions("sys:user:delete") @RequiresRoles(value = {"ADMIN"})
public Result<String> delete(@RequestBody Long[] userIds){ public Result<String> delete(@RequestBody Long[] userIds){
if(ArrayUtils.contains(userIds, 1L)){ if(ArrayUtils.contains(userIds, 1L)){
throw new BusinessException("系统管理员不能删除"); throw new BusinessException("系统管理员不能删除");
......
...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; ...@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qkdata.biz.sys.entity.SysRolePO; import com.qkdata.biz.sys.entity.SysRolePO;
import com.qkdata.biz.sys.vo.QueryRoleModel; import com.qkdata.biz.sys.vo.QueryRoleModel;
import com.qkdata.biz.sys.vo.SysRoleModel;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
...@@ -12,4 +13,6 @@ import java.util.List; ...@@ -12,4 +13,6 @@ import java.util.List;
@Mapper @Mapper
public interface SysRoleMapper extends BaseMapper<SysRolePO> { public interface SysRoleMapper extends BaseMapper<SysRolePO> {
List<SysRolePO> queryPageList(Page<SysRolePO> page, @Param("p") QueryRoleModel queryRoleModel); List<SysRolePO> queryPageList(Page<SysRolePO> page, @Param("p") QueryRoleModel queryRoleModel);
List<SysRoleModel> findUserRoles(Long userId);
} }
package com.qkdata.biz.sys.service; package com.qkdata.biz.sys.service;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
...@@ -7,6 +8,7 @@ import com.qkdata.biz.enums.AccountStatusEnum; ...@@ -7,6 +8,7 @@ import com.qkdata.biz.enums.AccountStatusEnum;
import com.qkdata.biz.sys.entity.SysMenuPO; import com.qkdata.biz.sys.entity.SysMenuPO;
import com.qkdata.biz.sys.entity.SysUserPO; import com.qkdata.biz.sys.entity.SysUserPO;
import com.qkdata.biz.sys.vo.LoginUserInfo; import com.qkdata.biz.sys.vo.LoginUserInfo;
import com.qkdata.biz.sys.vo.SysRoleModel;
import com.qkdata.common.base.exception.BusinessException; import com.qkdata.common.base.exception.BusinessException;
import com.qkdata.common.jwt.JWTService; import com.qkdata.common.jwt.JWTService;
import com.qkdata.common.oauth.AuthorizedUser; import com.qkdata.common.oauth.AuthorizedUser;
...@@ -17,6 +19,7 @@ import org.springframework.stereotype.Service; ...@@ -17,6 +19,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Service @Service
public class ShiroService { public class ShiroService {
...@@ -28,6 +31,8 @@ public class ShiroService { ...@@ -28,6 +31,8 @@ public class ShiroService {
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@Autowired @Autowired
private JWTService jwtService; private JWTService jwtService;
@Autowired
private SysRoleService sysRoleService;
public Set<String> getUserPermissions(Long userId) { public Set<String> getUserPermissions(Long userId) {
List<String> permsList; List<String> permsList;
...@@ -61,10 +66,15 @@ public class ShiroService { ...@@ -61,10 +66,15 @@ public class ShiroService {
if (userPO.getStatus() == AccountStatusEnum.DISABLE){ if (userPO.getStatus() == AccountStatusEnum.DISABLE){
throw new BusinessException("帐号已禁用"); throw new BusinessException("帐号已禁用");
} }
if (userPO.getStatus() == AccountStatusEnum.UNACTIVATE){
userPO.setActivateTime(DateUtil.date());
sysUserService.updateById(userPO);
}
String token = generatorToken(userPO); String token = generatorToken(userPO);
LoginUserInfo loginUser = new LoginUserInfo(); LoginUserInfo loginUser = new LoginUserInfo();
BeanUtils.copyProperties(userPO,loginUser); BeanUtils.copyProperties(userPO,loginUser);
loginUser.setAuthorization(token); loginUser.setAuthorization(token);
loginUser.setRoles(sysRoleService.getUserRoles(userPO.getId()));
return loginUser; return loginUser;
} }
...@@ -81,4 +91,9 @@ public class ShiroService { ...@@ -81,4 +91,9 @@ public class ShiroService {
public SysUserPO getUserByUserName(String username) { public SysUserPO getUserByUserName(String username) {
return sysUserService.getByUsername(username); return sysUserService.getByUsername(username);
} }
public Set<String> getUserRoles(Long userId) {
List<SysRoleModel> roles = sysRoleService.getUserRoles(userId);
return roles.stream().map(SysRoleModel::getCode).collect(Collectors.toSet());
}
} }
...@@ -59,4 +59,8 @@ public class SysRoleService extends BaseServiceImpl<SysRoleMapper, SysRolePO> { ...@@ -59,4 +59,8 @@ public class SysRoleService extends BaseServiceImpl<SysRoleMapper, SysRolePO> {
sysRoleMenuService.saveOrUpdateRoleMenu(po.getId(),sysRoleModel.getMenuIdList()); sysRoleMenuService.saveOrUpdateRoleMenu(po.getId(),sysRoleModel.getMenuIdList());
} }
public List<SysRoleModel> getUserRoles(Long userId) {
return baseMapper.findUserRoles(userId);
}
} }
...@@ -2,11 +2,13 @@ package com.qkdata.biz.sys.vo; ...@@ -2,11 +2,13 @@ package com.qkdata.biz.sys.vo;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
public class LoginUserInfo { public class LoginUserInfo {
private Long id; private Long id;
private String username; private String username;
private String email; private String nickName;
private String mobile;
private String authorization; private String authorization;
private List<SysRoleModel> roles;
} }
...@@ -26,34 +26,30 @@ public class Swagger2Config { ...@@ -26,34 +26,30 @@ public class Swagger2Config {
*/ */
@Bean @Bean
public Docket createRestApi() { public Docket createRestApi() {
// ParameterBuilder ticketPar = new ParameterBuilder();
// List<Parameter> pars = new ArrayList<>();
// ticketPar.name(HttpHeaders.AUTHORIZATION).description("user token")
// .modelRef(new ModelRef("string")).parameterType("header")
// .required(false).build();
// pars.add(ticketPar.build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.qkdata")) .apis(RequestHandlerSelectors.basePackage("com.qkdata"))
.paths(PathSelectors.any()).build() .paths(PathSelectors.any()).build()
.securitySchemes(securitySchemes()).securityContexts(securityContexts()); .securitySchemes(securitySchemes()).securityContexts(securityContexts());
// .globalOperationParameters(pars);
} }
private List<ApiKey> securitySchemes() { private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeys = new ArrayList<>(1); List<ApiKey> apiKeys = new ArrayList<>(1);
ApiKey apiKey = new ApiKey("Authorization","Authorization","header"); ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
apiKeys.add(apiKey); apiKeys.add(apiKey);
return apiKeys; return apiKeys;
} }
private List<SecurityContext> securityContexts() { private List<SecurityContext> securityContexts() {
List<SecurityContext> contexts = new ArrayList<>(1); List<SecurityContext> contexts = new ArrayList<>(1);
SecurityContext securityContext = SecurityContext.builder() SecurityContext securityContext = SecurityContext.builder()
.securityReferences(defaultAuth()) .securityReferences(defaultAuth())
//.forPaths(PathSelectors.regex("^(?!auth).*$"))
.build(); .build();
contexts.add(securityContext); contexts.add(securityContext);
return contexts; return contexts;
} }
private List<SecurityReference> defaultAuth() { private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
......
...@@ -19,6 +19,7 @@ import org.apache.shiro.subject.PrincipalCollection; ...@@ -19,6 +19,7 @@ import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.sound.sampled.Line;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -48,10 +49,15 @@ public class OAuthRealm extends AuthorizingRealm { ...@@ -48,10 +49,15 @@ public class OAuthRealm extends AuthorizingRealm {
SysUserPO user = (SysUserPO) principals.getPrimaryPrincipal(); SysUserPO user = (SysUserPO) principals.getPrimaryPrincipal();
Long userId = user.getId(); Long userId = user.getId();
//用户角色
Set<String> roles = shiroService.getUserRoles(userId);
//用户权限列表 //用户权限列表
Set<String> permsSet = new HashSet<>(); Set<String> permsSet = shiroService.getUserPermissions(userId);
permsSet.add("all"); // Set<String> permsSet = new HashSet<>();
// permsSet.add("all");
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.setRoles(roles);
info.setStringPermissions(permsSet); info.setStringPermissions(permsSet);
return info; return info;
} }
......
...@@ -394,6 +394,7 @@ CREATE TABLE `sys_user_role` ( ...@@ -394,6 +394,7 @@ CREATE TABLE `sys_user_role` (
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关联表'; ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关联表';
INSERT INTO `sys_user_role` VALUES (1,1,1);
-- ---------------------------- -- ----------------------------
-- Table structure for teacher -- Table structure for teacher
......
...@@ -10,4 +10,8 @@ ...@@ -10,4 +10,8 @@
</if> </if>
order by id desc order by id desc
</select> </select>
<select id="findUserRoles" resultType="com.qkdata.biz.sys.vo.SysRoleModel">
SELECT r.id,r.code,r.name from sys_role r INNER JOIN sys_user_role t on r.id = t.role_id
WHERE t.user_id=#{userId}
</select>
</mapper> </mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment