这篇主要说明基于Cookie的单点登录实现,以及Cookie的一些特性以及使用说明。
package com.csdn.cas;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求参数
String userName = req.getParameter("userName");
String passwd = req.getParameter("passwd");
// 创建cookie对象
Cookie userInfoCookie = new Cookie("userInfo", userName + ":" + passwd);
// 返回给浏览器的数据中添加cookie信息
resp.addCookie(userInfoCookie);
}
}
上面就是简单的操作cookie的代码,我们将这个servlet部署到tomcat中,使用谷歌,并观察相关的cookie信息(使用F12,有调试工具)。
package com.csdn.cas;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求参数
String userName = req.getParameter("userName");
String passwd = req.getParameter("passwd");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();
// 只有用户名与密码相同,则登录成功
if(userName.equals(passwd)){
// 创建cookie对象
Cookie userInfoCookie = new Cookie("userInfo", userName + ":" + passwd);
// 这里很重要,不设置无法夸子域 这里最好以 .开头,例如.qiandu.com
// 谷歌浏览器自动给他添加了.
userInfoCookie.setDomain("qiandu.com");
// 返回给浏览器的数据中添加cookie信息
resp.addCookie(userInfoCookie);
session.setAttribute("userName", userName + ",登录成功");
}else {
session.setAttribute("userName", userName + ",登录失败");
}
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
package com.csdn.cas;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request ;
HttpServletResponse resp = (HttpServletResponse) response ;
Cookie[] cookies = req.getCookies();
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();
Object userInfo = session.getAttribute("userName");
if(userInfo == null){ // 没登录
if(cookies != null){ // 有cookie
for(Cookie cookie : cookies){
if("userInfo".equals(cookie.getName())){
String[] value = cookie.getValue().split(":");
String userName = value[0];
String passwd = value[1];
// 只有用户名与密码相同,则登录成功
if(userName.equals(passwd)){
// 创建cookie对象
session.setAttribute("userName", userName + ",从filter登录成功");
}else {
session.setAttribute("userName", userName + ",从filter登录失败");
}
}
}
} else {
// 这里应该跳转到登录页面
}
}
chain.doFilter(request, response);
}
@Override
public void destroy() { }
}
Hello !
<%=session.getAttribute("userName")%>