Skip to content
\n

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

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

If you start a separate thread at that time and you want to be able to use method based security you'd have to pass the Authentication into that thread and use the same trick to populate the SecurityContext with that Authentication there. From that point on method based security should work.

","upvoteCount":1,"url":"https://github.com/graphql-java-kickstart/graphql-java-servlet/discussions/134#discussioncomment-225973"}}}

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.