新增 AuthorityDto 类,避免 GrantedAuthority 序列化报错问题

pull/789/head
Zheng Jie 2022-05-24 16:34:47 +08:00
parent 9015cae9db
commit d78e1decb3
4 changed files with 44 additions and 12 deletions

View File

@ -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;
}

View File

@ -38,10 +38,10 @@ public class JwtUserDto implements UserDetails {
private final List<Long> dataScopes; private final List<Long> dataScopes;
@JSONField(serialize = false) @JSONField(serialize = false)
private final List<GrantedAuthority> authorities; private final List<AuthorityDto> authorities;
public Set<String> getRoles() { public Set<String> getRoles() {
return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()); return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
} }
@Override @Override

View File

@ -15,13 +15,13 @@
*/ */
package me.zhengjie.modules.system.service; 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.domain.Role;
import me.zhengjie.modules.system.service.dto.RoleDto; import me.zhengjie.modules.system.service.dto.RoleDto;
import me.zhengjie.modules.system.service.dto.RoleQueryCriteria; import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
import me.zhengjie.modules.system.service.dto.RoleSmallDto; import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.modules.system.service.dto.UserDto; import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.security.core.GrantedAuthority;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -119,7 +119,7 @@ public interface RoleService {
* @param user * @param user
* @return * @return
*/ */
List<GrantedAuthority> mapToGrantedAuthorities(UserDto user); List<AuthorityDto> mapToGrantedAuthorities(UserDto user);
/** /**
* *

View File

@ -19,6 +19,7 @@ import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.security.service.UserCacheClean; 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.Menu;
import me.zhengjie.modules.system.domain.Role; import me.zhengjie.modules.system.domain.Role;
import me.zhengjie.exception.EntityExistException; 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.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@ -166,19 +164,19 @@ public class RoleServiceImpl implements RoleService {
@Override @Override
@Cacheable(key = "'auth:' + #p0.id") @Cacheable(key = "'auth:' + #p0.id")
public List<GrantedAuthority> mapToGrantedAuthorities(UserDto user) { public List<AuthorityDto> mapToGrantedAuthorities(UserDto user) {
Set<String> permissions = new HashSet<>(); Set<String> permissions = new HashSet<>();
// 如果是管理员直接返回 // 如果是管理员直接返回
if (user.getIsAdmin()) { if (user.getIsAdmin()) {
permissions.add("admin"); permissions.add("admin");
return permissions.stream().map(SimpleGrantedAuthority::new) return permissions.stream().map(AuthorityDto::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
Set<Role> roles = roleRepository.findByUserId(user.getId()); Set<Role> roles = roleRepository.findByUserId(user.getId());
permissions = roles.stream().flatMap(role -> role.getMenus().stream()) permissions = roles.stream().flatMap(role -> role.getMenus().stream())
.filter(menu -> StringUtils.isNotBlank(menu.getPermission())) .map(Menu::getPermission)
.map(Menu::getPermission).collect(Collectors.toSet()); .filter(StringUtils::isNotBlank).collect(Collectors.toSet());
return permissions.stream().map(SimpleGrantedAuthority::new) return permissions.stream().map(AuthorityDto::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }