Skip to content

Commit

Permalink
refactor($Gateway): update log for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Miller (锺俊) committed Dec 22, 2020
1 parent b94944d commit eae68fc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@Component
@ConfigurationProperties(prefix = "jwt.configuration")
public class JwtConfiguration {
public static final String TOKEN_PREFIX = "Bearer ";

public JwtConfiguration(ProjectProperty projectProperty) {
this.signingKey = String.format("%s %s", projectProperty.getProjectParentArtifactId(), projectProperty.getVersion());
log.info("Initiated JWT signing key: {}. The specified key byte array is {} bits", this.signingKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Mono<Authentication> authenticate(Authentication authentication) {
try {
username = jwtService.getUsernameFromJwt(jwt);
} catch (Exception e) {
log.error("Exception occurred when authenticating", e);
log.error("Authentication failed! Cause: {}", e.getMessage());
return Mono.empty();
}
if (StrUtil.isBlank(username)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
@Slf4j
@Component
@RequiredArgsConstructor
public class ReactiveServerSecurityContextRepository implements ServerSecurityContextRepository {
private static final String TOKEN_PREFIX = "Bearer ";
public class JwtReactiveServerSecurityContextRepository implements ServerSecurityContextRepository {
private final ReactiveAuthenticationManager authenticationManager;

@Override
Expand All @@ -36,12 +35,12 @@ public Mono<Void> save(ServerWebExchange exchange, SecurityContext context) {
public Mono<SecurityContext> load(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
String authorization = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
if (StrUtil.isBlank(authorization) || !authorization.startsWith(TOKEN_PREFIX)) {
if (StrUtil.isBlank(authorization) || !authorization.startsWith(JwtConfiguration.TOKEN_PREFIX)) {
log.warn("Authentication failed! Cause: `{}` in HTTP headers not found. Request URL: [{}] {}",
HttpHeaders.AUTHORIZATION, request.getMethod(), request.getURI());
return Mono.empty();
}
String jwt = authorization.replace(TOKEN_PREFIX, "");
String jwt = authorization.replace(JwtConfiguration.TOKEN_PREFIX, "");
Authentication authentication = new UsernamePasswordAuthenticationToken(null, jwt);
return this.authenticationManager.authenticate(authentication).map(SecurityContextImpl::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
@Configuration
public class ServerAuthenticationEntryPointImpl implements ServerAuthenticationEntryPoint {
@Override
public Mono<Void> commence(ServerWebExchange serverWebExchange, AuthenticationException e) {
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
log.error("Exception occurred when authenticating! Exception message: {}. Request URL: [{}] {}", e.getMessage(),
serverWebExchange.getRequest().getMethod(), serverWebExchange.getRequest().getURI());
return ResponseUtil.renderJson(serverWebExchange, HttpStatus.NETWORK_AUTHENTICATION_REQUIRED, null);
exchange.getRequest().getMethod(), exchange.getRequest().getURI());
return ResponseUtil.renderJson(exchange, HttpStatus.NETWORK_AUTHENTICATION_REQUIRED, null);
}
}

0 comments on commit eae68fc

Please sign in to comment.