【问题标题】:Remember me functionality using cookies [duplicate]使用 cookie 记住我的功能 [重复]
【发布时间】:2014-07-24 22:37:29
【问题描述】:

我正在尝试使用 JSF 2.0 实现记住我的功能,但不知道如何实现 COOKIES 来做到这一点。你能分享一个工作示例代码吗?

【问题讨论】:

    标签: jsf-2 remember-me


    【解决方案1】:

    编辑:不要在 cookie 中存储密码和用户名!

    See this post by BalusC for a better implementation.


    我相信这可能会对您有所帮助:

    login.xhtml

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich">
    
    <h:body>
       <h:form id="LoginForm">
    
    <h:panelGrid id="lpg" columns="4" >
    <h:outputText value="User Id"/> 
    <h:inputText id="username" value="#{loginBean.userId}"/>
    <h:outputText value=""/>
    
    <h:outputText value="Password"/> 
    <h:inputSecret id="password" value="#{loginBean.password}"/>
    <h:outputText value=""/>
    
    <h:outputText value=""/>
    <h:outputText value=""/>
    <h:outputText value=""/>
    <h:outputText value=""/>
    <h:selectBooleanCheckbox value="#{loginBean.checkBox}" />
    <h:outputText value="Remember me" />
    <h:outputText value=""/>
    <h:outputText value=""/>
    <h:commandButton value="Login" action="#{loginBean.doLogin}"/>
    
    </h:form>
    </h:body>
    
    </html>
    

    LoginBean.java

    public class LoginBean {
    
    
    private String userId;
    private String password;
    private boolean checkBox = false;
    private String virtualCheck;
    
        // Setter and getter
    
        public LoginBean() {
        isChecked();
    }
    
    public void isChecked() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Cookie[] cookiesArr = ((HttpServletRequest)(fc.getExternalContext().getRequest())).getCookies();
        if(cookiesArr != null && cookiesArr.length > 0)
            for(int i =0; i < cookiesArr.length; i++) {
                String cName = cookiesArr[i].getName();
                String cValue= cookiesArr[i].getValue();
                System.out.println("***cValue***"+cValue);
                if(cName.equals("cUserId")) {
                    setUserId(cValue);
                } else if(cName.equals("cPassword")) {
                    setPassword(cValue);
                } else if(cName.equals("cVirtualCheck")) {
                    setVirtualCheck(cValue);
                    if(getVirtualCheck().equals("false")) {
                        setCheckBox(false);
                        setUserId(null);
                        setPassword(null);
                    } else if(getVirtualCheck().equals("true")) {
                        System.out.println("Here in doLogin() line 99");
                        setCheckBox(true);
                    }
                }
            }
    
    }
    
        public String doLogin() {
            if(userId != null && password!= null){
            FacesContext fc = FacesContext.getCurrentInstance();
            if(checkBox == true) {
                virtualCheck = "true";
                Cookie cUserId = new Cookie("cUserId", userId);
                Cookie cPassword = new Cookie("cPassword", password);
                Cookie cVirtualCheck = new Cookie("cVirtualCheck", virtualCheck);
                cUserId.setMaxAge(3600);
                cPassword.setMaxAge(3600);
                cVirtualCheck.setMaxAge(3600);
                ((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cUserId);
                ((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cPassword);
                ((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cVirtualCheck);
            } else {
                virtualCheck = "false";
                Cookie cVirtualCheck = new Cookie("cVirtualCheck", virtualCheck);
                ((HttpServletResponse)(fc.getExternalContext().getResponse())).addCookie(cVirtualCheck);
            }
                    return "success";
        }
    

    注意为了保存密码,浏览器会提示是否保存密码,无论 Java Web 技术和浏览器设置如何,检索 cookie 都将发挥主要作用。

    【讨论】:

    • 我试过了,效果很好。这就是我要找的。谢谢!!!
    • 所以您建议 OP 将密码和用户名作为 cookie 传递?任何有两个拇指和浏览器的人都可以查看的内容?
    • @kolossus :我将这段代码用于我自己的网站并且它有效。我不确定什么是更好的方法。请让我知道是否有更好的方法来实现它。我会很高兴学习它。
    • @kolossus : 你有更好的建议吗,如果有,请分享。
    • @user3680182 - 查看最佳答案here。虽然我认为用户名根本不应该设置在 cookie 中,但它比我们这里的更接近安全。这个故事的寓意是永远、永远、永远在 cookie 中设置密码。不要这样做。这个答案甚至不建议你散列或加密任何东西。相当大的安全风险
    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 2011-04-01
    • 2013-05-16
    • 2012-12-04
    • 1970-01-01
    • 2013-03-18
    • 2011-02-26
    • 1970-01-01
    相关资源
    最近更新 更多