Skip to content

Commit

Permalink
perf($Gateway): refine access log
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Miller (锺俊) committed Dec 22, 2020
1 parent 7ba75c3 commit 281a958
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ public Mono<Authentication> authenticate(Authentication authentication) {
try {
username = jwtService.getUsernameFromJwt(jwt);
} catch (Exception e) {
log.error("Authentication failed! Cause: {}", e.getMessage());
log.error("Authentication failure! Cause: {}", e.getMessage());
return Mono.empty();
}
if (StrUtil.isBlank(username)) {
log.warn("Authentication failed! Cause: the username mustn't be blank");
log.warn("Authentication failure! Cause: the username mustn't be blank");
return Mono.empty();
}
val response = authCenterRemoteApi.getUserByLoginToken(username);
Mono<GetUserByLoginTokenResponse> responseMono = response.map(ResponseBodyBean::getData)
.switchIfEmpty(Mono.error(new BusinessException("Authentication failed! Cause: User not found")));
.switchIfEmpty(Mono.error(new BusinessException("Authentication failure! Cause: User not found")));
return responseMono.map(getUserByLoginTokenResponse -> {
log.info("Authentication success! Found username: {}", getUserByLoginTokenResponse.getUsername());
UserPrincipal userPrincipal = UserPrincipal.create(getUserByLoginTokenResponse, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Mono<SecurityContext> load(ServerWebExchange exchange) {
}
String authorization = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
if (StrUtil.isBlank(authorization) || !authorization.startsWith(JwtConfiguration.TOKEN_PREFIX)) {
log.warn("Authentication failed! Cause: `{}` in HTTP headers not found. Request URL: [{}] {}",
log.warn("Authentication failure! Cause: `{}` in HTTP headers not found. Request URL: [{}] {}",
HttpHeaders.AUTHORIZATION, request.getMethod(), request.getURI());
return Mono.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, Au
.switchIfEmpty(Mono.error(new BusinessException("User not found!")));
});
val zip = Mono.zip(getPermissionListByUserIdResponseMono, userPrincipalMono);
return zip.map(objects -> {
val permissionList = objects.getT1().getPermissionList();
return zip.map(mapper -> {
val permissionList = mapper.getT1().getPermissionList();
val buttonPermissionList = permissionList.stream()
.filter(permission -> PermissionType.BUTTON.getType().equals(permission.getType()))
.filter(permission -> StrUtil.isNotBlank(permission.getUrl()))
.filter(permission -> StrUtil.isNotBlank(permission.getMethod()))
.collect(Collectors.toList());
val path = request.getURI().getPath();
val userPrincipal = objects.getT2();
val userPrincipal = mapper.getT2();
for (val buttonPermission : buttonPermissionList) {
if (antPathMatcher.match(buttonPermission.getUrl(), path)) {
log.info("Resource [{}] {} is accessible for user(username: {})", request.getMethod(),
request.getURI(), userPrincipal.getUsername());
log.info("Authorization success! Resource [{}] {} is accessible for user(username: {})",
request.getMethod(), request.getURI(), userPrincipal.getUsername());
return new AuthorizationDecision(true);
}
}
log.warn("Resource [{}] {} is inaccessible for user(username: {})", request.getMethod(),
request.getURI(), userPrincipal.getUsername());
log.warn("Authorization failure! Resource [{}] {} is inaccessible for user(username: {})",
request.getMethod(), request.getURI(), userPrincipal.getUsername());
return new AuthorizationDecision(false);
});
}
Expand Down

0 comments on commit 281a958

Please sign in to comment.