Skip to content

Commit

Permalink
fix bug hs-web#91
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Oct 10, 2018
1 parent 13471f9 commit 98a7f7d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void init() {
}
}
}
authentications.put(id, properties.toAuthentication(dataAccessConfigBuilderFactory));
});
}

Expand All @@ -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("用户不存在")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class SimpleAuthenticationManager implements AuthenticationManager {

private AuthenticationInitializeService authenticationInitializeService;


@Setter
@Getter
private AuthenticationManager parent;
Expand Down Expand Up @@ -85,7 +84,16 @@ public Authentication authenticate(AuthenticationRequest request) {
@Override
// @Cacheable(value = USER_AUTH_CACHE_NAME, key = "#userId")
public Authentication getByUserId(String userId) {
Supplier<Authentication> supplier = () -> authenticationInitializeService.initUserAuthorization(userId);
Supplier<Authentication> 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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98a7f7d

Please sign in to comment.