基于Springboot搭建CAS-client,Springboot搭建CAS客户端-(四)-ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台
记录工作点滴
分享运维知识

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

二、配置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页面:

到此项目搭建完成

未经允许不得转载:ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台 » 基于Springboot搭建CAS-client,Springboot搭建CAS客户端-(四)

如果文章对你有帮助,欢迎点击上方按钮打赏作者

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

中国IT部落服务于运维技术人员交流分享平台

关于我们联系我们