Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
d78e1dec
Commit
d78e1dec
authored
May 24, 2022
by
Zheng Jie
Browse files
新增 AuthorityDto 类,避免 GrantedAuthority 序列化报错问题
parent
9015cae9
Changes
4
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/AuthorityDto.java
0 → 100644
View file @
d78e1dec
/*
* 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
;
}
eladmin-system/src/main/java/me/zhengjie/modules/security/service/dto/JwtUserDto.java
View file @
d78e1dec
...
@@ -38,10 +38,10 @@ public class JwtUserDto implements UserDetails {
...
@@ -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
<
Granted
Authority
>
authorities
;
private
final
List
<
Authority
Dto
>
authorities
;
public
Set
<
String
>
getRoles
()
{
public
Set
<
String
>
getRoles
()
{
return
authorities
.
stream
().
map
(
Granted
Authority:
:
getAuthority
).
collect
(
Collectors
.
toSet
());
return
authorities
.
stream
().
map
(
Authority
Dto
:
:
getAuthority
).
collect
(
Collectors
.
toSet
());
}
}
@Override
@Override
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java
View file @
d78e1dec
...
@@ -15,13 +15,13 @@
...
@@ -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 {
...
@@ -119,7 +119,7 @@ public interface RoleService {
* @param user 用户信息
* @param user 用户信息
* @return 权限信息
* @return 权限信息
*/
*/
List
<
Granted
Authority
>
mapToGrantedAuthorities
(
UserDto
user
);
List
<
Authority
Dto
>
mapToGrantedAuthorities
(
UserDto
user
);
/**
/**
* 验证是否被用户关联
* 验证是否被用户关联
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java
View file @
d78e1dec
...
@@ -19,6 +19,7 @@ import cn.hutool.core.collection.CollectionUtil;
...
@@ -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;
...
@@ -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 {
...
@@ -166,19 +164,19 @@ public class RoleServiceImpl implements RoleService {
@Override
@Override
@Cacheable
(
key
=
"'auth:' + #p0.id"
)
@Cacheable
(
key
=
"'auth:' + #p0.id"
)
public
List
<
Granted
Authority
>
mapToGrantedAuthorities
(
UserDto
user
)
{
public
List
<
Authority
Dto
>
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
(
SimpleGranted
Authority:
:
new
)
return
permissions
.
stream
().
map
(
Authority
Dto
:
:
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
(
m
enu
.
getPermission
())
)
.
map
(
M
enu
:
:
getPermission
)
.
map
(
Menu:
:
getPermission
).
collect
(
Collectors
.
toSet
());
.
filter
(
StringUtils:
:
isNotBlank
).
collect
(
Collectors
.
toSet
());
return
permissions
.
stream
().
map
(
SimpleGranted
Authority:
:
new
)
return
permissions
.
stream
().
map
(
Authority
Dto
:
:
new
)
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment