1. 程式人生 > >OSS單點登入2

OSS單點登入2

https://blog.csdn.net/emailscott/article/details/79448422

1. 新增 cas-client-core-3.2.1.jar 包

<dependency>
    <groupId>org.jasig.cas.client</groupId>
    <artifactId>cas-client-core</artifactId>

    <version>3.2.1</version>

2. 專案新增 caslogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%><%
/* /ossDemo/shouye.jsp=專案名+首頁 */
response.sendRedirect("/ahome/index.jsp");

%>

3. 編寫 filter

package com.cusd.oss;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.jasig.cas.client.validation.Assertion;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
import sun.misc.BASE64Encoder;
import com.ahome.basic.entity.User;
import com.ahome.basic.service.impl.UserServiceImpl;
public class MyCas20ProxyReceivingTicketValidationFilter extends Cas20ProxyReceivingTicketValidationFilter{
private Logger log = Logger.getLogger(MyCas20ProxyReceivingTicketValidationFilter.class.getName());
public void init() {
        super.init();
    }
protected void onSuccessfulValidation(final HttpServletRequest request, final HttpServletResponse response, final Assertion assertion) {
      //驗證成功後初始化使用者資訊
    System.out.println("----successful validation ,then init the userobject");
String username = assertion.getPrincipal().getName();
    HttpSession session = request.getSession();
    session.setAttribute("username", username);
    session.getAttribute(username);
    
    System.out.println("username=="+username);
    
    // 獲取spring 上的例項
    ServletContext sc = session.getServletContext();
    XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
    UserServiceImpl userService = (UserServiceImpl) cxt.getBean("userService");
    
    /************************/
    //在這裡根據傳過來的username(系統登入帳號)初始化業務資訊
    //此處已經通過cas驗證安全,因此只需要根據使用者名稱初始化session即可
    User user = userService.getUser(username);
    if(user == null) {
log.error("It can not find the user with the username[username="
+ username
+ "] in the ahome, please import the ahome user to the cas db and try again.");

String loginUrl = request.getContextPath() + "/ahome/login.jsp";
String str = "<script language='javascript'>"
                    + "window.top.location.href='"
                    + loginUrl
                    + "';</script>";
            response.setContentType("text/html;charset=UTF-8");// 解決中文亂碼
            try {
                PrintWriter writer = response.getWriter();
                writer.write(str);
                writer.flush();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    } else {
    // 載入使用者資訊到, 不要新增任何調轉地址
    }
    }
    public void destroy() {
        super.destroy();
    }
}

4. 編寫 web.xml 檔案

        <!-- cas開始 -->
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<!--單點退出配置-->
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 該過濾器負責使用者的認證工作,必須啟用它 -->
<filter>
<filter-name>CASFilter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<!-- 內部門戶cas server登入認證地址 -->
<param-name>casServerLoginUrl</param-name>
<param-value>http://192.168.1.201:8080/cas/login</param-value>
</init-param>
<init-param>
<!--第三方系統的主機+埠號,根據實際情況修改-->
<param-name>serverName</param-name>
<param-value>http://192.168.1.202:8080</param-value>
</init-param>
</filter>
<filter-mapping>
       <filter-name>CASFilter</filter-name>
        <url-pattern>/caslogin.jsp</url-pattern>  <!-- 需要過濾第三方系統相關地址 -->
</filter-mapping>


<!-- 該過濾器負責對Ticket的校驗工作,必須啟用它 -->
<filter>
<filter-name>CASValidationFilter</filter-name>
<filter-class>com.cusd.oss.MyCas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<!-- 門戶單點登入認證服務地址(測試環境) -->
<param-name>casServerUrlPrefix</param-name>
<param-value>http://192.168.1.201:8080/cas</param-value>
</init-param>
<init-param>  
 <param-name>redirectAfterValidation</param-name>  
 <param-value>true</param-value>  
</init-param>  
<init-param>
<!--第三方系統的主機+埠號,根據實際情況修改-->
<param-name>serverName</param-name>
<param-value>http://192.168.1.202:8080</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CASValidationFilter</filter-name>
<url-pattern>/caslogin.jsp</url-pattern>
</filter-mapping>

<!-- cas結束 -->

5. 訪問: http://192.168.1.202:8080/ahome/caslogin.jsp 會跳到 http://192.168.1.201:8080/cas/login.jsp, 登入成功後跳到 http://192.168.1.202:8080/ahome/index.jsp
--------------------- 
作者:ScottSZLuo 
來源:CSDN 
原文:https://blog.csdn.net/emailscott/article/details/79448422 
版權宣告:本文為博主原創文章,轉載請附上博文連結!