Skip to content

Commit d9e604c

Browse files
committed
签权服务用户资源数据通过远程调用从sysadmin服务中获取
1 parent 6db0c3c commit d9e604c

File tree

8 files changed

+28
-135
lines changed

8 files changed

+28
-135
lines changed

src/main/java/com/springboot/auth/authentication/dao/ResourceMapper.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/com/springboot/auth/authentication/provider/ResourceProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public interface ResourceProvider {
1414
@GetMapping(value = "/resource/all")
1515
Result<Set<Resource>> resources();
1616

17-
@GetMapping(value = "/resource/{userId}")
18-
Result<Set<Resource>> resources(@PathVariable("userId") long userId);
17+
@GetMapping(value = "/resource/user/{username}")
18+
Result<Set<Resource>> resources(@PathVariable("username") String username);
1919
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.springboot.auth.authentication.provider;
22

3+
import com.springboot.auth.authentication.entity.Resource;
34
import com.springboot.cloud.common.core.entity.vo.Result;
4-
import com.springboot.cloud.common.core.exception.SystemErrorType;
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
58

69
public class ResourceProviderFallback implements ResourceProvider {
710
@Override
8-
public Result resources() {
9-
return Result.fail(SystemErrorType.SYSTEM_BUSY);
11+
public Result<Set<Resource>> resources() {
12+
return Result.success(new HashSet<Resource>());
1013
}
1114

1215
@Override
13-
public Result resources(long userId) {
14-
return Result.fail(SystemErrorType.SYSTEM_BUSY);
16+
public Result<Set<Resource>> resources(String username) {
17+
return Result.success(new HashSet<Resource>());
1518
}
1619
}

src/main/java/com/springboot/auth/authentication/service/IResourceService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@
1212
@Service
1313
public interface IResourceService {
1414

15-
/**
16-
* 根据角色code查询到角色把对应的资源定义
17-
*
18-
* @param roleCodes
19-
* @return
20-
*/
21-
Set<Resource> queryByRoleCodes(String[] roleCodes);
22-
2315
/**
2416
* 动态新增权限
2517
*
@@ -39,4 +31,12 @@ public interface IResourceService {
3931
* @return
4032
*/
4133
ConfigAttribute findConfigAttributesByUrl(HttpServletRequest authRequest);
34+
35+
/**
36+
* 根据用户名查询 该用户所拥有的角色对应的资源信息
37+
*
38+
* @param username
39+
* @return
40+
*/
41+
Set<Resource> queryByUsername(String username);
4242
}

src/main/java/com/springboot/auth/authentication/service/impl/AuthenticationService.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.security.access.ConfigAttribute;
88
import org.springframework.security.core.Authentication;
9-
import org.springframework.security.core.GrantedAuthority;
109
import org.springframework.security.core.context.SecurityContextHolder;
1110
import org.springframework.stereotype.Service;
1211

1312
import javax.servlet.http.HttpServletRequest;
14-
import java.util.Collection;
1513
import java.util.Set;
16-
import java.util.stream.Collectors;
1714

1815
@Service
1916
@Slf4j
@@ -41,7 +38,7 @@ public boolean decide(HttpServletRequest authRequest) {
4138
if (NONEXISTENT_URL.equals(urlConfigAttribute.getAttribute()))
4239
log.debug("url未在资源池中找到,拒绝访问");
4340
//获取此访问用户所有角色拥有的权限资源
44-
Set<Resource> userResources = findResourcesByAuthorityRoles(authentication.getAuthorities());
41+
Set<Resource> userResources = findResourcesByUsername(authentication.getName());
4542
//用户拥有权限资源 与 url要求的资源进行对比
4643
return isMatch(urlConfigAttribute, userResources);
4744
}
@@ -60,17 +57,12 @@ public boolean isMatch(ConfigAttribute urlConfigAttribute, Set<Resource> userRes
6057
/**
6158
* 根据用户所被授予的角色,查询到用户所拥有的资源
6259
*
63-
* @param authorityRoles
60+
* @param username
6461
* @return
6562
*/
66-
private Set<Resource> findResourcesByAuthorityRoles(Collection<? extends GrantedAuthority> authorityRoles) {
67-
//用户被授予的角色
68-
log.debug("用户的授权角色集合信息为:{}", authorityRoles);
69-
String[] authorityRoleCodes = authorityRoles.stream()
70-
.map(GrantedAuthority::getAuthority)
71-
.collect(Collectors.toList())
72-
.toArray(new String[authorityRoles.size()]);
73-
Set<Resource> resources = resourceService.queryByRoleCodes(authorityRoleCodes);
63+
private Set<Resource> findResourcesByUsername(String username) {
64+
//用户被授予的角色资源
65+
Set<Resource> resources = resourceService.queryByUsername(username);
7466
if (log.isDebugEnabled()) {
7567
log.debug("用户被授予角色的资源数量是:{}, 资源集合信息为:{}", resources.size(), resources);
7668
}

src/main/java/com/springboot/auth/authentication/service/impl/ResourceService.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.springboot.auth.authentication.service.impl;
22

3-
import com.springboot.auth.authentication.dao.ResourceMapper;
43
import com.springboot.auth.authentication.entity.Resource;
54
import com.springboot.auth.authentication.provider.ResourceProvider;
65
import com.springboot.auth.authentication.service.IResourceService;
@@ -26,9 +25,6 @@ public class ResourceService implements IResourceService {
2625
@Autowired
2726
private HandlerMappingIntrospector mvcHandlerMappingIntrospector;
2827

29-
@Autowired
30-
private ResourceMapper resourceMapper;
31-
3228
@Autowired
3329
private ResourceProvider resourceProvider;
3430

@@ -37,11 +33,6 @@ public class ResourceService implements IResourceService {
3733
*/
3834
private Map<RequestMatcher, ConfigAttribute> resourceConfigAttributes;
3935

40-
@Override
41-
public Set<Resource> queryByRoleCodes(String[] roleCodes) {
42-
return resourceMapper.queryByRoleCodes(roleCodes);
43-
}
44-
4536
@Override
4637
public void addResource(Resource resource) {
4738
resourceConfigAttributes.put(
@@ -72,6 +63,11 @@ public ConfigAttribute findConfigAttributesByUrl(HttpServletRequest authRequest)
7263
.orElse(new SecurityConfig("NONEXISTENT_URL"));
7364
}
7465

66+
@Override
67+
public Set<Resource> queryByUsername(String username) {
68+
return resourceProvider.resources(username).getData();
69+
}
70+
7571
/**
7672
* 创建RequestMatcher
7773
*

src/test/java/com/springboot/auth/authentication/dao/ResourceMapperTest.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/test/java/com/springboot/auth/authentication/service/impl/ResourceServiceTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.springboot.auth.authentication.service.impl;
22

3-
import com.springboot.auth.authentication.entity.Resource;
43
import com.springboot.auth.authentication.rest.HttpServletRequestAuthWrapper;
54
import org.junit.Assert;
65
import org.junit.Test;
@@ -11,10 +10,6 @@
1110
import org.springframework.security.access.ConfigAttribute;
1211
import org.springframework.test.context.junit4.SpringRunner;
1312

14-
import java.util.Set;
15-
16-
import static org.hamcrest.Matchers.greaterThan;
17-
1813
@RunWith(SpringRunner.class)
1914
@SpringBootTest
2015
public class ResourceServiceTest {
@@ -42,18 +37,6 @@ public class ResourceServiceTest {
4237
@Autowired
4338
private ResourceService resourceService;
4439

45-
@Test
46-
public void testQueryByRoleCodes_假如存在角色ADMIN_当传入ADMIN时_那么可以获取到角色拥有的资源集合() {
47-
Set<Resource> resources = resourceService.queryByRoleCodes(new String[]{"ADMIN"});
48-
Assert.assertThat(resources.size(), greaterThan(2));
49-
}
50-
51-
@Test
52-
public void testQueryByRoleCodes_假如不存在角色NOTHING_当传入NOTHING时_那么获取不到资源信息() {
53-
Set<Resource> resources = resourceService.queryByRoleCodes(new String[]{"NOTHING"});
54-
Assert.assertEquals(0, resources.size());
55-
}
56-
5740
@Test
5841
public void testGetConfigAttributesByUrl_假如存在如上资源信息_当请求不存在method的资源时_那么返回NONEXISTENT_URL() {
5942
ConfigAttribute attributesByUrl = resourceService

0 commit comments

Comments
 (0)