CAS(四)基于Springboot搭建CAS-client,Springboot搭建CAS客户端

环境要求

  • JDK 8+
  • CAS 5.2
  • tomcat 8+

步骤

一、搭建CAS服务器  --> CAS(一)搭建CAS - server服务器

 

二、配置hosts,加入如下配置

127.0.0.1      cas.server.com
127.0.0.1      cas.client1.com

三、搭建Springboot项目

项目名为cas-clientB,项目结构如下:

  • 包含三个页面:主页--index.jsp(无需登录),hello页面--hello.jsp(需要登录),退出成功提示页--logoutsuccess.jsp(无需登录)

 

  • pom.xml文件配置如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.oumuv</groupId>
    <artifactId>cas-clientB</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cas-clientB</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

  • application.properties配置:
cas.server-url-prefix=http://cas.server.com:8443/cas
cas.server-login-url=http://cas.server.com:8443/cas/login
cas.client-host-url=http://cas.client1.com:9002
cas.use-session=true
cas.validation-type=cas
server.port=9002
#自定义的退出url,退出成功后跳转到 http://cas.client1.com:9002/logout/success
casClientLogoutUrl=http://cas.server.com:8443/cas/logout?service=http://cas.client1.com:9002/logout/success

其中http://cas.server.com:8443是服务端的地址,http://cas.client1.com:9002是客户端地址

 

  • CASAutoConfig配置类,配置需要忽略授权的url:
@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;

    /**
     * 授权过滤器
     * @return
     */
    @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", "/logout/success|/index");
        registration.setInitParameters(initParameters);
        // 设定加载的顺序
        registration.setOrder(1);
        return registration;
    }
}

 

  • CASController.java有两个业务如下:
@Controller
public class CASController {

    @RequestMapping("index")
    public String index(ModelMap map) {
        map.addAttribute("name", "clien B");
        return "index";
    }

    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }
}

 

  • 启动类加入开启cas client的注解
@EnableCasClient//启用cas client

四、启动项目、测试

依次启动CAS-server服务端、CAS-clientB客户端

访问http://cas.client1.com:9002/index进入CAS-clientB主页:

点击client B(http://cas.client1.com:9002/hello),会跳转到cas登录认证页面:

输入账号和密码完成登录后跳转到hello页面:

到此项目搭建完成

 

单点退出实现请看这里:CAS(五)CAS客户端单点退出实现

代码托管在https://gitee.com/oumuv/cas-Demo

(cas-clientA基于SpringMVC,cas-clientB、cas-clientC基于springboot)

 

上一篇:CAS(三)基于SpringMVC搭建CAS-client,SpringMVC搭建CAS客户端

下一篇:CAS(五)CAS客户端单点退出实现

 

希望可以帮助到有需要的人

 

发布了39 篇原创文章 · 获赞 54 · 访问量 5万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览