question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring SecurityContext disappears in StreamResource callback method

See original GitHub issue

Tested using the newest bakery app. Spring SecurityContext disappears and becomes Null authentication in StreamResource callback method. Below an example that reproduces the issue.

`@Route(“test”) public class TestView extends VerticalLayout {

public TestView() {
    Anchor anchor = new Anchor();
    anchor.add("DOWNLOAD");
    anchor.setHref(new StreamResource("file", () -> createInputStream()));
    add(anchor);
    // SecurityContext returns correct value UsernamePasswordAuthenticationToken.
    System.err.println(SecurityContextHolder.getContext());
}

private InputStream createInputStream() {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        outputStream.write("text".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    // SecurityContextHolder.getContext() returns Null authentication here.
    System.err.println(SecurityContextHolder.getContext());
    return new ByteArrayInputStream(outputStream.toByteArray());
}

}`

Basically I created a simple view that contains only an Anchor element. When I click on the anchor createInputStream method is executed but Spring SecurityContext changes to null authentication.

Expected behaviour: Spring SecurityContext should be the same during user session and not disappear like in createInputStream method.

Actual behaviour: Spring SecurityContext disappears and changes to Null authentication.

  • Versions:
  • Vaadin 13.0.1
  • Spring boot 2.1.0.RELEASE
  • Java 11
  • Windows 10

As a side note I believe the Upload component’s callback method addSucceededListener suffers from the same problem.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
Julius95commented, Mar 25, 2019

Thank you @heruan for pointing out the problem. So bakery app comes with following WebSecurityConfiguration:

@Override
public void configure(WebSecurity web) throws Exception 

web.ignoring().antMatchers(
// Vaadin Flow static resources
 "/VAADIN/**", //This is the problematic spot
// the standard favicon URI
"/favicon.ico",
// Rest of configuration omitted for simplicity...
}

We can see that all “/VAADIN/" requests are ignored and won’t have SpringSecurityContext. Dynamically created files are mapped to "VAADIN/dynamic/resource” url. When a request to dynamic resource is sent SpringSecurityContext is lost due to “/VAADIN/" configuration since the request url maps to it. To fix this remove "/VAADIN/” configuration or change it to “/VAADIN/static/**”. This issue should be moved to bakery app since it contains this problematic configuration.

0reactions
ZheSun88commented, Mar 25, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Vaadin missing SpringSecurityContext in StreamResource ...
SecurityContext is lost in addSucceededListener callback method. I'm using Vaadin 13.0.1 and Spring Boot 2.1.3. java · spring-boot · spring- ...
Read more >
Spring SecurityContext within VaadinListener
Hello, how can I access the spring security context within a Vaadin listener. ... /vaadin-missing-springsecuritycontext-in-streamresource-callback-method.
Read more >
Spring Security Context Propagation with @Async
We want to check if the Spring SecurityContext is propagated to the new thread. · By default, the security context inside the @Async...
Read more >
All Classes (Jenkins core 2.374 API)
Executor -like class that executes a single task repeatedly, in such a way that a single execution can cover multiple pending queued requests....
Read more >
documentation - CUBA Platform. Developer's Manual
xml in a special way to create the JMX interface in the current JVM. Calls to all JMX bean interface methods are intercepted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found