OSS单点登录cas-client-core-3.2.1

1. 添加 cas-client-core-3.2.1.jar 包


    org.jasig.cas.client
    cas-client-core

    3.2.1

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 = "";
            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 文件

        

org.jasig.cas.client.session.SingleSignOutHttpSessionListener



CAS Single Sign Out Filter
org.jasig.cas.client.session.SingleSignOutFilter


CAS Single Sign Out Filter
/*



CASFilter
org.jasig.cas.client.authentication.AuthenticationFilter


casServerLoginUrl
http://192.168.1.201:8080/cas/login



serverName
http://192.168.1.202:8080



       CASFilter
        /caslogin.jsp  





CASValidationFilter
com.cusd.oss.MyCas20ProxyReceivingTicketValidationFilter


casServerUrlPrefix
http://192.168.1.201:8080/cas

 
 redirectAfterValidation  
 true  
 


serverName
http://192.168.1.202:8080



CASValidationFilter
/caslogin.jsp

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

你可能感兴趣的:(OSS单点登录cas-client-core-3.2.1)