All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gccloud.starter.sso.cas.controller.CasController Maven / Gradle / Ivy

package com.gccloud.starter.sso.cas.controller;

import com.gccloud.starter.common.utils.HttpUtils;
import com.gccloud.starter.common.utils.XmlUtils;
import com.gccloud.starter.common.entity.SysUserEntity;
import com.gccloud.starter.core.service.ISysTokenService;
import com.gccloud.starter.core.service.ISysUserService;
import com.gccloud.starter.core.vo.SysTokenVO;
import com.gccloud.starter.plugins.cache.common.IStarterCache;
import com.gccloud.starter.sso.cas.cache.TicketCache;
import com.gccloud.starter.sso.cas.config.CasConfig;
import com.gccloud.starter.sso.cas.service.ICasNoUserService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * CAS单点登录,支持cas 20 和 30
 *
 * @author liuchengbiao
 * @date 2021/7/5 11:03 上午
 */
@Slf4j
@Controller
@RequestMapping("/cas")
public class CasController {
    @Resource
    private CasConfig casConfig;
    @Resource
    private ISysUserService userService;
    @Resource
    private ISysTokenService tokenService;
    @Resource
    private IStarterCache starterCache;
    @Resource
    private ICasNoUserService casNoUserService;

    @RequestMapping("/login")
    public void validateLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String ticket = request.getParameter("ticket");
        log.info(ticket);
        if (StringUtils.isBlank(ticket)) {
            log.error("校验ticket失败,ticket 不能为空");
            response.sendRedirect(casConfig.getUiUrl() + "/403?code=loginError");
            return;
        }
        Response validateResponse = HttpUtils.get(casConfig.getServerUrlPrefix() + casConfig.getServiceValidateSuffix() + "?service=" + casConfig.getService() + "&ticket=" + ticket, null);
        String body = validateResponse.body().string();
        log.info("响应报文:{}", body);
        if (!validateResponse.isSuccessful()) {
            log.error("校验ticket失败:{}", body);
            validateResponse.close();
            response.sendRedirect(casConfig.getUiUrl() + "/403");
            return;
        }
        String error = XmlUtils.getTextForElement(body, "authenticationFailure");
        if (StringUtils.isNotBlank(error)) {
            log.error("登录失败,{}", body);
            validateResponse.close();
            response.sendRedirect(casConfig.getUiUrl() + "/403?code=loginError");
            return;
        }
        String username = XmlUtils.getTextForElement(body, "user");
        if (StringUtils.isBlank(username)) {
            log.error("登录失败,报文中未找到user,{}", body);
            response.sendRedirect(casConfig.getUiUrl() + "/403?code=loginError");
            return;
        }
        SysUserEntity user = userService.getByCount(username);
        if (user == null) {
            log.error("登录失败,本系统不存在该用户,{}", username);
            casNoUserService.handle(request, response, ticket, body);
            return;
        }
        SysTokenVO token = tokenService.create(user.getId());
        TicketCache ticketCache = new TicketCache();
        ticketCache.setTicket(ticket);
        ticketCache.setToken(token.getToken());
        starterCache.put(TicketCache.class, ticketCache.getTicket(), ticketCache);
        response.sendRedirect(casConfig.getUiUrl() + "/sys/cas?token=" + token.getToken());
        return;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy