tianhandigeng 2010-12-26 17:30
浏览 264
已采纳

struts2中add cookie 不能再客户端生成cookie文件,为什么?

我做自动登录的功能,是这样操作的:
[code="java"]
package com.zuwoba.presentation.action.account;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionContext;
import com.zuwoba.model.User;
import com.zuwoba.model.UserSession;
import com.zuwoba.presentation.action.base.BaseAction;
import com.zuwoba.util.EmailLoginAddress;
import com.zuwoba.util.MD5_Encoding;

/**

  • @project_name zuwoba
  • @file_name LoginAction.java
  • @author tianhandigeng
  • @version Oct 25, 2010 2:53:04 PM
  • @declaration
    */
    public class LoginAction extends BaseAction implements ServletRequestAware,ServletResponseAware{
    private String email_username;
    private String password;
    private String captcha;
    private String autologin;

    private HttpServletRequest request;
    private HttpServletResponse response;

    // 提示窗口
    private String hint;
    private String tip;

    public String getTip() {
    return tip;
    }

    public void setTip(String tip) {
    this.tip = tip;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public String getAutologin() {
    return autologin;
    }

    public void setAutologin(String autologin) {
    this.autologin = autologin;
    }

    public String getEmail_username() {
    return email_username;
    }

    public void setEmail_username(String email_username) {
    this.email_username = email_username;
    }

    public String getHint() {
    return hint;
    }

    public void setHint(String hint) {
    this.hint = hint;
    }

    public String getCaptcha() {
    return captcha;
    }

    public void setCaptcha(String captcha) {
    this.captcha = captcha;
    }

    public void setServletRequest(HttpServletRequest servletRequest) {
    this.request=servletRequest;

    }

    public void setServletResponse(HttpServletResponse servletResponse) {
    this.response=servletResponse;
    }

    @SuppressWarnings("unchecked")
    public String execute() {
    //// HttpServletResponse response = (HttpServletResponse) ActionContext
    //// .getContext().get(StrutsStatics.HTTP_RESPONSE);
    //// HttpServletRequest request = (HttpServletRequest) ActionContext
    //// .getContext().get(StrutsStatics.HTTP_REQUEST);
    //// HttpSession session = request.getSession();
    // HttpServletResponse response=(HttpServletResponse)ServletActionContext.getResponse();
    // HttpServletRequest request=(HttpServletRequest)ServletActionContext.getRequest();
    HttpSession session=request.getSession();

    //获得域名
    

    // String host=request.getServerName();
    String host="ceshi";

    email_username = this.getEmail_username().trim();
    password = this.getPassword().trim();
    captcha = this.getCaptcha().trim().toLowerCase();
    autologin = this.getAutologin();
    
    // 密码加密
    MD5_Encoding md5 = new MD5_Encoding();
    password = md5.getMD5ofStr(password);
    
    if (!captcha.equals(session.getAttribute("idcode"))) {
        this.setTip("验证码错误");
        this.setHint("failed");
        return INPUT;
    } else {
        // Email登录
        User user1 = userService.findUserByEmailPassword(email_username,
                password);
    
        if (user1 != null) {
            if ("N".equals(user1.getEnable())) {// 邮件登陆但没有激活
                String emailAddress = EmailLoginAddress
                        .getEmialLoginAddress(email_username);
    
                session.setAttribute("email", email_username);
                session.setAttribute("secret", user1.getSecret());
                session.setAttribute("tempname", user1.getUsername());
                session.setAttribute("emailAddress", emailAddress);
    
                return "unverified";
            } else {
                // 处理自动登录
                if (autologin != null) {
                    // 创建两个cookie对象
                    // 一个cookie记录用户名,另一个记录唯一的验证码
                    // 并将此验证码写入数据库,以备用户返回时查询(防止伪造cookie)
                    Cookie cookie1 = new Cookie("SESSION_LOGIN_USERNAME", user1
                             .getUsername());
                    cookie1.setMaxAge(60 * 60 * 24 * 14);// 设置cookie有效期为2周
                    cookie1.setPath("/");
                    cookie1.setDomain(host);
                    response.addCookie(cookie1);
    
                    String sessionid = session.getId();
                    Cookie cookie2 = new Cookie("SESSION_ID", sessionid);
                    cookie2.setMaxAge(60 * 60 * 24 * 14);
                    cookie2.setPath("/");
                    cookie2.setDomain(host);
                    response.addCookie(cookie2);
    
                    // 在数据库中插入相应记录
                    UserSession userSession = new UserSession();
                    userSession.setUsername(user1.getUsername());
                    userSession.setSessionid(sessionid);
                    userService.addUserSession(userSession);
                }
                session.setAttribute("user", user1);
                return SUCCESS;
            }
        } else {
            // 用户名登陆
            User user2 = userService.findUserByUserNamePassword(
                    email_username, password);
            if (user2 != null) {
                if ("N".equals(user2.getEnable())) {// 用户名登陆但没有激活
                    String emailAddress = EmailLoginAddress
                            .getEmialLoginAddress(email_username);
    
                    session.setAttribute("email", email_username);
                    session.setAttribute("secret", user2.getSecret());
                    session.setAttribute("tempname", user2.getUsername());
                    session.setAttribute("emailAddress", emailAddress);
    
                    return "unverified";
                } else {
    
                    // 处理自动登录
                    if (autologin != null) {
                        // 创建两个cookie对象
                        // 一个cookie记录用户名,另一个记录唯一的验证码
                        // 并将此验证码写入数据库,以备用户返回时查询(防止伪造cookie)
                        Cookie cookie1 = new Cookie("SESSION_LOGIN_USERNAME", user2
                                 .getUsername());
                        cookie1.setMaxAge(60*60*24*14);// 设置cookie有效期为2周
                        cookie1.setPath("/");
                        cookie1.setDomain(host);
                        response.addCookie(cookie1);
    
                        String sessionid = session.getId();
                        Cookie cookie2 = new Cookie("SESSION_ID", sessionid);
                        cookie2.setMaxAge(60 * 60 * 24 * 14);
                        cookie2.setPath("/");
                        cookie2.setDomain(host);
                        response.addCookie(cookie2);
    
                        // 在数据库中插入相应记录
                        UserSession userSession = new UserSession();
                        userSession.setUsername(user2.getUsername());
                        userSession.setSessionid(sessionid);
                        userService.addUserSession(userSession);
                    }
                    session.setAttribute("user", user2);
                    return SUCCESS;
                }
            } else {
                this.setHint("failed");
                this.setTip("不存在这样的Email或用户名或密码错误");
                return INPUT;
            }
        }
    }
    

    }
    }

[/code]
奇怪的是,客户端就是不能生成cookie文件,我真的不知道是为什么了,哪位大哥分析一下是什么原因,小弟感激不尽

  • 写回答

3条回答

  • sunnywolf1010 2010-12-28 17:33
    关注

    没仔细看楼主的代码,你的domain是什么值? domain应该是xxxx.com的形式,比如网站是www.baidu.com,domain应该是baidu.com。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程