From d78e1decb3830b88fc64859b9baa10563cb9de82 Mon Sep 17 00:00:00 2001 From: Zheng Jie <201507802@qq.com> Date: Tue, 24 May 2022 16:34:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20AuthorityDto=20=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=20GrantedAuthority=20=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/service/dto/AuthorityDto.java | 34 +++++++++++++++++++ .../security/service/dto/JwtUserDto.java | 4 +-- .../modules/system/service/RoleService.java | 4 +-- .../system/service/impl/RoleServiceImpl.java | 14 ++++---- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/AuthorityDto.java diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/AuthorityDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/AuthorityDto.java new file mode 100644 index 00000000..888cceb6 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/AuthorityDto.java @@ -0,0 +1,34 @@ +/* + * Copyright 2019-2020 Zheng Jie + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package me.zhengjie.modules.security.service.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.security.core.GrantedAuthority; + +/** + * 避免序列化问题 + * @author Zheng Jie + * @date 2018-11-30 + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class AuthorityDto implements GrantedAuthority { + + private String authority; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/JwtUserDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/JwtUserDto.java index 00f4372c..52e84961 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/JwtUserDto.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/JwtUserDto.java @@ -38,10 +38,10 @@ public class JwtUserDto implements UserDetails { private final List dataScopes; @JSONField(serialize = false) - private final List authorities; + private final List authorities; public Set getRoles() { - return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()); + return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet()); } @Override diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java index 65e4f587..feb614b9 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java @@ -15,13 +15,13 @@ */ package me.zhengjie.modules.system.service; +import me.zhengjie.modules.security.service.dto.AuthorityDto; import me.zhengjie.modules.system.domain.Role; import me.zhengjie.modules.system.service.dto.RoleDto; import me.zhengjie.modules.system.service.dto.RoleQueryCriteria; import me.zhengjie.modules.system.service.dto.RoleSmallDto; import me.zhengjie.modules.system.service.dto.UserDto; import org.springframework.data.domain.Pageable; -import org.springframework.security.core.GrantedAuthority; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @@ -119,7 +119,7 @@ public interface RoleService { * @param user 用户信息 * @return 权限信息 */ - List mapToGrantedAuthorities(UserDto user); + List mapToGrantedAuthorities(UserDto user); /** * 验证是否被用户关联 diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java index e8b41438..7f49f5ea 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java @@ -19,6 +19,7 @@ import cn.hutool.core.collection.CollectionUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.security.service.UserCacheClean; +import me.zhengjie.modules.security.service.dto.AuthorityDto; import me.zhengjie.modules.system.domain.Menu; import me.zhengjie.modules.system.domain.Role; import me.zhengjie.exception.EntityExistException; @@ -38,11 +39,8 @@ import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; - import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; @@ -166,19 +164,19 @@ public class RoleServiceImpl implements RoleService { @Override @Cacheable(key = "'auth:' + #p0.id") - public List mapToGrantedAuthorities(UserDto user) { + public List mapToGrantedAuthorities(UserDto user) { Set permissions = new HashSet<>(); // 如果是管理员直接返回 if (user.getIsAdmin()) { permissions.add("admin"); - return permissions.stream().map(SimpleGrantedAuthority::new) + return permissions.stream().map(AuthorityDto::new) .collect(Collectors.toList()); } Set roles = roleRepository.findByUserId(user.getId()); permissions = roles.stream().flatMap(role -> role.getMenus().stream()) - .filter(menu -> StringUtils.isNotBlank(menu.getPermission())) - .map(Menu::getPermission).collect(Collectors.toSet()); - return permissions.stream().map(SimpleGrantedAuthority::new) + .map(Menu::getPermission) + .filter(StringUtils::isNotBlank).collect(Collectors.toSet()); + return permissions.stream().map(AuthorityDto::new) .collect(Collectors.toList()); }