-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added security to the STOMP messaging layer [#648]
* Attach the web auth token to outbound websocket connections. * Generate a principal as a part of this process. * Created a new module for messaging in Angular.
- Loading branch information
Showing
31 changed files
with
688 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...-api/src/main/java/org/comixedproject/messaging/ComiXedWebSocketAuthenticationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.comixedproject.messaging; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.messaging.simp.config.ChannelRegistration; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
@Order(Ordered.HIGHEST_PRECEDENCE + 99) | ||
@Log4j2 | ||
public class ComiXedWebSocketAuthenticationConfig implements WebSocketMessageBrokerConfigurer { | ||
@Autowired private ComiXedWebSocketChannelInterceptor webSocketChannelTokenInterceptor; | ||
|
||
@Override | ||
public void configureClientInboundChannel(final ChannelRegistration registration) { | ||
log.trace("Configuring websocket inbound channel"); | ||
registration.interceptors(webSocketChannelTokenInterceptor); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...st-api/src/main/java/org/comixedproject/messaging/ComiXedWebSocketChannelInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* ComiXed - A digital comic book library management application. | ||
* Copyright (C) 2021, The ComiXed Project | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses> | ||
*/ | ||
|
||
package org.comixedproject.messaging; | ||
|
||
import java.util.Collections; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.comixedproject.authentication.ComiXedUserDetailsService; | ||
import org.comixedproject.authentication.JwtTokenUtil; | ||
import org.comixedproject.messaging.model.StompPrincipal; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageChannel; | ||
import org.springframework.messaging.simp.stomp.StompCommand; | ||
import org.springframework.messaging.simp.stomp.StompHeaderAccessor; | ||
import org.springframework.messaging.support.ChannelInterceptor; | ||
import org.springframework.messaging.support.MessageHeaderAccessor; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* <code>ComiXedWebSocketChannelInterceptor</code> provides a security interceptor to create the | ||
* principal for the messaging layer. | ||
* | ||
* @author Darryl L. Pierce | ||
*/ | ||
@Component | ||
@Log4j2 | ||
public class ComiXedWebSocketChannelInterceptor implements ChannelInterceptor { | ||
private static final String HEADER_STRING = "Authorization"; | ||
static final String TOKEN_PREFIX = "Bearer "; | ||
|
||
@Autowired private ComiXedUserDetailsService userDetailsService; | ||
@Autowired private JwtTokenUtil jwtTokenUtil; | ||
|
||
@Override | ||
public Message<?> preSend(final Message<?> message, final MessageChannel channel) { | ||
StompHeaderAccessor accessor = | ||
MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class); | ||
if (accessor != null) { | ||
log.debug("Received STOMP command: {}", accessor.getCommand()); | ||
if (StompCommand.CONNECT.equals(accessor.getCommand())) { | ||
final Optional<String> optionalToken = | ||
Optional.ofNullable(accessor.getNativeHeader(HEADER_STRING)) | ||
.orElse(Collections.emptyList()).stream() | ||
.flatMap(Stream::ofNullable) | ||
.findFirst(); | ||
optionalToken.ifPresent( | ||
token -> { | ||
log.trace("Auth token found"); | ||
createPrincipal(accessor, token); | ||
}); | ||
} | ||
} | ||
return message; | ||
} | ||
|
||
void createPrincipal(final StompHeaderAccessor accessor, final String token) { | ||
String username = null; | ||
if (!StringUtils.isEmpty(token)) { | ||
try { | ||
username = this.jwtTokenUtil.getEmailFromToken(token); | ||
} catch (Exception error) { | ||
log.error("Unable to extract username from auth token", error); | ||
} | ||
} | ||
if (!StringUtils.isEmpty(username)) { | ||
UserDetails userDetails = this.userDetailsService.loadUserByUsername(username); | ||
|
||
if (Boolean.TRUE.equals(this.jwtTokenUtil.validateToken(token, userDetails))) { | ||
log.debug("authenticated user " + username + ", setting security context"); | ||
accessor.setUser(new StompPrincipal(username)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.