CAS Server(二):基于SpringBoot搭建客户端

1. 概述

本篇介绍SpringBoot项目接入CAS Server,先创建一个空的SpringBoot项目

2. pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.unicon.cas</groupId>
            <artifactId>cas-client-autoconfig-support</artifactId>
            <version>1.4.0-GA</version>
        </dependency>
    </dependencies>

3. 创建页面

在templates目录下创建2个页面:

index.html 首页

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>cas-client</title>
</head>
<body>
<h2>Hello:</h2><h2 th:text="${name}"></h2>
<a href="/logout">logout</a>
</body>
</html>

logoutsuccess.html 退出后跳转的页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Logout success! </title>
</head>
<body>
<h1>Logout success!</h1>
<a href="/index">back to index</a>
</body>
</html>

4. 创建 Controller

@Controller
public class CASController {

    @Value("${casClientLogoutUrl}")
    private String clientLogoutUrl;

    @RequestMapping("index")
    public String index(ModelMap map, HttpServletRequest request) {
    	//获取登录的用户名
        map.addAttribute("name", request.getUserPrincipal());
        return "index";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:" + clientLogoutUrl;
    }

    @RequestMapping("logoutsuccess")
    public String logoutsuccess(HttpSession session) {
        return "logoutsuccess";
    }
}

5. application.properties

server.port=99

cas.server-url-prefix=http://127.0.0.1:8080/cas
cas.server-login-url=http://127.0.0.1:8080/cas/login
cas.client-host-url=http://127.0.0.1:99
cas.use-session=true
cas.validation-type=cas

# 自定义的退出url
casClientLogoutUrl=http://127.0.0.1:8080/cas/logout?service=http://127.0.0.1:99/logoutsuccess

6. 创建过滤器

@Configuration
public class CASAutoConfig {
    @Value("${cas.server-url-prefix}")
    private String serverUrlPrefix;
    @Value("${cas.server-login-url}")
    private String serverLoginUrl;
    @Value("${cas.client-host-url}")
    private String clientHostUrl;

    @Bean
    public FilterRegistrationBean filterAuthenticationRegistration(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        // 设定匹配的路径
        registration.addUrlPatterns("/*");
        Map<String,String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerLoginUrl", serverUrlPrefix);
        initParameters.put("serverName", clientHostUrl);
        //忽略的url,"|"分隔多个url
        //initParameters.put("ignorePattern", "/logoutsuccess|/index");
        initParameters.put("ignorePattern", "/logoutsuccess");
        registration.setInitParameters(initParameters);
        // 设定加载的顺序
        registration.setOrder(1);
        return registration;
    }
}

7. 测试

打开首页地址:http://localhost:99/index,会跳转到CAS Server登录页面。这时候注意看浏览器的地址:http://127.0.0.1:8080/cas/login?service=http%3A%2F%2F127.0.0.1%3A99%2Findex,自动携带的service参数就是我们刚才访问的页面

输入账号密码,登录后会跳转会前面service的页面地址

在这里插入图片描述
注意看前面的application.properties,lotout 链接我们同样传了service参数,目的是CAS Server单点登出后能够跳回我们指定的页面。

CAS Server默认不会开启登出跳转,需要修改E:\apache-tomcat-8.5.59\webapps\cas\WEB-INF\cas.properties文件,将cas.logout.followServiceRedirects属性值改成true

在这里插入图片描述
成功退出后,CAS会跳回我们的页面

在这里插入图片描述

到这里,简单的 SpringBoot CAS 客户端就搭建成功了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

°Fuhb

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值