Skip to content

How to authenticate user using subscription #134

Answered by oliemansm
guitcastro asked this question in Q&A
Discussion options

You must be logged in to vote

@guitcastro
I was able to get a hold of the principal in my publisher by setting the authentication in the SecurityContext in the ConnectionListener

  public Optional<Object> onConnect(Object payload) {
    log.debug("onConnect with payload {}", payload.getClass());
    String token = ((Map<String, String>) payload).get("authToken");
    log.info("Token: {}", token);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(token, null));
    return Optional.of(token);
  }

Then in my publisher I could get a hold of it using

Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

If you start a separate thread at that…

Replies: 14 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by oliemansm
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@metsfan
Comment options

@philip-jvm
Comment options

@metsfan
Comment options

@oliemansm
Comment options

@philip-jvm
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
8 participants
Converted from issue

This discussion was converted from issue #134 on December 19, 2020 20:48.