SSO单点登录学习总结(2)——基于Cookie+fliter单点登录实例

1、使用Cookie解决单点登录

       技术点:

       1、设置Cookie的路径为setPath("/").即Tomcat的目录下都有效

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

       3、设置Cookie的时间。即使用户不选择在几天内自动登录,也应该保存Cookie以保存在当前浏览器没有关闭的情况下有效。

       4、使用Filter自动登录。


实现步骤

1:首先要准备出几个虚拟主机并配置hosts文件,即本机DNS修改本机的C:\Windows\System32\drivers\etc下的hosts文件。


[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;"># localhost name resolution is handled within DNS itself.  
  2. #   127.0.0.1        localhost  
  3. #   ::1             localhost  
  4.   
  5. 127.0.0.1       localhost  
  6. 127.0.0.1       www.bbs.itcast.cn  
  7. 127.0.0.1       www.news.itcast.cn  
  8. 127.0.0.1       www.news.com  
  9. 127.0.0.1       www.bbs.com  
  10. 127.0.0.1       www.server.com  
  11. </span></span>  

增加几个Host节点,通过Cookie实现自动登录,必须配置的虚拟主页满足xxx.itcast.cn,即主域名必须保持一致。


一般web应用中一般部署在web.xml文件中,单点退出相关配置如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;"><filter>  
  2.         <filter-name>CAS Authentication Filter</filter-name>  
  3.         <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
  4.         <init-param>  
  5.         <!--到哪去登录-->  
  6.             <param-name>casServerLoginUrl</param-name>  
  7.             <param-value>http://www.server.com:8080/login</param-value>  
  8.         </init-param>  
  9.         <init-param>  
  10.         <!--我是谁-->  
  11.             <param-name>serverName</param-name>  
  12.             <param-value>http://www.bbs.com:8080</param-value>  
  13.         </init-param>  
  14.         <init-param>  
  15.             <param-name>renew</param-name>  
  16.             <param-value>false</param-value>  
  17.         </init-param>  
  18.         <init-param>  
  19.             <param-name>gateway</param-name>  
  20.             <param-value>false</param-value>  
  21.         </init-param>  
  22.     </filter>  
  23.       
  24.     <filter>  
  25.         <filter-name>CAS Validation Filter</filter-name>  
  26.         <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  
  27.         <init-param>  
  28.             <param-name>casServerUrlPrefix</param-name>  
  29.             <param-value>http://www.server.com:8080</param-value>  
  30.         </init-param>  
  31.         <init-param>  
  32.             <param-name>serverName</param-name>  
  33.             <param-value>http://www.bbs.com:8080</param-value>  
  34.         </init-param>  
  35.           
  36.           
  37.         <!--  
  38.         <init-param>  
  39.             <param-name>proxyCallbackUrl</param-name>  
  40.             <param-value>https://localhost:8443/mywebapp/proxyCallback</param-value>  
  41.         </init-param>  
  42.         <init-param>  
  43.             <param-name>proxyReceptorUrl</param-name>  
  44.             <param-value>/mywebapp/proxyCallback</param-value>  
  45.         </init-param>  
  46.         -->  
  47.     </filter>  
  48.       
  49.     <filter>  
  50.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  51.         <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  
  52.     </filter>  
  53.       
  54.     <filter>  
  55.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  56.         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
  57.     </filter>  
  58.   
  59.     <!-- ************************* -->  
  60.   
  61. <!-- Sign out not yet implemented -->  
  62. <!--   
  63.     <filter-mapping>  
  64.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  65.         <url-pattern>/*</url-pattern>  
  66.     </filter-mapping>  
  67. -->  
  68.   
  69.     <filter-mapping>  
  70.         <filter-name>CAS Authentication Filter</filter-name>  
  71.         <url-pattern>/protected/*</url-pattern>  
  72.     </filter-mapping>  
  73.   
  74.     <filter-mapping>  
  75.         <filter-name>CAS Validation Filter</filter-name>  
  76.         <url-pattern>/*</url-pattern>  
  77.     </filter-mapping>  
  78.        
  79.     <filter-mapping>  
  80.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  81.         <url-pattern>/*</url-pattern>  
  82.     </filter-mapping>  
  83.       
  84.     <filter-mapping>  
  85.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  86.         <url-pattern>/*</url-pattern>  
  87.     </filter-mapping>  
  88.       
  89.     <filter-mapping>  
  90.         <filter-name>CAS Validation Filter</filter-name>  
  91.         <url-pattern>/proxyCallback</url-pattern>     
  92.     </filter-mapping>  
  93.       
  94.     <!--  *********************** -->  
  95.   
  96. <!-- Sign out not yet implemented -->  
  97. <!--   
  98.     <listener>  
  99.         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  
  100.     </listener>  
  101. -->  
  102.   
  103.     <!--  *********************** -->  
  104.   
  105.     <welcome-file-list>  
  106.         <welcome-file>index.jsp</welcome-file>  
  107.     </welcome-file-list></span></span>  

说明:我们看到单点退出的相关类结构,web.xml配置了单点退出的相关类(1个监听器SingleSignOutHttpSessionListener,2个过滤器SingleSignOutFilter,SimpleServerLogoutHandler)。

       

       实现利用了session存储机制,SessionStoreManager是个单例类,用于管理session的存储、删除;SessionMappingStorage是session的存储、删除的执行者,可以看到实际存储的结构是一个artifactId、sessionId为名值对的HashMap表;监听器SingleSignOutHttpSessionListener的作用是session销毁时,调用session管理单例类SessionStoreManager进行session的删除销毁;

        SingleSignOutFilter的作用有2个:一个是在单点访问拦截安全资源时调用单例类SessionStoreManager存储session,另一个是在单点退出时调用单例类SessionStoreManager删除session;SimpleServerLogoutHandler的作用是将客户端的退出请求转发到SSO服务器端,集中处理做各个子系统的单点退出。


2、先在bbs(或是mail)虚拟目录下,开发一个可以自动登录的程序,使用Filter:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">1、登录的主页如下:  
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  4. <html>  
  5.   <head>  
  6.   </head>  
  7.   <body>  
  8.         <p>在同一台服务器上,多个站点自动登录....>>:<%=session.getId()%></p>  
  9.         <c:if test="${empty sessionScope.user}">  
  10.             <form name="f" method="post" action="<c:url value='/login'/>">  
  11.                 Name:<input type="text" name="name"/><br/>  
  12.                 Pwd:<input type="text" name="pwd"/><br/>  
  13.                 <input type="checkbox" name="chk" value="7">一周内自动登录<br/>  
  14.                 <input type="submit" value="登录"/>           
  15.             </form>  
  16.         </c:if>  
  17.         <c:if test="${not empty sessionScope.user}">  
  18.             欢迎你:${user}。<a href="<c:url value='/loginout'/>">安全退出</a>  
  19.         </c:if>  
  20.         <br/>  
  21.         相关站点:(只要在一边登录成功,即可以自动登录到另一个程序)<br/>  
  22.         <a href="http://mail.itcast.com:7777">mail.itcast.com</a><br/>  
  23.         <a href="http://bbs.itcast.com:7777">bbs.itcast.com</a><br/>  
  24.   </body>  
  25. </html>  
  26. </span></span>  

2、登录的Servlet程序如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">/** 
  2.  * 用户登录 
  3.  */  
  4. public class LoginServlet extends HttpServlet{  
  5.     public void doGet(HttpServletRequest req, HttpServletResponse resp)  
  6.             throws ServletException, IOException {  
  7.         doPost(req, resp);  
  8.     }  
  9.     public void doPost(HttpServletRequest req, HttpServletResponse resp)  
  10.             throws ServletException, IOException {  
  11.         String nm = req.getParameter("name");  
  12.         String pwd = req.getParameter("pwd");  
  13.         String chk = req.getParameter("chk");   //是否选中了7天自动登录  
  14.         String forward = "/index.jsp";  
  15.         if(nm!=null && !nm.trim().equals("") && nm.startsWith("it")//用户名是it开始,且密码是pwd开始的可以登录  
  16.                 && pwd !=null && !pwd.trim().equals("") &&  
  17.                 pwd.startsWith("pwd")){  
  18.             System.err.println("登录成功。。。。。");  
  19.             forward = "/jsps/welcome.jsp";  
  20.             //无论如何,都要设置cookie,如果没有选择自动登录,则只在当前页面的跳转时有效,否则设置有效期间为7天。  
  21.             Cookie cookie = new Cookie("autologin",nm+"@"+pwd);  
  22.             cookie.setPath("/");                //如果路径为/则为整个tomcat目录有用  
  23.             cookie.setDomain(".itcast.com");    //设置对所有*.itcast.com为后缀的域名效  
  24.             if(chk!=null){  
  25.                 int time = 1*60*60*24*7;    //1秒*60=1分*60分=1小时*24=1天*7=7天  
  26.                 cookie.setMaxAge(time);  
  27.             }  
  28.             resp.addCookie(cookie);  
  29.             req.getSession().setAttribute("user", nm);  
  30.         }else{  
  31.             System.err.println("登录不成功。。。。。。");  
  32.         }  
  33.         req.getRequestDispatcher(forward).forward(req, resp);  
  34.     }  
  35. }  
  36.   
  37. </span></span>  

3、自动登录的Filter程序如下:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">/** 
  2.  * 自动登录 
  3.  */  
  4. public class AutoLogin implements Filter {  
  5.     public void destroy() {}  
  6.     public void doFilter(ServletRequest req, ServletResponse resp,  
  7.             FilterChain chain) throws IOException, ServletException {  
  8.         System.err.println("开始自动登录验证.....");//此类中应该对登录的servlet直接放行。根据判断url决定。  
  9.         HttpServletRequest requ = (HttpServletRequest) req;  
  10.         HttpSession s = requ.getSession();  
  11.         if (s.getAttribute("user") != null) {//如果用户已经登录则直接放行  
  12.             System.err.println("用户已经登录,没有必须要再做自动登录。。。。");  
  13.         } else {  
  14.             Cookie[] cookies = requ.getCookies();  
  15.             if (cookies != null) {  
  16.                 for (Cookie ck : cookies) {  
  17.                     if (ck.getName().equals("autologin")) {// 是否是自动登录。。。。  
  18.                         System.err.println("自动登录成功。。。。。");  
  19.                         String val = ck.getValue();  
  20.                         String[] vals = val.split("@");  
  21.                         s.setAttribute("user", vals[0]);  
  22.                     }  
  23.                 }  
  24.             }  
  25.         }  
  26.         chain.doFilter(req, resp);  
  27.     }  
  28.     public void init(FilterConfig filterConfig) throws ServletException {}  
  29. }  
  30. </span></span>  


4、正常退出的Servlet如下

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">/** 
  2.  * 安全退出删除Cookie 
  3.  */  
  4. public class LoginOutServlet extends HttpServlet {  
  5.     public void doGet(HttpServletRequest req, HttpServletResponse resp)  
  6.             throws ServletException, IOException {  
  7.         HttpSession s = req.getSession();       //获取Session  
  8.         Cookie cookie = new Cookie("autologin","");//必须声明一个完全相同名称的Cookie  
  9.         cookie.setPath("/");//路径也要完全相同  
  10.         cookie.setDomain(".itcast.com");//域也要完全相同  
  11.         cookie.setMaxAge(0);//设置时间为0,以直接删除Cookie  
  12.         resp.addCookie(cookie);  
  13.         s.removeAttribute("user");  
  14.         System.err.println("安全退出。。。。。");  
  15.         resp.sendRedirect(req.getContextPath()+"/index.jsp");  
  16.     }  
  17. }  
  18. </span></span>  

这种是基于最简单的方式实现的单点登录

转载于:https://my.oschina.net/zhanghaiyang/blog/606638

猜你喜欢

转载自blog.csdn.net/weixin_34194317/article/details/92662511