Spring SecurityContext within VaadinListener

Hello,

how can I access the spring security context within a Vaadin listener. In more detail, I’m using an upload component and the uploaded content should be send to a REST-service. I use a SucceededListener to get informed about a file upload. Within the method of that listener, I want to get the SecurityContext in order to get authentication information. With the listener method I have no access to the SecurityContext of the spring application. How can I get access to that context?

MemoryBuffer buffer = new MemoryBuffer();
Upload upload = new Upload(buffer);
upload.addSucceededListener(e -> {
	SecurityContext context = SecurityContextHolder.getContext();
	context.getAuthentication(); // null
});

Thanks, Michael

Hello Michael.

Did you find a solution to this problem? Because I am in the same situation.

Thanks

Try this:

SecurityContext context = SecurityContextHolder.getContext();
Authentication auth= context.getAuthentication();

MemoryBuffer buffer = new MemoryBuffer();
Upload upload = new Upload(buffer);
upload.addSucceededListener(e -> {
	
	 service.upload(e.getfile,auth);
});

I faced a similar issue. The issue in my case was with Spring Security configuration. See here for information
https://stackoverflow.com/questions/55276834/vaadin-missing-springsecuritycontext-in-streamresource-callback-method.

Julius Koljonen:
I faced a similar issue. The issue in my case was with Spring Security configuration. See here for information
https://stackoverflow.com/questions/55276834/vaadin-missing-springsecuritycontext-in-streamresource-callback-method.

Great, thank you for your solution, it’s work for my.

The solution provided in the stackoverflow link worked for us also.