3

In my managed bean, I wish to fetch the j_security_check username, which auths to a LDAP server, as a bean property.

Essentially, I want to pull the username from

<input type="text" name="j_username" />

once it is submited and all authed and use it in the following:

@Override
public String getName() {
    return getId();
}

How would I use

FacesContext.getExternalContext().getUserPrincipal();

to get the username as bean property?

This is the complete backing bean if you need to know what is doing. I used to manually have the user to enter a username in a text box, I want to now stop this and have it automatically pull the username.

//@Named("user")
@SessionScoped
public class UserBean implements Serializable, Principal {

    private static final long serialVersionUID = 1L;
    @NotNull(message = "The username can not be blank")
    @Size(min = 6, max = 12, message = "Please enter a valid username (6-12 characters)")
//@Pattern(regexp = "[a-zA-Z0-9_]", message = "Please enter a valid username consiting of only characters that are from the alphabet or are numeric ")
//@Pattern(regexp = "(a-z)(A-Z)(0-9))", message = "Please enter a valid username consiting of only characters that are from the alphabet or are numeric ")
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String newValue) {
        id = newValue;
    }
    private String fileText;

    @NotNull(message = "You must select a file to upload")
    public String getFileText() {
        return fileText;
    }

    public void setFileText(String fileText) {
        this.fileText = fileText;
    }

    /**
     * public void getName(HttpServletRequest req, HttpServletResponse res)
     * throws ServletException, java.io.IOException { id = req.getRemoteUser();
     * }
     */
    @Override
    public String getName() {
        return getId();
    }
    /*
     * @Override
     *
     * public String request.remoteUser() { return getId();
     *
     * }
     * .
     */
} 
user1924104
  • 891
  • 2
  • 16
  • 38
  • you `UserBean` class shouldn't implement the `Principal` interface, that's a system interface intended for libraries that want to offer a security identities. You are confusing concepts, when using *container managed authentication* is the container (Glassfish) the one in charge to process the form (that weird `j_security_check` is a container-specific action meant to request authentication for the user). – Alonso Dominguez Jan 22 '13 at 15:38
  • Ok thank you, i think i understand, is it possible for me in the userBean to getName() as the username from the login ? as the userBean needs a user in order to do other stuff, and it would be better if it could auto get this from the login page rather than getting the user to type it again – user1924104 Jan 22 '13 at 15:41
  • look at BalusC answer, it's a simple way of getting the username in whichever backing bean you want. Besides, check this other answer: http://stackoverflow.com/questions/11638155/java-session-filters-with-users-and-admin-login/11640472#11640472 – Alonso Dominguez Jan 22 '13 at 16:34

1 Answers1

5

Initialize it in @PostConstruct.

private String username;

@PostConstruct
public void init() {
    username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
}

Or, if you only need it during processing the form submit, just obtain it in action method instead.

Note that getRemoteUser() basically returns the same as getUserPrincipal().getName().


Unrelated to the concrete problem: this kind of bean shouldn't be session scoped, but instead view or conversation scoped. Also, it should not implement the Principal interface. That makes no utter sense.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555