Browse Source

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

tags/v2.4.1
xiaoying 1 year ago
parent
commit
29317b7803
15 changed files with 61 additions and 10 deletions
  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 View File

* 未查询到数据 * 未查询到数据
*/ */
GET_NO_DATA(10001, "未查到该记录!"), GET_NO_DATA(10001, "未查到该记录!"),
/**
* 获取不到当前用户信息
*/
NO_DATA_USER(10000,"获取不到当前用户信息!"),
/** /**
* 参数为空 * 参数为空
*/ */

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

import com.alibaba.druid.util.StringUtils; import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.tuoheng.common.ServiceException;
import com.tuoheng.common.ServiceExceptionEnum;
import com.tuoheng.mapper.ClientUserMapper; import com.tuoheng.mapper.ClientUserMapper;
import com.tuoheng.model.dto.LoginUser; import com.tuoheng.model.dto.LoginUser;
import com.tuoheng.model.po.UserPo; import com.tuoheng.model.po.UserPo;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.method.support.ModelAndViewContainer;


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

/** /**
* @author chenjiandong * @author chenjiandong
* @description: TODO * @description: TODO
parameter.getParameterType().isAssignableFrom(LoginUser.class); parameter.getParameterType().isAssignableFrom(LoginUser.class);
} }


@Autowired
private ClientUserMapper clientUserMapper;

private static LoginUserHandler handler;

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

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

return user; return user;
} }
} }

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

package com.tuoheng.controller; package com.tuoheng.controller;


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


/** /**
*/ */
@RestController @RestController
@Slf4j @Slf4j
@RequestMapping("/test")
public class DemoController { public class DemoController {


@GetMapping("getHealth")
public String getHealth(){
@GetMapping("/getHealth")
public String getHealth(@CurrentUser LoginUser loginUser) {
log.info("tuoheng-oidc-admin is ok~"); 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 View File

List<UserPo> selectByTenantId(@Param("tenantId") Long id); List<UserPo> selectByTenantId(@Param("tenantId") Long id);


IPage<UserVo> selectByTenantIdAndPage(UserQuery query, IPage<UserPo> page); 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 View File



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


/** /**
* @author 小影 * @author 小影
* @createDate 2023-03-20 14:50:28 * @createDate 2023-03-20 14:50:28
* @Entity com.tuoheng.model.entity.Menus * @Entity com.tuoheng.model.entity.Menus
*/ */
@Mapper
public interface MenusMapper extends BaseMapper<Menus> { public interface MenusMapper extends BaseMapper<Menus> {


} }

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



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


/** /**
* @author 小影 * @author 小影
* @createDate 2023-03-20 14:55:07 * @createDate 2023-03-20 14:55:07
* @Entity com.tuoheng.model.entity.Permissions * @Entity com.tuoheng.model.entity.Permissions
*/ */
@Mapper
public interface PermissionsMapper extends BaseMapper<Permissions> { public interface PermissionsMapper extends BaseMapper<Permissions> {


} }

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



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


/** /**
* @author 小影 * @author 小影
* @createDate 2023-03-20 14:55:25 * @createDate 2023-03-20 14:55:25
* @Entity com.tuoheng.model.entity.RoleMenu * @Entity com.tuoheng.model.entity.RoleMenu
*/ */
@Mapper
public interface RoleMenuMapper extends BaseMapper<RoleMenu> { public interface RoleMenuMapper extends BaseMapper<RoleMenu> {


} }

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



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


/** /**
* @author 小影 * @author 小影
* @createDate 2023-03-20 14:55:35 * @createDate 2023-03-20 14:55:35
* @Entity com.tuoheng.model.entity.RolePermission * @Entity com.tuoheng.model.entity.RolePermission
*/ */
@Mapper
public interface RolePermissionMapper extends BaseMapper<RolePermission> { public interface RolePermissionMapper extends BaseMapper<RolePermission> {


} }

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



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


/** /**
* @author 小影 * @author 小影
* @createDate 2023-03-20 14:55:52 * @createDate 2023-03-20 14:55:52
* @Entity com.tuoheng.model.entity.Roles * @Entity com.tuoheng.model.entity.Roles
*/ */
@Mapper
public interface RolesMapper extends BaseMapper<Roles> { public interface RolesMapper extends BaseMapper<Roles> {


} }

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



private String thToken; private String thToken;


private Integer roleId;

} }

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

private Long tenantId; private Long tenantId;


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


} }

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

<mapper namespace="com.tuoheng.mapper.ClientUserMapper"> <mapper namespace="com.tuoheng.mapper.ClientUserMapper">


<insert id="insertClientUser" parameterType="com.tuoheng.model.po.UserPo" keyProperty="id" useGeneratedKeys="true"> <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> </insert>


<select id="judgeCreateByUserName" parameterType="java.lang.String" resultType="int"> <select id="judgeCreateByUserName" parameterType="java.lang.String" resultType="int">
where username = #{username} where username = #{username}
and enabled = 1 and enabled = 1
</select> </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="selectByUserId" resultType="com.tuoheng.model.po.UserPo">
SELECT id, SELECT id,
username, username,
`password`, `password`,
enabled, enabled,
tenant_id, tenant_id,
is_tenant
is_tenant,
role_id
FROM users FROM users
WHERE id = #{userId} WHERE id = #{userId}
</select> </select>

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


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


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


Loading…
Cancel
Save