package com.pj.h5;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pj.sso.SsoRequestUtil;
import com.pj.util.AjaxJson;
import com.pj.util.MyHttpSessionHolder;

/**
 * 前后台分离架构下集成SSO所需的代码 
 * @author kong
 *
 */
@RestController
public class H5Controller {

	// 返回SSO认证中心登录地址 （前后台分离环境下专用）
	@RequestMapping("/sso/getSsoAuthUrl")
	public AjaxJson getSsoAuthUrl(String clientLoginUrl) {
		String serverAuthUrl = SsoRequestUtil.buildServerAuthUrl(clientLoginUrl);
		return AjaxJson.getSuccessData(serverAuthUrl);
	}
	
	// 根据ticket进行登录（前后台分离环境下专用）
	@RequestMapping("/sso/doLoginByTicket")
	public AjaxJson doLoginByTicket(String ticket, HttpServletRequest request) {
		HttpSession session = MyHttpSessionHolder.getSession(request);
		Object loginId = SsoRequestUtil.checkTicket(ticket, request, "/sso/doLoginByTicket");
		if(loginId != null) {
			session.setAttribute("userId", loginId);
			session.setAttribute("user", SsoRequestUtil.getUserInfo(loginId));
			return AjaxJson.getSuccessData(session.getId());
		}
		return AjaxJson.getError("无效ticket：" + ticket); 
	}
	
}
