|
@@ -0,0 +1,164 @@
|
|
|
+package com.hei.servlet;
|
|
|
+
|
|
|
+import javax.servlet.ServletConfig;
|
|
|
+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 java.io.IOException;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.concurrent.ConcurrentMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * SSOAuth:统一认证中心
|
|
|
+ *
|
|
|
+ * 本类实现简单的实现验证操作
|
|
|
+ *
|
|
|
+ * @author mincong
|
|
|
+ */
|
|
|
+public class AuthServlet extends HttpServlet {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用父类的构造方法
|
|
|
+ */
|
|
|
+ public AuthServlet() {
|
|
|
+ super();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用父类的销毁方法
|
|
|
+ */
|
|
|
+ public void destroy() {
|
|
|
+ super.destroy();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * servlet的doGet方法
|
|
|
+ * 当表单的标签值方法等于get时,将调用此方法。
|
|
|
+ *
|
|
|
+ * @param request 由客户端发送到服务器的请求
|
|
|
+ * @param response 由服务器发送到客户端的响应
|
|
|
+ * @throws ServletException 如果发生错误
|
|
|
+ * @throws IOException 如果发生错误
|
|
|
+ */
|
|
|
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
|
|
|
+ throws ServletException, IOException {
|
|
|
+
|
|
|
+ doPost(request, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * servlet的doPost方法。
|
|
|
+ * 当表单的标签值方法等于post时,调用此方法
|
|
|
+ *
|
|
|
+ * @param request 由客户端发送到服务器的请求
|
|
|
+ * @param response 由服务器发送到客户端的响应
|
|
|
+ * @throws ServletException 如果发生错误
|
|
|
+ * @throws IOException 如果发生错误
|
|
|
+ */
|
|
|
+ public void doPost(HttpServletRequest request, HttpServletResponse response)
|
|
|
+ throws ServletException, IOException {
|
|
|
+ // 解决中文乱码问题
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+
|
|
|
+ System.out.println("进入servlet了");
|
|
|
+ /**
|
|
|
+ * DomainName和CookName这2个参数在web.xml的context-param标签中定义
|
|
|
+ * 校验是否请求登录了,然后存到Cookie那里
|
|
|
+ */
|
|
|
+ DomainName = request.getSession().getServletContext().getInitParameter("DomainName");
|
|
|
+ CookName = request.getSession().getServletContext().getInitParameter("CookieName");
|
|
|
+
|
|
|
+ System.out.println("---------" + DomainName + "----------------跳转了-------------" + CookName + "----------------");
|
|
|
+
|
|
|
+ // 定义登录页面
|
|
|
+ String location = request.getContextPath() + "/login.jsp";
|
|
|
+ // 获取随机生成的附加码
|
|
|
+ String ccode = (String) request.getSession().getAttribute("rand");
|
|
|
+ // 获取我们输入的附加码
|
|
|
+ String checkcode = request.getParameter("checkcode");
|
|
|
+
|
|
|
+ System.out.println("ccode:" + ccode + ", checkcode:" + checkcode);
|
|
|
+
|
|
|
+ // 接收“goto”这个参数来保存用户最初访问的URL:登录失败时暂存,登录成功时调用此url
|
|
|
+ String gotoURL = request.getParameter("goto");
|
|
|
+ System.out.println("gotoURL:" + gotoURL);
|
|
|
+
|
|
|
+ // 判断附加码是否相等
|
|
|
+ // 附加码不等,重定向到认证系统
|
|
|
+ if (!checkcode.equals(ccode)) {
|
|
|
+ response.sendRedirect(gotoURL);
|
|
|
+ System.out.println("error1");
|
|
|
+ } else {
|
|
|
+ // 附加码相等,判断登录账户是否正确
|
|
|
+ System.out.println("success1");
|
|
|
+ // 获取用户输入的用户名
|
|
|
+ String username = request.getParameter("username");
|
|
|
+ // 获取用户输入的密码
|
|
|
+ String userpassword = request.getParameter("userpassword");
|
|
|
+ // 根据预先设定好的用户集合(accounts)的用户名获取对应的密码
|
|
|
+ String key = accounts.get(username);
|
|
|
+ // 密码为空,重定向到认证系统
|
|
|
+ if (key == null) {
|
|
|
+ response.sendRedirect(gotoURL);
|
|
|
+ } else {
|
|
|
+ // 密码不为空,判断密码是否相等
|
|
|
+ // 如果密码相等,保存sessionId到Cookie便于实现SSO
|
|
|
+ if (key.equals(userpassword)) {
|
|
|
+ String sessionId = request.getSession().getId();
|
|
|
+ // 新建Cookie,将用户的信息存进cookie中
|
|
|
+ Cookie cookie = new Cookie(CookName, sessionId);
|
|
|
+ //设置Cookie最大生存时间,单位:秒
|
|
|
+ // 设置Cookie路径 如果不使用这个方法或者参数为负数的话,当浏览器关闭的时候,这个cookie就失效了。
|
|
|
+ cookie.setMaxAge(100);
|
|
|
+ // 让所有访问地址以path开头的servlet共享该cookie,即这种情况下浏览器都可以访问该cookie
|
|
|
+ cookie.setPath("/");
|
|
|
+ // 将Cookie添加到Response中,使之生效
|
|
|
+ response.addCookie(cookie);
|
|
|
+ // 如果用户最初想访问的URL不为空,则登陆成功,跳转到主页面
|
|
|
+ if (gotoURL != null) {
|
|
|
+ System.out.println("gotoURL:" + gotoURL);
|
|
|
+ response.sendRedirect(gotoURL);
|
|
|
+ System.out.println("登录成功!!!!" + cookie + "----------------" + sessionId);
|
|
|
+ } else {
|
|
|
+ // 如果用户最初想访问的URL为空,重定向到认证系统
|
|
|
+ response.sendRedirect(location);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果密码不相等,重定向到认证系统
|
|
|
+ response.sendRedirect(location);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 声明用户账号数组
|
|
|
+ */
|
|
|
+ static private ConcurrentMap<String, String> accounts;
|
|
|
+ /**
|
|
|
+ * 保留sessionId的Cookie
|
|
|
+ */
|
|
|
+ String CookName;
|
|
|
+ /**
|
|
|
+ * 域名
|
|
|
+ */
|
|
|
+ String DomainName;
|
|
|
+ /**
|
|
|
+ * 初始化Servlet时,给系统添加了三个合法的用户
|
|
|
+ * init()方法:当servlet第一次被创建才会调用,整个生命周期内仅调用一次。
|
|
|
+ * @param config 初始化参数
|
|
|
+ * @throws ServletException 如果发生错误
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void init(ServletConfig config) throws ServletException {
|
|
|
+ accounts = new ConcurrentHashMap<String, String>();
|
|
|
+ accounts.put("joylife", "123456");
|
|
|
+ accounts.put("admin", "123456");
|
|
|
+ accounts.put("json", "123456");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|