- 后端:重构 oauth2 模块,方便后续 User 接入。
- 后端:重写 Admin 安全拦截器,实现类似 Shiro 的效果。
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.mall.admin.dao;
|
||||
|
||||
import cn.iocoder.mall.admin.dataobject.DataDictDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@@ -8,9 +10,7 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface DataDictMapper {
|
||||
|
||||
DataDictDO selectById(@Param("id") Integer id);
|
||||
public interface DataDictMapper extends BaseMapper<DataDictDO> {
|
||||
|
||||
DataDictDO selectByEnumValueAndValue(
|
||||
@Param("enumValue") String enumValue,
|
||||
@@ -26,10 +26,9 @@ public interface DataDictMapper {
|
||||
@Param("enumValue") String enumValue
|
||||
);
|
||||
|
||||
List<DataDictDO> selectList();
|
||||
default List<DataDictDO> selectList() {
|
||||
return selectList(new QueryWrapper<>());
|
||||
}
|
||||
|
||||
void insert(DataDictDO dataDict);
|
||||
|
||||
int update(DataDictDO dataDict);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package cn.iocoder.mall.admin.dao;
|
||||
|
||||
import cn.iocoder.mall.admin.dataobject.OAuth2AccessTokenDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OAuth2AccessTokenMapper extends BaseMapper<OAuth2AccessTokenDO> {
|
||||
|
||||
int updateToInvalidByAdminId(@Param("adminId") Integer adminId);
|
||||
default int updateToInvalidByAdminId(Integer adminId) {
|
||||
QueryWrapper<OAuth2AccessTokenDO> query = new QueryWrapper<OAuth2AccessTokenDO>()
|
||||
.eq("admin_id", adminId).eq("valid", true);
|
||||
return update(new OAuth2AccessTokenDO().setValid(false), query);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
package cn.iocoder.mall.admin.dao;
|
||||
|
||||
import cn.iocoder.mall.admin.dataobject.OAuth2RefreshTokenDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OAuth2RefreshTokenMapper extends BaseMapper<OAuth2RefreshTokenDO> {
|
||||
|
||||
int updateToInvalidByAdminId(@Param("adminId") Integer adminId);
|
||||
default int updateToInvalidByAdminId(Integer adminId) {
|
||||
QueryWrapper<OAuth2RefreshTokenDO> query = new QueryWrapper<OAuth2RefreshTokenDO>()
|
||||
.eq("admin_id", adminId).eq("valid", true);
|
||||
return update(new OAuth2RefreshTokenDO().setValid(false), query);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.Set;
|
||||
@Repository
|
||||
public interface ResourceMapper extends BaseMapper<ResourceDO> {
|
||||
|
||||
@Deprecated
|
||||
// TODO 芋艿,后续改造。
|
||||
List<ResourceDO> selectListByTypeAndRoleIds(@Param("type") Integer type,
|
||||
@Param("roleIds") Set<Integer> roleIds);
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.DeletableDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@@ -13,6 +14,7 @@ import lombok.experimental.Accessors;
|
||||
* value:1 男
|
||||
* value:2 女
|
||||
*/
|
||||
@TableName("data_dict")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DataDictDO extends DeletableDO {
|
||||
|
||||
@@ -31,16 +31,16 @@ public class DataDictServiceImpl implements DataDictService {
|
||||
private DataDictMapper dataDictMapper;
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DataDictBO>> selectDataDictList() {
|
||||
public List<DataDictBO> selectDataDictList() {
|
||||
List<DataDictDO> dataDicts = dataDictMapper.selectList();
|
||||
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDicts));
|
||||
return DataDictConvert.INSTANCE.convert(dataDicts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<DataDictBO> addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO) {
|
||||
public DataDictBO addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO) {
|
||||
// 校验数据字典重复
|
||||
if (dataDictMapper.selectByEnumValueAndValue(dataDictAddDTO.getEnumValue(), dataDictAddDTO.getValue()) != null) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
|
||||
}
|
||||
// 保存到数据库
|
||||
DataDictDO dataDict = DataDictConvert.INSTANCE.convert(dataDictAddDTO);
|
||||
@@ -49,45 +49,43 @@ public class DataDictServiceImpl implements DataDictService {
|
||||
dataDictMapper.insert(dataDict);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDict));
|
||||
return DataDictConvert.INSTANCE.convert(dataDict);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO) {
|
||||
public Boolean updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO) {
|
||||
// 校验数据字典不存在
|
||||
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictUpdateDTO.getId());
|
||||
if (existsDataDict == null) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 校验数据字典重复
|
||||
DataDictDO duplicateDataDict = dataDictMapper.selectByEnumValueAndValue(existsDataDict.getEnumValue(), dataDictUpdateDTO.getValue());
|
||||
if (duplicateDataDict != null && !duplicateDataDict.getId().equals(dataDictUpdateDTO.getId())) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
|
||||
}
|
||||
// 更新到数据库
|
||||
DataDictDO updateDataDict = DataDictConvert.INSTANCE.convert(dataDictUpdateDTO);
|
||||
dataDictMapper.update(updateDataDict);
|
||||
dataDictMapper.updateById(updateDataDict);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 一般情况下,不要删除数据字典。
|
||||
// 因为,业务数据正在使用该数据字典,删除后,可能有不可预知的问题。
|
||||
@Override
|
||||
public CommonResult<Boolean> deleteDataDict(Integer adminId, Integer dataDictId) {
|
||||
public Boolean deleteDataDict(Integer adminId, Integer dataDictId) {
|
||||
// 校验数据字典不存在
|
||||
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictId);
|
||||
if (existsDataDict == null) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 更新到数据库
|
||||
DataDictDO updateDataDict = new DataDictDO().setId(dataDictId);
|
||||
updateDataDict.setDeleted(DeletedStatusEnum.DELETED_YES.getValue());
|
||||
dataDictMapper.update(updateDataDict);
|
||||
// 标记删除
|
||||
dataDictMapper.deleteById(dataDictId);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -106,7 +104,7 @@ public class DataDictServiceImpl implements DataDictService {
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DataDictBO>> getDataDictList(String dictKey, Collection<?> dictValueList) {
|
||||
Set<String> convertDictValueList = dictValueList.stream().map(o -> String.valueOf(o)).collect(Collectors.toSet());
|
||||
Set<String> convertDictValueList = dictValueList.stream().map(String::valueOf).collect(Collectors.toSet());
|
||||
List<DataDictDO> dataDictDOList = dataDictMapper.selectByEnumValueAndValues(dictKey, convertDictValueList);
|
||||
List<DataDictBO> dataDictBOList = DataDictConvert.INSTANCE.convert(dataDictDOList);
|
||||
return CommonResult.success(dataDictBOList);
|
||||
|
||||
@@ -29,21 +29,6 @@
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
|
||||
<select id="selectById" resultType="DataDictDO">
|
||||
SELECT
|
||||
<include refid="FIELDS"/>
|
||||
FROM data_dict
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectList" resultType="DataDictDO">
|
||||
SELECT
|
||||
<include refid="FIELDS"/>
|
||||
FROM data_dict
|
||||
WHERE deleted = 0
|
||||
</select>
|
||||
<select id="selectByEnumValue" resultType="cn.iocoder.mall.admin.dataobject.DataDictDO">
|
||||
SELECT
|
||||
<include refid="FIELDS"/>
|
||||
@@ -52,39 +37,4 @@
|
||||
AND enum_value = #{enumValue}
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="DataDictDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO data_dict (
|
||||
id, enum_value, value, display_name, sort,
|
||||
memo, create_time, deleted
|
||||
) VALUES (
|
||||
#{id}, #{enumValue}, #{value}, #{displayName}, #{sort},
|
||||
#{memo}, #{createTime}, #{deleted}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="DataDictDO">
|
||||
UPDATE data_dict
|
||||
<set>
|
||||
<if test="enumValue != null">
|
||||
enum_value = #{enumValue},
|
||||
</if>
|
||||
<if test="value != null">
|
||||
value = #{value},
|
||||
</if>
|
||||
<if test="displayName != null">
|
||||
display_name = #{displayName},
|
||||
</if>
|
||||
<if test="sort != null">
|
||||
sort = #{sort},
|
||||
</if>
|
||||
<if test="memo != null">
|
||||
memo = #{memo},
|
||||
</if>
|
||||
<if test="deleted != null">
|
||||
deleted = #{deleted}
|
||||
</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
</mapper>
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.admin.dao.OAuth2AccessTokenMapper">
|
||||
|
||||
<update id="updateToInvalidByAdminId" parameterType="Integer">
|
||||
UPDATE oauth2_access_token
|
||||
SET valid = 0
|
||||
WHERE admin_id = #{adminId}
|
||||
AND valid = 1
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@@ -1,12 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.admin.dao.OAuth2RefreshTokenMapper">
|
||||
|
||||
<update id="updateToInvalidByAdminId" parameterType="Integer">
|
||||
UPDATE oauth2_refresh_token
|
||||
SET valid = 0
|
||||
WHERE admin_id = #{adminId}
|
||||
AND valid = 1
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user