Sfoglia il codice sorgente

新增登陆用户时获取的角色id

tags/v2.4.1
xiaoying 1 anno fa
parent
commit
29317b7803
15 ha cambiato i file con 61 aggiunte e 10 eliminazioni
  1. +4
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/common/ServiceExceptionEnum.java
  2. +21
    -3
      tuoheng_oidc_admin/src/main/java/com/tuoheng/config/LoginUserHandler.java
  3. +7
    -4
      tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/DemoController.java
  4. +3
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/ClientUserMapper.java
  5. +2
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/MenusMapper.java
  6. +2
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/PermissionsMapper.java
  7. +2
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/RoleMenuMapper.java
  8. +2
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/RolePermissionMapper.java
  9. +2
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/RolesMapper.java
  10. +2
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/LoginUser.java
  11. +4
    -0
      tuoheng_oidc_admin/src/main/java/com/tuoheng/model/po/UserPo.java
  12. +10
    -3
      tuoheng_oidc_admin/src/main/resources/mapper/ClientUserMapper.xml
  13. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/config/LoginUserHandler.class
  14. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/controller/DemoController.class
  15. BIN
      tuoheng_oidc_admin/target/classes/com/tuoheng/mapper/ClientUserMapper.class

+ 4
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/common/ServiceExceptionEnum.java Vedi File

@@ -16,6 +16,10 @@ public enum ServiceExceptionEnum implements ExceptionInterface {
* 未查询到数据
*/
GET_NO_DATA(10001, "未查到该记录!"),
/**
* 获取不到当前用户信息
*/
NO_DATA_USER(10000,"获取不到当前用户信息!"),
/**
* 参数为空
*/

+ 21
- 3
tuoheng_oidc_admin/src/main/java/com/tuoheng/config/LoginUserHandler.java Vedi File

@@ -3,6 +3,8 @@ package com.tuoheng.config;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.common.ServiceException;
import com.tuoheng.common.ServiceExceptionEnum;
import com.tuoheng.mapper.ClientUserMapper;
import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.po.UserPo;
@@ -16,6 +18,9 @@ import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.annotation.PostConstruct;
import java.util.Optional;

/**
* @author chenjiandong
* @description: TODO
@@ -30,25 +35,38 @@ public class LoginUserHandler implements HandlerMethodArgumentResolver {
parameter.getParameterType().isAssignableFrom(LoginUser.class);
}

@Autowired
private ClientUserMapper clientUserMapper;

private static LoginUserHandler handler;

@PostConstruct
public void init() {
handler = this;
handler.clientUserMapper = this.clientUserMapper;
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
NativeWebRequest request, WebDataBinderFactory factory) {
// header中获取用户token
String token = request.getHeader("th-token");
String oUserJson = request.getHeader("o-user-json");
if(StringUtils.isEmpty(token) || StringUtils.isEmpty(oUserJson)){
if (StringUtils.isEmpty(token) || StringUtils.isEmpty(oUserJson)) {
return new LoginUser();
}
String json = EncryptUtil.decodeUTF8StringBase64(oUserJson);
JSONObject jsonObject = JSON.parseObject(json);
String username = jsonObject.getString("username");
Integer roleId = handler.clientUserMapper.getUserRoleIdByUserName(username);
Long oidcUserId = jsonObject.getLong("oUserId");
// 这里可以自定义封装自己的用户信息
//UserPo userPo = clientUserMapper.getUserByUserName(username);
LoginUser user = new LoginUser()
.setUsername(username)
.setUserId(oidcUserId)
.setThToken(token);
.setThToken(token)
.setRoleId(roleId);

return user;
}
}

+ 7
- 4
tuoheng_oidc_admin/src/main/java/com/tuoheng/controller/DemoController.java Vedi File

@@ -1,7 +1,10 @@
package com.tuoheng.controller;

import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.service.CurrentUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
@@ -11,12 +14,12 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@Slf4j
@RequestMapping("/test")
public class DemoController {

@GetMapping("getHealth")
public String getHealth(){
@GetMapping("/getHealth")
public String getHealth(@CurrentUser LoginUser loginUser) {
log.info("tuoheng-oidc-admin is ok~");
return "tuoheng-oidc-admin is ok~";
return null == loginUser.getRoleId() ? null : loginUser.getRoleId().toString();
}

}

+ 3
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/ClientUserMapper.java Vedi File

@@ -31,4 +31,7 @@ public interface ClientUserMapper {
List<UserPo> selectByTenantId(@Param("tenantId") Long id);

IPage<UserVo> selectByTenantIdAndPage(UserQuery query, IPage<UserPo> page);

Integer getUserRoleIdByUserName(String username);

}

+ 2
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/MenusMapper.java Vedi File

@@ -2,6 +2,7 @@ package com.tuoheng.mapper.op;

import com.tuoheng.model.entity.Menus;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 小影
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2023-03-20 14:50:28
* @Entity com.tuoheng.model.entity.Menus
*/
@Mapper
public interface MenusMapper extends BaseMapper<Menus> {

}

+ 2
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/PermissionsMapper.java Vedi File

@@ -2,6 +2,7 @@ package com.tuoheng.mapper.op;

import com.tuoheng.model.entity.Permissions;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 小影
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2023-03-20 14:55:07
* @Entity com.tuoheng.model.entity.Permissions
*/
@Mapper
public interface PermissionsMapper extends BaseMapper<Permissions> {

}

+ 2
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/RoleMenuMapper.java Vedi File

@@ -2,6 +2,7 @@ package com.tuoheng.mapper.op;

import com.tuoheng.model.entity.RoleMenu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 小影
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2023-03-20 14:55:25
* @Entity com.tuoheng.model.entity.RoleMenu
*/
@Mapper
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {

}

+ 2
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/RolePermissionMapper.java Vedi File

@@ -2,6 +2,7 @@ package com.tuoheng.mapper.op;

import com.tuoheng.model.entity.RolePermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 小影
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2023-03-20 14:55:35
* @Entity com.tuoheng.model.entity.RolePermission
*/
@Mapper
public interface RolePermissionMapper extends BaseMapper<RolePermission> {

}

+ 2
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/mapper/op/RolesMapper.java Vedi File

@@ -2,6 +2,7 @@ package com.tuoheng.mapper.op;

import com.tuoheng.model.entity.Roles;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
* @author 小影
@@ -9,6 +10,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @createDate 2023-03-20 14:55:52
* @Entity com.tuoheng.model.entity.Roles
*/
@Mapper
public interface RolesMapper extends BaseMapper<Roles> {

}

+ 2
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/dto/LoginUser.java Vedi File

@@ -18,4 +18,6 @@ public class LoginUser {

private String thToken;

private Integer roleId;

}

+ 4
- 0
tuoheng_oidc_admin/src/main/java/com/tuoheng/model/po/UserPo.java Vedi File

@@ -23,5 +23,9 @@ public class UserPo extends BasePo {
private Long tenantId;

private Integer isTenant;
/**
* 当前系统中的角色id
*/
private Integer roleId;

}

+ 10
- 3
tuoheng_oidc_admin/src/main/resources/mapper/ClientUserMapper.xml Vedi File

@@ -3,8 +3,8 @@
<mapper namespace="com.tuoheng.mapper.ClientUserMapper">

<insert id="insertClientUser" parameterType="com.tuoheng.model.po.UserPo" keyProperty="id" useGeneratedKeys="true">
insert into users (username, password, create_user, tenant_id, is_tenant)
values (#{username}, #{password}, #{createUser}, #{tenantId}, #{isTenant})
insert into users (username, password, create_user, tenant_id, is_tenant, role_id)
values (#{username}, #{password}, #{createUser}, #{tenantId}, #{isTenant}, #{roleId})
</insert>

<select id="judgeCreateByUserName" parameterType="java.lang.String" resultType="int">
@@ -20,13 +20,20 @@
where username = #{username}
and enabled = 1
</select>
<select id="getUserRoleIdByUserName" parameterType="java.lang.String" resultType="int">
select role_id
from users
where username = #{username}
and enabled = 1
</select>
<select id="selectByUserId" resultType="com.tuoheng.model.po.UserPo">
SELECT id,
username,
`password`,
enabled,
tenant_id,
is_tenant
is_tenant,
role_id
FROM users
WHERE id = #{userId}
</select>

BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/config/LoginUserHandler.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/controller/DemoController.class Vedi File


BIN
tuoheng_oidc_admin/target/classes/com/tuoheng/mapper/ClientUserMapper.class Vedi File


Loading…
Annulla
Salva