How can I access shiro authentication information

Platform:
Java 1.8
karaf 4.0.7
pax web 4.3.0
shiro 1.3.1
vaadin 7.7.4

I have a web application where I’m trying out various Java web technologies and now the turn has come to vaadin.
The “using-vaadin” branch of my web application currently contains a vaadin hello world application that has been transformed from a shiro-basic-authenticated primefaces web application:
https://github.com/steinarb/ukelonn/tree/using-vaadin

My next step is to retrieve the shiro authentication information, but I have been unable to google up how to do this with vaadin 7. All examples and prior forum questions relate to vaadin 6.

How do I find the authenticated username? Is it somewhere in the session object? Do I need to create some kind of handler to listen to the http request? Where do I put it in the handler so that it is available in the UI.init() method?

Do my questions make sense when applied to vaadin…? :slight_smile:

Thanks!

  • Steinar

I figured out how to access the current user from the init() method, use the static getSubject() method of the Shiro SecurityUtils class:

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class UkelonnUI extends UI {
    private static final long serialVersionUID = 1388525490129647161L;

    @Override
    protected void init(VaadinRequest request) {
        Subject currentUser = SecurityUtils.getSubject();
        // Create the content root layout for the UI
        VerticalLayout content = new VerticalLayout();
        setContent(content);

        // Display the greeting
        content.addComponent(new Label("Hello " + currentUser.getPrincipal()))

It turned out to be a lot simpler than the above example: The VaadinRequest argument to the init() method contains the necessary information:

public class UkelonnUI extends UI {
    @Override
    protected void init(VaadinRequest request) {
        Principal currentUser = request.getUserPrincipal();
        // Create the content root layout for the UI
        VerticalLayout content = new VerticalLayout();
        setContent(content);

        // Display the greeting
        Component greeting = new Label("Hello " + currentUser.getName());
        greeting.setStyleName("h1");
        content.addComponent(greeting);