1 year ago

#73714

test-img

Andrew Miterev

Vaadin: What is the Spring context of the Drop component?

I am using the 21 version of Vaadin. I have the following code, taken from an example from the Vaadin website and slightly (purely beauty) modified by me. The remaining files are unchanged.

the codes are given below. Why do I get such a log?

2022-01-20 10:08:32.589 DEBUG 12556 --- [nio-8888-exec-6] c.e.photoarchive.components.DropView : Place 1. User here 2022-01-20 10:08:38.197 TRACE 12556 --- [nio-8888-exec-1] c.example.photoarchive.SecurityService : authentication is null 2022-01-20 10:08:38.197 DEBUG 12556 --- [nio-8888-exec-1] c.e.photoarchive.components.DropView : Place 2. User here <>


@Log4j2
@Route(value = "drop", layout = MainAppLayout.class)
public class DropView extends VerticalLayout implements HasComponents {
    private final SecurityService securityService;

    public DropView(SecurityService securityService) {
        this.securityService = securityService;

        MultiFileMemoryBuffer buffer = new MultiFileMemoryBuffer();
        Upload upload = new Upload(buffer);
        upload.setUploadButton(new Button(getTranslation("Upload_files")));
        upload.setDropLabel(new Label(getTranslation("Drop_files")));
        upload.setMaxFileSize(-1);

        log.debug("Place 1. User here <{}>", getUsername());

        upload.addFinishedListener(e -> {
            log.debug("Place 2. User here <{}>", getUsername());
            // my code ... too much :-)
        });
        add(upload);
    }

    private String getUsername() {
        var user = securityService.getAuthenticatedUser();
        if (user != null) return user.getUsername();
        return "";
    }
}


@Log4j2
@Service
public class SecurityService {
    private static final String LOGOUT_SUCCESS_URL = "/";

    public UserDetails getAuthenticatedUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (Objects.isNull(context)) {log.trace("context is null");return null;}

        Authentication authentication = context.getAuthentication();
        if (Objects.isNull(authentication)) {log.trace("authentication is null");return null;}

        Object principal = authentication.getPrincipal();
        if (Objects.isNull(principal)) {log.trace("principal is null");return null;}

        if (principal instanceof UserDetails) {
            return (UserDetails) principal;
        }
        // Anonymous or no authentication.
        return null;
    }
}

spring-security

vaadin

drop

security-context

0 Answers

Your Answer

Accepted video resources