Frage

I'm quite new to java web page environment. Recently I try to develop an E-Business platform by using Java.

Hence, I'm using j_security_check Form-Based as my authentication tool. It was successfully to re-direct to desire page after authentication completed.

However, due to I have to load end user setting and information from Database (MS SQL 2005), so I have to load all the information after authentication completed and before page re-direct.

I had tried some method by using primefaces but it was unable to trigger the bean method that load user information.

One of the methods that I tried is using oncomplete to trigger the bean method

<h:form id="login" prependId="false"
                    onsubmit="document.getElementById('login').action='j_security_check';">
                    <h:panelGrid columns="2" cellpadding="5">  
                        <h:outputLabel for="j_username" value="Username:" />
                        <p:inputText value="#{loginBean.username}"   
                                     id="j_username" required="true" label="j_username" />
                        <h:outputLabel for="j_password" value="Password:" />  
                        <h:inputSecret value="#{loginBean.password}"   
                                       id="j_password" required="true" label="j_password" />
                        <f:facet name="footer">  
                            <p:commandButton id="loginButton" value="Login" type="submit" ajax="false"                                                 
                                              oncomplete="#{loginBean.login()}"/>
                        </f:facet>  
                    </h:panelGrid>
                </h:form>

I also been tried something similar to this which just change oncomplete to onclick and others. End up doesn't has any method is workable.

Hence, I hope that anyone of you can give me advise how should I overcome this problem.

Million thanks,

War es hilfreich?

Lösung

There are several solutions for this.

  1. Create a servlet filter which checks the remote user and the presence of User entity in the session. If the remote user is not null, but the User entity is, then load it from the DB and set it in session. This is concretely answered here: Accessing user details after logging in with Java EE Form authentication

  2. Perform lazy loading in the getter of a session scoped bean which should return the User entity. This is concretely answered in 1st part of this answer: Performing user authentication in Java EE / JSF using j_security_check

  3. Perform programmatic login using a real JSF bean action method and obtain the User entity directly. This is concretely answered in 2nd part of this answer: Performing user authentication in Java EE / JSF using j_security_check


Unrelated to the concrete problem, you should be using <form action="j_security_check" method="post"> instead of a <h:form prependId="false">. Then you also don't need that nasty JS hack to change the form's action.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top