CAS 之 实现用户注册后自动登录

来源:互联网 发布:繁忙的收发室 优化 编辑:程序博客网 时间:2024/05/23 19:18

转自:http://denger.iteye.com/blog/805743   


1. 关于CAS的介绍不再累述,我想涉及过SSO同学应该都会对该框架所有了解,我们目前项目采用的CAS Server 版本为 3.4.2.1, 其 CAS Client 版本为 3.1.10。

         CAS项目官方:http://www.jasig.org/cas
         本文讲述CAS登录处理未包括 CAS Client 与 Server 端的对 ST 采用SMAL验证的流程。

     2. 对于登录其主要处理流程:
         注册成功后 -> 调用CAS登录处理的相关模块 -> 验证用户名密码 -> 生成TGT -> 生成TG -> Add ST&TGT至相关Register类 -> Add TGT至Cookie -> 重定向至 cas/login URL -> 完成


     3. 
CAS 登录处理主要模块(类):
              a. Credentials  用于存储用户登录认证信息接口。
                  其默认实现类:org.jasig.cas.authentication.principal.UsernamePasswordCredentials

              b. CentralAuthenticationService 用于生成 ST(Service Ticket) 和  TGT(TicketGrantingTicket)的认证服务类。
                  其默认实现类: org.jasig.cas.CentralAuthenticationServiceImpl

              c. CookieRetrievingCookieGenerator 用于将TGT添加至Cookie及对Cookie进行管理。


     4.  具体实现代码:

Java代码  收藏代码
  1. /** 
  2.  * user register process and automatic login. 
  3.  * @param userForm the user information object. 
  4.  * @param request  the HttpServletRequest object 
  5.  * @param response the HttpServletResponse object 
  6.  * @return get result view 
  7.  */  
  8. protected ModelAndView handleUserRegisterInternal(UserInfoVo userForm, HttpServletRequest request, HttpServletResponse response) {  
  9.       
  10.     ModelAndView signinView = new ModelAndView(REGISTER_VIEW);;  
  11.     final boolean isUnique = userService.checkUserUnique(userForm.getLoginName());  
  12.     final boolean isRegistered = isUnique ? registerUser(userForm, request, response) : false;  
  13.   
  14.     if (isRegistered) {  
  15.         bindTicketGrantingTicket(userForm.getLoginName(), userForm.getLoginPassword(), request, response);  
  16.         signinView.setViewName(getSignInView(request));  
  17.     }  
  18.     return signinView;  
  19. }  

          
Java代码  收藏代码
  1. /** 
  2.  * Invoke generate validate Tickets and add the TGT to cookie. 
  3.  * @param loginName     the user login name. 
  4.  * @param loginPassword the user login password. 
  5.  * @param request       the HttpServletRequest object. 
  6.  * @param response      the HttpServletResponse object. 
  7.  */  
  8. protected void bindTicketGrantingTicket(String loginName, String loginPassword, HttpServletRequest request, HttpServletResponse response){  
  9.     try {  
  10.         UsernamePasswordCredentials credentials = new UsernamePasswordCredentials();  
  11.         credentials.setUsername(loginName);  
  12.         credentials.setPassword(loginPassword);  
  13.         String ticketGrantingTicket = centralAuthenticationService.createTicketGrantingTicket(credentials);  
  14.         ticketGrantingTicketCookieGenerator.addCookie(request, response, ticketGrantingTicket);  
  15.     } catch (TicketException te) {  
  16.         logger.error("Validate the login name " + loginName + " failure, can't bind the TGT!", te);  
  17.     } catch (Exception e){  
  18.         logger.error("bindTicketGrantingTicket has exception.", e);  
  19.     }  
  20. }  



Java代码  收藏代码
  1. /** 
  2.  * Get the signIn view URL. 
  3.  * @param request the HttpServletRequest object. 
  4.  * @return redirect URL 
  5.  */  
  6. protected String getSignInView(HttpServletRequest request) {  
  7.     String service = ServletRequestUtils.getStringParameter(request, "service""");  
  8.     return ("redirect:login" + (service.length() > 0 ? "?service=" + service : ""));  
  9. }  

   
cas-servlet.xml 相关代码:
Xml代码  收藏代码
  1. <bean id="registerController" class="com.xxxxx.sso.web.RegisterController"   
  2.     p:userService-ref="userService"  
  3.     p:validator-ref="registerValidator"  
  4.     p:centralAuthenticationService-ref="centralAuthenticationService"  
  5.     p:ticketGrantingTicketCookieGenerator-ref="ticketGrantingTicketCookieGenerator"/>  

注: 关于centralAuthenticationService及ticketGrantingTicketCookieGenerator已声明在 spring-configuration/applicationContext.xml 和 ticketGrantingTicketCookieGenerator.xml中
原创粉丝点击