單點登陸(SSO)問題

  單點登陸(Single Sign On),簡稱爲 SSO,是目前比較流行的企業業務整合的解決方案之一。SSO的定義是在多個應用系統中,用戶只須要登陸一次就能夠訪問全部相互信任的應用系統。html

--------------------------------------------------java

www.bbs.itcast.cn和www.mail.itcast.cn可使用cookie+filter的形式實現單點登陸。跨域

一級域名相同,二級域名不一樣瀏覽器

www.bbs.itcast.cntomcat

www.mail.itcast.cn安全

--------------------------------------------------服務器

一、使用Cookie解決單點登陸cookie

       技術點:session

  一、設置Cookie的路徑爲setPath("/") .即Tomcat的目錄下都有效app

  二、設置Cookie的域setDomain(".itcast.com");即bbs.itcast.com,或是mail.itcast.com有效。即跨域。

  三、設置Cookie的時間。即便用戶不選擇在幾天內自動登陸,也應該保存Cookie以保存在當前瀏覽器沒有關閉的狀況下有效。

  四、使用Filter自動登陸。

實現步驟:

一、首先要準備出幾個虛擬主機並配置hosts文件,即本機DNS。

127.0.0.1       localhost
127.0.0.1       www.bbs.itcast.cn
127.0.0.1       www.mail.itcast.cn

配置虛擬主機,主要經過修改tomcat_home/conf/server.xml文件完成:

<Host name="www.bbs.itcast.cn"  appBase="bbs"
            unpackWARs="true" autoDeploy="true"/>
            
<Host name="www.mail.itcast.cn"  appBase="mail"
            unpackWARs="true" autoDeploy="true"/>

增長几個Host節點,經過Cookie實現自動登陸,必須配置的虛擬主頁知足xxx.itcast.cn,即主域名必須保持一致。

二、先在bbs(或是mail)虛擬目錄下,開發一個能夠自動登陸的程序,使用Filter:

  一、登陸的主頁以下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
  <head>
  </head>
  <body>
        <p>在同一臺服務器上,多個站點自動登陸(只要在一邊登陸成功,便可以自動登陸到另外一個程序)....
        </p>
        <c:if test="${empty sessionScope.user}">
            <form name="f" method="post" action="<c:url value='/login'/>">
                userName:<input type="text" name="userName"/><br/>
                passWord:<input type="text" name="password"/><br/>
                <input type="checkbox" name="check" value="7">一週內自動登陸<br/>
                <input type="submit" value="登陸"/>           
            </form>
        </c:if>
        <c:if test="${not empty sessionScope.user}">
            歡迎你:${user}。<a href="<c:url value='/loginout'/>">安全退出</a>
        </c:if>
        <br/>
  </body>
</html>

二、登陸的Servlet程序以下:

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String check = request.getParameter("check");    //是否選中了7天自動登陸
        if(userName!=null && !userName.trim().equals("") && userName.startsWith("it")//用戶名是it開始,且密碼是pwd開始的能夠登陸
                && password !=null && !password.trim().equals("") &&
                        password.startsWith("pwd")){
            System.err.println("登陸成功。。。。。");
            request.getSession().setAttribute("user", userName);
            //不管如何,都要設置cookie,若是沒有選擇自動登陸,則只在當前頁面的跳轉時有效,不然設置有效期間爲7天。
            Cookie cookie = new Cookie("sso",userName);
            cookie.setPath("/");                 //若是路徑爲/則爲整個tomcat目錄有用
            cookie.setDomain(".itcast.cn");    //設置對全部*.itcast.cn爲後綴的域名效
            if(check!=null){
                int time = 1*60*60*24*7;    //1秒*60=1分*60分=1小時*24=1天*7=7天
                cookie.setMaxAge(time);
            }
            response.addCookie(cookie);
        }else{
            System.err.println("登陸不成功。。。。。。");
        }
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

}

三、自動登陸的Filter程序以下:

public void doFilter(ServletRequest req, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        System.err.println("開始自動登陸驗證.....");//此類中應該對登陸的servlet直接放行。根據判斷url決定。
        HttpServletRequest request = (HttpServletRequest) req;
        HttpSession s = request.getSession();
        if (s.getAttribute("user") != null) {//若是用戶已經登陸則直接放行
            System.err.println("用戶已經登陸,沒有必需要再作自動登陸。。。。");
        } else {
            Cookie[] cookies = request.getCookies();
            if (cookies != null&&cookies.length>0) {
                for (Cookie ck : cookies) {
                    if (ck.getName().equals("sso")) {// 是不是自動登陸。。。。
                        System.err.println("自動登陸成功。。。。。");
                        String userName = ck.getValue();
                        s.setAttribute("user", userName);
                    }
                }
            }
        }
        chain.doFilter(req, response);
    }
<filter>
        <filter-name>autoLogin</filter-name>
        <filter-class>cn.itcast.sso.filter.AutoLoginFilter</filter-class>
    </filter>    
    <filter-mapping>
        <filter-name>autoLogin</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

四、正常退出的Servlet以下

public class LoginOutServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        HttpSession s = req.getSession();        //獲取Session
        Cookie cookie = new Cookie("user","");//必須聲明一個徹底相同名稱的Cookie
        cookie.setPath("/");//路徑也要徹底相同
        cookie.setDomain(".itcast.com");//域也要徹底相同
        cookie.setMaxAge(0);//設置時間爲0,以直接刪除Cookie
        resp.addCookie(cookie);
        s.removeAttribute("user");
        System.err.println("安全退出。。。。。");
        resp.sendRedirect(req.getContextPath()+"/index.jsp");
    }
}
相關文章
相關標籤/搜索