feat: uaa改造为Spring Authorization Server
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
package com.central.file.config;
|
||||
|
||||
import com.central.common.config.DefaultWebMvcConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig extends DefaultWebMvcConfig {
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.central.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig extends DefaultWebMvcConfig {
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.central.user.config;
|
||||
|
||||
import com.central.common.config.DefaultWebMvcConfig;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author zlt
|
||||
* @date 2019/8/5
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig extends DefaultWebMvcConfig {
|
||||
}
|
||||
+3
-8
@@ -3,10 +3,8 @@ package com.central.user.controller;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.context.LoginUserContextHolder;
|
||||
import com.central.common.model.*;
|
||||
@@ -165,12 +163,9 @@ public class SysMenuController {
|
||||
*/
|
||||
@GetMapping("/current")
|
||||
@Operation(summary = "查询当前用户菜单")
|
||||
public List<SysMenu> findMyMenu(@LoginUser SysUser user) {
|
||||
List<SysRole> roles = user.getRoles();
|
||||
if (CollectionUtil.isEmpty(roles)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<SysMenu> menus = menuService.findByRoleCodes(roles.stream().map(SysRole::getCode).collect(Collectors.toSet()), CommonConstant.MENU);
|
||||
public List<SysMenu> findMyMenu() {
|
||||
LoginAppUser user = LoginUserContextHolder.getUser();
|
||||
List<SysMenu> menus = menuService.findByUserId(user.getId(), CommonConstant.MENU);
|
||||
return treeBuilder(menus);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -8,10 +8,10 @@ import java.util.Set;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.central.common.annotation.LoginUser;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.model.*;
|
||||
import com.central.common.utils.ExcelUtil;
|
||||
import com.central.common.utils.LoginUserUtils;
|
||||
import com.central.log.annotation.AuditLog;
|
||||
import com.central.search.client.service.IQueryService;
|
||||
import com.central.search.model.LogicDelDto;
|
||||
@@ -65,8 +65,8 @@ public class SysUserController {
|
||||
*/
|
||||
@Operation(summary = "根据access_token当前登录用户")
|
||||
@GetMapping("/users/current")
|
||||
public Result<LoginAppUser> getLoginAppUser(@LoginUser(isFull = true) SysUser user) {
|
||||
return Result.succeed(appUserService.getLoginAppUser(user));
|
||||
public Result<LoginAppUser> getLoginAppUser() {
|
||||
return Result.succeed(LoginUserUtils.getCurrentUser(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,7 +84,7 @@ public class SysUserController {
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/login", params = "username")
|
||||
@Operation(summary = "根据用户名查询用户")
|
||||
public LoginAppUser findByUsername(String username) {
|
||||
public SysUser findByUsername(String username) {
|
||||
return appUserService.findByUsername(username);
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public class SysUserController {
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/mobile", params = "mobile")
|
||||
@Operation(summary = "根据手机号查询用户")
|
||||
public LoginAppUser findByMobile(String mobile) {
|
||||
public SysUser findByMobile(String mobile) {
|
||||
return appUserService.findByMobile(mobile);
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ public class SysUserController {
|
||||
*/
|
||||
@GetMapping(value = "/users-anon/openId", params = "openId")
|
||||
@Operation(summary = "根据OpenId查询用户")
|
||||
public LoginAppUser findByOpenId(String openId) {
|
||||
public SysUser findByOpenId(String openId) {
|
||||
return appUserService.findByOpenId(openId);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ package com.central.user.mapper;
|
||||
import com.central.db.mapper.SuperMapper;
|
||||
import com.central.common.model.SysMenu;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 菜单
|
||||
@@ -11,5 +14,5 @@ import org.apache.ibatis.annotations.Mapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface SysMenuMapper extends SuperMapper<SysMenu> {
|
||||
|
||||
List<SysMenu> findByUserId(@Param("userId") Long userId, @Param("type") Integer type);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ public interface ISysMenuService extends ISuperService<SysMenu> {
|
||||
*/
|
||||
List<SysMenu> findByRoles(Set<Long> roleIds);
|
||||
|
||||
List<SysMenu> findByUserId(Long userId, Integer type);
|
||||
|
||||
/**
|
||||
* 角色菜单列表
|
||||
* @param roleIds 角色ids
|
||||
|
||||
+5
-6
@@ -8,7 +8,6 @@ import com.central.common.service.ISuperService;
|
||||
import com.central.user.model.SysUserExcel;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import com.central.common.model.SysRole;
|
||||
import com.central.common.model.SysUser;
|
||||
|
||||
@@ -24,18 +23,18 @@ public interface ISysUserService extends ISuperService<SysUser> {
|
||||
* @param username
|
||||
* @return
|
||||
*/
|
||||
LoginAppUser findByUsername(String username);
|
||||
SysUser findByUsername(String username);
|
||||
|
||||
LoginAppUser findByOpenId(String username);
|
||||
SysUser findByOpenId(String username);
|
||||
|
||||
LoginAppUser findByMobile(String username);
|
||||
SysUser findByMobile(String username);
|
||||
|
||||
/**
|
||||
* 通过SysUser 转换为 LoginAppUser,把roles和permissions也查询出来
|
||||
* 通过SysUser 把roles和permissions也查询出来
|
||||
* @param sysUser
|
||||
* @return
|
||||
*/
|
||||
LoginAppUser getLoginAppUser(SysUser sysUser);
|
||||
void setUserPermission(SysUser sysUser);
|
||||
|
||||
/**
|
||||
* 根据用户名查询用户
|
||||
|
||||
+4
@@ -66,6 +66,10 @@ public class SysMenuServiceImpl extends SuperServiceImpl<SysMenuMapper, SysMenu>
|
||||
public List<SysMenu> findByRoleCodes(Set<String> roleCodes, Integer type) {
|
||||
return roleMenuService.findMenusByRoleCodes(roleCodes, type);
|
||||
}
|
||||
@Override
|
||||
public List<SysMenu> findByUserId(Long userId, Integer type) {
|
||||
return baseMapper.findByUserId(userId, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有菜单
|
||||
|
||||
+13
-20
@@ -21,8 +21,6 @@ import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -55,48 +53,43 @@ public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser>
|
||||
private DistributedLock lock;
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByUsername(String username) {
|
||||
public SysUser findByUsername(String username) {
|
||||
SysUser sysUser = this.selectByUsername(username);
|
||||
return getLoginAppUser(sysUser);
|
||||
setUserPermission(sysUser);
|
||||
return sysUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByOpenId(String username) {
|
||||
public SysUser findByOpenId(String username) {
|
||||
SysUser sysUser = this.selectByOpenId(username);
|
||||
return getLoginAppUser(sysUser);
|
||||
setUserPermission(sysUser);
|
||||
return sysUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser findByMobile(String username) {
|
||||
public SysUser findByMobile(String username) {
|
||||
SysUser sysUser = this.selectByMobile(username);
|
||||
return getLoginAppUser(sysUser);
|
||||
setUserPermission(sysUser);
|
||||
return sysUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginAppUser getLoginAppUser(SysUser sysUser) {
|
||||
public void setUserPermission(SysUser sysUser) {
|
||||
if (sysUser != null) {
|
||||
List<SysRole> sysRoles = roleUserService.findRolesByUserId(sysUser.getId());
|
||||
Collection<GrantedAuthority> authorities = new HashSet<>();
|
||||
if (sysRoles != null) {
|
||||
sysRoles.forEach(role -> authorities.add(new SimpleGrantedAuthority(role.getCode())));
|
||||
}
|
||||
sysUser.setRoles(sysRoles);
|
||||
|
||||
Set<String> permissions = null;
|
||||
Set<String> permissions;
|
||||
if (!CollectionUtils.isEmpty(sysRoles)) {
|
||||
Set<Long> roleIds = sysRoles.stream().map(SuperEntity::getId).collect(Collectors.toSet());
|
||||
List<SysMenu> menus = roleMenuMapper.findMenusByRoleIds(roleIds, CommonConstant.PERMISSION);
|
||||
if (!CollectionUtils.isEmpty(menus)) {
|
||||
permissions = menus.stream().map(p -> p.getPath())
|
||||
.collect(Collectors.toSet());
|
||||
sysUser.setPermissions(permissions);
|
||||
}
|
||||
}
|
||||
return new LoginAppUser(sysUser.getId()
|
||||
, sysUser.getUsername(), sysUser.getPassword()
|
||||
, sysUser.getMobile(), permissions
|
||||
, sysUser.getEnabled(), true, true, true
|
||||
, authorities);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,5 +2,15 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.user.mapper.SysMenuMapper">
|
||||
|
||||
<select id="findByUserId" resultType="com.central.common.model.SysMenu">
|
||||
select distinct t.* from sys_menu t
|
||||
inner join sys_role_menu r on r.menu_id = t.id
|
||||
inner join sys_role_user ru on ru.role_id = r.role_id
|
||||
where ru.user_id = #{userId}
|
||||
<if test="type != null">
|
||||
and t.type = #{type}
|
||||
</if>
|
||||
and t.hidden = 0
|
||||
ORDER BY sort ASC
|
||||
</select>
|
||||
</mapper>
|
||||
+2
-3
@@ -1,6 +1,5 @@
|
||||
package com.central.user.service;
|
||||
|
||||
import com.central.common.model.LoginAppUser;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import com.central.common.model.PageResult;
|
||||
@@ -27,8 +26,8 @@ public class SysUserServiceTest {
|
||||
|
||||
@Test
|
||||
public void testFindByUsername() {
|
||||
LoginAppUser loginAppUser = sysUserService.findByUsername("admin");
|
||||
assertThat(loginAppUser).isNotNull();
|
||||
SysUser sysUser = sysUserService.findByUsername("admin");
|
||||
assertThat(sysUser).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user