Springboot cas client实战项目总结(实例)

前言–项目需求

在智慧校园系统中,加入一个超链接,直接跳转到XX系统。

思路

前提:本系统同步了单点登录服务器中的账户信息

点击超链接时,访问XX系统接口,由xx系统接口去访问sos服务器,取得当前登录账户的信息,
若与xx系统同步的信息匹配,则由sos服务器回调成功url。
(中间涉及在sos服务器配置xx系统信息和回调成功地址信息等)

1.导包

pom文件依赖:

        <!--CAS Client-->
        <dependency>
            <groupId>org.jasig.cas.client</groupId>
            <artifactId>cas-client-core</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!--CAS Client Autoconfig-Support-->
        <!-- https://mvnrepository.com/artifact/net.unicon.cas/cas-client-autoconfig-support -->
        <dependency>
            <groupId>net.unicon.cas</groupId>
            <artifactId>cas-client-autoconfig-support</artifactId>
            <version>2.3.0-GA</version>
        </dependency>

2.配置

application.properties 文件配置

#cas配置
#cas服务端前缀
cas.server-url-prefix=http://authserver.swun.edu.cn/authserver
#cas的登录地址
cas.server-login-url=http://authserver.swun.edu.cn/authserver/login
#当前客户端的地址(替换 me.local 为域名或ip地址  ip或域名需要在cas server授权)
cas.client-host-url=http://me.local/meeting-web/casClient/loginByCasClient
#cas.client-host-url=http://172.18.0.246/meeting-web/casClient/loginByCasClient
cas.validation-type=CAS3
#设置拦截url地址
cas.authentication-url-patterns[0]=/casClient/loginByCasClient
cas.validation-url-patterns[0]=/casClient/loginByCasClient
cas.request-wrapper-url-patterns[0]=/casClient/loginByCasClient
cas.assertion-thread-local-url-patterns[0]=/casClient/loginByCasClient

3.编写逻辑完成登录

退出同理,xx系统采用jwt,逻辑类比。

 /**
     * 1. 人员组织已通过定时任务同步 2. cas已登录 1.1 重定向
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @AuthIgnore
    @ApiOperation("登录 - loginByCasClient")
    @GetMapping(value = "/loginByCasClient")
    public String loginByCasClient(HttpServletRequest request, HttpServletResponse response, HttpSession sessions)
            throws IOException {
        String cas_token = (String) sessions.getAttribute("cas_token");
        if ( !"".equals(cas_token)&&null!=cas_token) {
            logger.info("直接登录!");
            return "<!--\n"
                    + "    @author : biuaxia\n"
                    + "    @date: 2020/1/15 10:42\n"
                    + "    @apiNote: contact biuaxia@qq.com,\n"
                    + "-->\n"
                    + "<!DOCTYPE html>\n"
                    + "<html lang=\"en\">\n"
                    + "<head>\n"
                    + "    <meta charset=\"utf-8\"/>\n"
                    + "    <title>引导页</title>\n"
                    + "    <title>恭喜,站点创建成功!</title>\n"
                    + "    <style>\n"
                    + "        .container {\n"
                    + "            width: 60%;\n"
                    + "            margin: 10% auto 0;\n"
                    + "            background-color: #f0f0f0;\n"
                    + "            padding: 2% 5%;\n"
                    + "            border-radius: 10px;\n"
                    + "        }\n"
                    + "\n"
                    + "        ul {\n"
                    + "            padding-left: 20px;\n"
                    + "        }\n"
                    + "\n"
                    + "        ul li {\n"
                    + "            line-height: 2.3;\n"
                    + "        }\n"
                    + "\n"
                    + "        a {\n"
                    + "            color: #20a53a;\n"
                    + "        }\n"
                    + "    </style>\n"
                    + "</head>\n"
                    + "<body>\n"
                    + "<div class=\"container\">\n"
                    + "    <h1>恭喜, 登录成功!</h1>\n"
                    + "    <h3>3秒后为您跳转到会议系统页面,请耐心等待</h3>\n"
                    + "    <ul>\n"
                    + "        <li>本页面由系统自动生成</li>\n"
                    + "        <li>您可以忽略本页面,直接访问会议系统/meeting-web</li>\n"
                    + "        <li>直接登录</li>\n"
                    + "    </ul>\n"
                    + "</div>\n"
                    + "\n"
                    + "<script>\n"
                    + "    localStorage.setItem(\"token\", \""
                    + cas_token
                    + "\");\n"
                    + "    setTimeout(function () {\n"
                    + "        window.location.href = '/meeting-web/#/personalSet'\n"
                    + "    }, 3000);\n"
                    + "</script>\n"
                    + "</body>\n"
                    + "</html>";
        }
        String loginName = request.getRemoteUser();
        String userName;
        if (loginName == null || "".equals(loginName)) {
            logger.info("未登录、重定向到默认登录页面");
            response.sendRedirect(context_path);
        } else {
            Principal principal = request.getUserPrincipal();
            AttributePrincipal aPrincipal = (AttributePrincipal) principal;
            Map<String, Object> map = aPrincipal.getAttributes();
            userName = (String) map.get("cn");
            logger.info(
                    String.format("login By CasClient  -- loginName: %s, userName: %s", loginName, userName));
        }
        BaseUserinfo existUserInfo = baseUserinfoService.getByLoginName(loginName);
        if (existUserInfo == null) {
            throw new RequestParamException(String.format("用户 %s 不存在.", loginName));
        }

        if (existUserInfo.getUserStatus().intValue() == 0) {
            throw new RequestParamException("用户被禁用");
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(existUserInfo.getId());
        userInfo.setLoginName(existUserInfo.getLoginName());
        userInfo.setUserName(existUserInfo.getUserName());
        userInfo.setDeptId(existUserInfo.getOrgId());

        cas_token = tokenService.generateToken(userInfo);
        sessions.setAttribute("cas_token", cas_token);
        logger.info("正常登录!");
        // TODO biuaxia 可以在登录时检查session,若存在直接登录,反之跳转cas,在退出时移除session的内容(仅供参考, 2020年1月15日14:33:53)
        return "<!--\n"
                + "    @author : biuaxia\n"
                + "    @date: 2020/1/15 10:42\n"
                + "    @apiNote: contact biuaxia@qq.com,\n"
                + "-->\n"
                + "<!DOCTYPE html>\n"
                + "<html lang=\"en\">\n"
                + "<head>\n"
                + "    <meta charset=\"utf-8\"/>\n"
                + "    <title>引导页</title>\n"
                + "    <title>恭喜,站点创建成功!</title>\n"
                + "    <style>\n"
                + "        .container {\n"
                + "            width: 60%;\n"
                + "            margin: 10% auto 0;\n"
                + "            background-color: #f0f0f0;\n"
                + "            padding: 2% 5%;\n"
                + "            border-radius: 10px;\n"
                + "        }\n"
                + "\n"
                + "        ul {\n"
                + "            padding-left: 20px;\n"
                + "        }\n"
                + "\n"
                + "        ul li {\n"
                + "            line-height: 2.3;\n"
                + "        }\n"
                + "\n"
                + "        a {\n"
                + "            color: #20a53a;\n"
                + "        }\n"
                + "    </style>\n"
                + "</head>\n"
                + "<body>\n"
                + "<div class=\"container\">\n"
                + "    <h1>恭喜, 登录成功!</h1>\n"
                + "    <h3>3秒后为您跳转到会议系统页面,请耐心等待</h3>\n"
                + "    <ul>\n"
                + "        <li>本页面由系统自动生成</li>\n"
                + "        <li>您可以忽略本页面,直接访问会议系统/meeting-web</li>\n"
                + "        <li>正常登录</li>\n"
                + "    </ul>\n"
                + "</div>\n"
                + "\n"
                + "<script>\n"
                + "    localStorage.setItem(\"token\", \""
                + cas_token
                + "\");\n"
                + "    setTimeout(function () {\n"
                + "        window.location.href = '/meeting-web/#/personalSet'\n"
                + "    }, 3000);\n"
                + "</script>\n"
                + "</body>\n"
                + "</html>";
    }

退出

    @AuthIgnore
    @ApiOperation("退出-logoutByCasClient")
    @PostMapping("/logoutCasClient")
    public void logoutByCas(HttpServletRequest request) throws ServletException {
        tokenService.logout(request.getHeader("token"));
        request.getSession().invalidate();
        request.logout();
        logger.info("logout By  CasClient Success!");
    }

注:退出只退出了xx系统,应考虑做单点退出。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Springboot CAS-Client 是一个基于Springboot框架集成CAS(Central Authentication Service)的客户端。 CAS是一种单点登录(Single Sign-On)协议,它允许用户在一次登录后就能够访问多个应用,而无需重新认证。 Springboot CAS-Client 的作用是充当CAS服务端和应用系统之间的中间件,它负责向CAS服务端发送认证请求,并根据认证结果来管理用户的登录状态。 为了集成CAS,我们首先需要在Springboot项目中引入相应的依赖,例如spring-boot-starter-web和spring-boot-starter-security。接着,我们需要配置CAS服务端的地址信息,包括CAS服务端的登录URL、登出URL以及验证票据的URL等。 在Springboot CAS-Client中,我们也可以自定义一些过滤器和拦截器来实现相关的功能。例如,我们可以编写一个CAS认证过滤器来拦截所有的请求,并判断用户的登录状态。如果用户未登录,则跳转到CAS服务端进行认证;如果用户已登录,则直接放行请求。此外,我们还可以编写一个CAS登出拦截器来处理用户的登出请求,并在登出完成后将用户重定向到指定的页面。 总的来说,Springboot CAS-Client 提供了一个简洁、灵活的方式来集成CAS协议,使得我们的Springboot应用能够享受到单点登录带来的便利。通过它,我们可以轻松地实现用户认证、登录状态管理以及注销等功能,提升用户体验并提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳宗德

您的鼓励是我进步的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值