CookieHelper.setCookie不起作用

时间:2016-03-15 13:22:50

标签: jsf cookies setcookie

我尝试在登录后设置cookie。我在这个网站上找到了一个例子。但有时它是有效的,有些时候不行。如果我重新启动然后我运行,它不起作用。这是我的代码。

public class CookieHelper {
public static void setCookie(String name, String value, int expiry) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;
    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                break;
            }
        }
    }
    if (cookie != null) {
        cookie.setValue(value);
    } else {
        cookie = new Cookie(name, value);
        cookie.setPath(request.getContextPath());
    }
    cookie.setMaxAge(expiry);
    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    response.addCookie(cookie);
}

public static Cookie getCookie(String name) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
    Cookie cookie = null;
    Cookie[] userCookies = request.getCookies();
    if (userCookies != null && userCookies.length > 0 ) {
        for (int i = 0; i < userCookies.length; i++) {
            if (userCookies[i].getName().equals(name)) {
                cookie = userCookies[i];
                return cookie;
            }
        }
    }
    return null;
}

}

这是我的bean登录

@ManagedBean(name="login", eager=true)
@RequestScoped
  public class Login {
private String message ="Enter username and password.";
private String username;
private String password;
private String host;
private LazyDataModel<Message> lazyModel;

public String findHost(){
    return username.substring(username.indexOf("@")+1, username.indexOf(".com"));
}

public String login(){
    if(username.equals("admin@admin") && password.equals("admin")){
        return "admin";
    } else{
        try {
            Properties props = new Properties();
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");
            Session session = Session.getInstance(props, null);
            Transport transport = session.getTransport("smtp");
            String SVhost = findHost();
            transport.connect(CheckTypeMail.checkSmtp(SVhost), 587, username, password);
            setHost(CheckTypeMail.checkSmtp(SVhost));        
            transport.close();

            CookieHelper.setCookie("user", username, 60*60*24);
            CookieHelper.setCookie("pass", password, 60*60*24);
            CookieHelper.setCookie("host", SVhost, 60*60*24);
            return "index";
         } 
         catch(AuthenticationFailedException e) {
               setMessage("Error email or pass");
               return "index";
         }
         catch(MessagingException e) {
               setMessage("Error email or pass");
               return "index";
         }
    }
} ...

这里是页面索引。登录后,我检查cookie以加载邮件

@ManagedBean(name="loadMail", eager=true)
@ViewScoped
 public class LoadMail {
private Properties pros;
private Session session;
private Store store;
private Folder folder;
private LazyDataModel<Message> lazyModel;

private Message selectedMgs;

@PostConstruct
public void init() {
    Message[] ini = loadAllMailByFolder("INBOX");
    Collections.reverse(Arrays.asList(ini));
    lazyModel = new LazyMessageDataModel(ini);
}

public void onRowSelect(SelectEvent event) throws MessagingException {
    FacesMessage msg = new FacesMessage("Message Selected", ((Message) event.getObject()).getSubject());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public Message[] loadAllMailByFolder(String folderName){
    String username = CookieHelper.getCookie("user").getValue();
    String password = CookieHelper.getCookie("pass").getValue();
    String hostname = CookieHelper.getCookie("host").getValue();

    Message[] mgs = null;
    this.pros = System.getProperties();
    this.pros.setProperty("mail.store.protocol", "imaps");

    this.session = Session.getDefaultInstance(pros);
    try {
        this.store = this.session.getStore("imaps");
        switch(hostname){
            case "gmail":
                this.store.connect("imap.googlemail.com", username, password);
                break;
            case "yahoo":
                this.store.connect("imap.mail.yahoo.com", username, password);
                break;
            default:
                this.store.connect("imap.googlemail.com", username, password);
                break;
        }
        this.folder = store.getFolder(folderName);
        this.folder.open(Folder.READ_ONLY);
        mgs = this.folder.getMessages();

    } catch (NoSuchProviderException ex) {
       ex.printStackTrace();
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
    //lazyModel = new LazyMessageDataModel(mgs);
    return mgs;
}

0 个答案:

没有答案