From 98a7f7d442b204bbffc11b7ad34e13a2ee32b6fa Mon Sep 17 00:00:00 2001 From: zhouhao Date: Wed, 10 Oct 2018 15:08:03 +0800 Subject: [PATCH] fix bug #91 --- .../embed/EmbedAuthenticationManager.java | 3 +- .../simple/SimpleAuthenticationManager.java | 15 +++- .../authorization/starter/FixBug91Test.groovy | 69 +++++++++++++++++++ .../src/test/resources/application.yml | 5 ++ 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/groovy/org/hswebframework/web/authorization/starter/FixBug91Test.groovy diff --git a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/embed/EmbedAuthenticationManager.java b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/embed/EmbedAuthenticationManager.java index f691011ad..b12e8fa1f 100644 --- a/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/embed/EmbedAuthenticationManager.java +++ b/hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/embed/EmbedAuthenticationManager.java @@ -55,6 +55,7 @@ public void init() { } } } + authentications.put(id, properties.toAuthentication(dataAccessConfigBuilderFactory)); }); } @@ -66,7 +67,7 @@ public Authentication authenticate(AuthenticationRequest request) { ((PlainTextUsernamePasswordAuthenticationRequest) request).getUsername().equals(user.getUsername()) && ((PlainTextUsernamePasswordAuthenticationRequest) request).getPassword().equals(user.getPassword())) .findFirst() - .map(properties -> properties.toAuthentication(dataAccessConfigBuilderFactory)) + .map(properties -> authentications.get(properties.getId())) .orElseThrow(() -> new ValidationException("用户不存在"))); } diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthenticationManager.java b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthenticationManager.java index c966cfb72..98ec4be3c 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthenticationManager.java +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-local/src/main/java/org/hswebframework/web/service/authorization/simple/SimpleAuthenticationManager.java @@ -27,7 +27,6 @@ public class SimpleAuthenticationManager implements AuthenticationManager { private AuthenticationInitializeService authenticationInitializeService; - @Setter @Getter private AuthenticationManager parent; @@ -85,7 +84,16 @@ public Authentication authenticate(AuthenticationRequest request) { @Override // @Cacheable(value = USER_AUTH_CACHE_NAME, key = "#userId") public Authentication getByUserId(String userId) { - Supplier supplier = () -> authenticationInitializeService.initUserAuthorization(userId); + Supplier supplier = () -> { + Authentication authentication = null; + if (parent != null) { + authentication = parent.getByUserId(userId); + } + if (authentication == null) { + authentication = authenticationInitializeService.initUserAuthorization(userId); + } + return authentication; + }; if (null != cacheManager) { Cache cache = cacheManager.getCache(USER_AUTH_CACHE_NAME); @@ -104,6 +112,9 @@ public Authentication getByUserId(String userId) { @Override @CachePut(value = USER_AUTH_CACHE_NAME, key = "#authentication.user.id") public Authentication sync(Authentication authentication) { + if (parent != null) { + parent.sync(authentication); + } return authentication; } } diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/groovy/org/hswebframework/web/authorization/starter/FixBug91Test.groovy b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/groovy/org/hswebframework/web/authorization/starter/FixBug91Test.groovy new file mode 100644 index 000000000..c9a7d6ba2 --- /dev/null +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/groovy/org/hswebframework/web/authorization/starter/FixBug91Test.groovy @@ -0,0 +1,69 @@ +package org.hswebframework.web.authorization.starter + +import org.hswebframework.web.authorization.AuthenticationManager +import org.hswebframework.web.authorization.simple.PlainTextUsernamePasswordAuthenticationRequest +import org.hswebframework.web.entity.authorization.UserEntity +import org.hswebframework.web.service.authorization.UserService +import org.hswebframework.web.validate.ValidationException +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.context.ConfigurableApplicationContext +import org.springframework.context.annotation.Configuration +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.web.WebAppConfiguration +import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.setup.MockMvcBuilders +import spock.lang.Shared +import spock.lang.Specification + +@WebAppConfiguration +@ContextConfiguration +@SpringBootTest(classes = [TestApplication.class], properties = ["classpath:application.yml"]) +@Configuration +class FixBug91Test extends Specification { + + @Autowired + private ConfigurableApplicationContext context; + + @Shared + private MockMvc mockMvc; + + @Autowired + private AuthenticationManager authenticationManager; + + @Autowired + private UserService userService; + + void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); + UserEntity userEntity = userService.createEntity(); + userEntity.setName("test"); + userEntity.setUsername("fix-bug#91"); + userEntity.setPassword("fix-bug#91"); + if (userService.selectByUsername("fix-bug#91") == null) { + userService.insert(userEntity); + } + } + + boolean authenticationInitSuccess(String username, String password) { + try { + def autz = authenticationManager.authenticate(new PlainTextUsernamePasswordAuthenticationRequest(username, password)); + if (autz != null) { + return null != authenticationManager.getByUserId(autz.getUser().getId()); + } + } catch (ValidationException e) { + return false; + } + return false; + } + + def "同时获取配置文件和数据库中的用户权限"() { + expect: + authenticationInitSuccess(username, password) == success + where: + username | password | success + "fix-bug#91" | "fix-bug#91" | true + "fix-bug-91-in-yml" | "fix-bug-91-in-yml" | true + "not-exists-user" | "not-exists-user" | false + } +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/resources/application.yml b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/resources/application.yml index a2d76338f..42cd5052a 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/resources/application.yml +++ b/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-starter/src/test/resources/application.yml @@ -14,6 +14,11 @@ hsweb: authorize: sync: false auto-parse: false + users: + fix-bug-91-in-yml: + username: "fix-bug-91-in-yml" + password: "fix-bug-91-in-yml" + logging: level: org.springframework: WARN