單點登錄-SpringBoot整合CAS

1.新建SpringBoot項目

在這裏插入圖片描述

2.導入項目中的依賴

<?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.bruceliiu.demo.cas</groupId>
    <artifactId>SpringBoot-Cas</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--導入SpringBoot的依賴-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <!--解決thymeleaf模板引擎語法過於嚴格報錯問題 -->
        <!--啓用不嚴格檢查html-->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <!--SpringBoot打包插件  jar  java -jar jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.在resources下新建application.properties(或者application.yml)

server.port=8080

#Thymeleaf配置
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
#設置爲LEGACYHTML5編碼格式
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

## CAS 配置
cas.validation-type=CAS
#配置的是cas服務器的前綴
cas.server-url-prefix=http://127.0.0.1:9999/cas
#這個配置的是 cas登陸的頁面
cas.server-login-url=http://127.0.0.1:9999/cas/login
#配置的是cas的退出功能的路徑
cas-server-logout-url=http://127.0.0.1:9999/cas/logout
#當前SpringBoot程序的主機和端口
cas.client-host-url=http://127.0.0.1:8080

4.在java下新建一個com.bruce包,在包下新建一個Application 類,代碼如下:

package com.bruce;

import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @BelongsProject: SpringBoot-Cas
 * @BelongsPackage: com.bruce
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-04-13 23:13
 * @Description: TODO
 */
@SpringBootApplication
@EnableCasClient
public class CasApp {

    public static void main(String[] args) {
        SpringApplication.run(CasApp.class,args);
    }
}

5.在新建一個測試類,代碼如下

package com.bruce.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @BelongsProject: SpringBoot-Cas
 * @BelongsPackage: com.bruce.controller
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-04-13 23:17
 * @Description: TODO
 */
@Controller
public class IndexController {

    @RequestMapping("/toIndex")
    public String toIndex(){
        System.out.println("控制器方法");
        return "index";
    }
}

6.整合測試

訪問控制器方法,會自動跳轉CAS服務器完成登錄驗證,登錄完畢後,自動跳轉回首頁
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章