* 未查询到数据 | * 未查询到数据 | ||||
*/ | */ | ||||
GET_NO_DATA(10001, "未查到该记录!"), | GET_NO_DATA(10001, "未查到该记录!"), | ||||
/** | |||||
* 获取不到当前用户信息 | |||||
*/ | |||||
NO_DATA_USER(10000,"获取不到当前用户信息!"), | |||||
/** | /** | ||||
* 参数为空 | * 参数为空 | ||||
*/ | */ |
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; | ||||
} | } | ||||
} | } |
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(); | |||||
} | } | ||||
} | } |
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); | |||||
} | } |
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> { | ||||
} | } |
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> { | ||||
} | } |
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> { | ||||
} | } |
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> { | ||||
} | } |
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> { | ||||
} | } |
private String thToken; | private String thToken; | ||||
private Integer roleId; | |||||
} | } |
private Long tenantId; | private Long tenantId; | ||||
private Integer isTenant; | private Integer isTenant; | ||||
/** | |||||
* 当前系统中的角色id | |||||
*/ | |||||
private Integer roleId; | |||||
} | } |
<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> |