@@ -16,6 +16,10 @@ public enum ServiceExceptionEnum implements ExceptionInterface { | |||
* 未查询到数据 | |||
*/ | |||
GET_NO_DATA(10001, "未查到该记录!"), | |||
/** | |||
* 获取不到当前用户信息 | |||
*/ | |||
NO_DATA_USER(10000,"获取不到当前用户信息!"), | |||
/** | |||
* 参数为空 | |||
*/ |
@@ -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; | |||
} | |||
} |
@@ -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(); | |||
} | |||
} |
@@ -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,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,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,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,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,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> { | |||
} |
@@ -18,4 +18,6 @@ public class LoginUser { | |||
private String thToken; | |||
private Integer roleId; | |||
} |
@@ -23,5 +23,9 @@ public class UserPo extends BasePo { | |||
private Long tenantId; | |||
private Integer isTenant; | |||
/** | |||
* 当前系统中的角色id | |||
*/ | |||
private Integer roleId; | |||
} |
@@ -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> |