今天早上我参考了JEE6教程的第39,40和41章。但我非常非常困惑。我没有JEE6的网络应用程序安全背景,而且我在理解和实施方面遇到很大困难。我的网络应用程序(JSF 2.0)的简单登录和注销功能
我需要为我的web应用程序创建一个授权机制,我的情况对于像我这样的JEE6中的begginer来说也不是很简单,所以我决定尝试找到最简单的方法来做到这一点。
我想解释我的想法,所以你可以纠正我,并给我一些建议,说明它是如何做到最简单的方法。
理念:
我的web应用程序使用称为船坞primefaces组件,在弹出对话框日志时使用的点击中的最后一项。此导航工具也位于应用程序中所有其他页面使用的JSF模板中。
<h:body>
<p:dock position="top">
<p:menuitem value="Naslovna" icon="unsecuredimages/naslovna.png" url="main.xhtml"
alt="The image could not be found." />
<p:menuitem value="Register" icon="unsecuredimages/register.png"
url="registration.xhtml" alt="The image could not be found." />
<p:menuitem value="Cesta pitanja" icon="unsecuredimages/faq.png"
url="faq.xhtml" alt="The image could not be found." />
<!-- The login will not have a page, it will pop up a login dialog -->
<p:menuitem value="Login" icon="unsecuredimages/login.png" url="#" onclick="dlg.show()"/>
</p:dock>
<p:dialog header="Prijavite se" widgetVar="dlg" modal="true" draggable="false" resizable="false" effect="SLIDE">
<h:outputText value="[email protected]:" /><h:inputText id="email" value=""/>
<br/>
<h:outputText value="Lozinka:" /><h:inputText id="password" value=""/>
<br/>
<h:commandButton value="Prijavi se" />
</p:dialog>
<br/><br/><br/><br/><br/><br/>
<ui:insert name="mainForm" />
<ui:insert name="registrationForm" />
<ui:insert name="registrationBuyerForm" />
<ui:insert name="registrationSellerForm" />
<ui:insert name="faqForm" />
<ui:insert name="registrationSuccessForm" />
</h:body>
我认为JSF应该有一个支持bean来处理电子邮件和密码到EJB。
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import ejbinterfaces.IAuthentificationEJB;
@ManagedBean
@SessionScoped
public class SecurityController {
@EJB
private IAuthentificationEJB authentificationEJB;
private String email;
private String password;
public void logIn() {
authentificationEJB.saveUserState(email, password);
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
}
那么EJB应该做的登录和注销(这是我得到非常困惑):
@Stateful(name = "ejbs/AuthentificationEJB")
public class AuthentificationEJB implements IAuthentificationEJB {
//Login
public boolean saveUserState(String email,String password) {
//1-Send query to database to see if that user exist
//2-If the query returns the user object, store it somewhere in the session(HOW?)
//3-return true if the user state was saved
//4-return false otherwise
return false;
}
//Logout
public void releaseUserState() {
//1-Check if there is something saved in the session(or wherever the state is saved)
//2-If 1 then flush it
}
//Check if user is logged in
public boolean checkAuthentificationStatus() {
//1-Check if there is something saved in the session(This means the user is logged in)
//2-If there is not a user already loged, then return false
return false;
}
}
我决定不使用jdbc领域或JEE6教程中解释的其他认证机制,因为我感到非常困惑,所以我认为现在对我来说手动更容易。这是我对我的方法有些怀疑:
- 这种方法是否正确(可以这样做)?
- 在这种情况下,EJB应该是@Stateless还是@Statefull(用户从数据库中检索到了2个字符串字段)?
我应该在哪里存储从数据库中检索到的用户的ID,持续到用户决定注销?
如果我必须在会话中存储用户状态,直到他/她决定注销,我该怎么办?
- 用这种方法将在未注销关闭浏览器用户的会话delated(如果没有,我怎么能一会儿后自动失效他/她的会话如果没有活动?)
我很感激你的帮助。
这里没有解决你的问题,但我建议你尝试使用容器方法来安全并让他们管理它(毕竟,框架在那里为你做这件事)。如果您在设置jdbc领域时遇到问题,可以设置一个领域来读取一个非常容易设置的属性文件。 – SJuan76 2011-04-12 09:59:03