美文网首页
client+springboot客户端整合

client+springboot客户端整合

作者: 你家门口的两朵云 | 来源:发表于2021-07-05 10:35 被阅读0次

    1.新建maven父子工程项目,

    父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>edu.hgnu</groupId>
        <artifactId>cas_demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
            <cas.version>2.3.0-GA</cas.version>
            <spring-boot.version>2.3.4.RELEASE</spring-boot.version>
        </properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>net.unicon.cas</groupId>
                    <artifactId>cas-client-autoconfig-support</artifactId>
                    <version>${cas.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    </project>
    

    新建3个实验用的springboot项目,黄师贴吧,黄师问答,黄师二手,三个module服务端;
    第一个子项目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">
        <parent>
            <groupId>edu.hgnu</groupId>
            <artifactId>cas_demo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>hgnu-tieba</artifactId>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <!--添加web依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--thymeleaf模板引擎-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <!--jpa-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <!--tomcat-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
            <!--cas客户端依赖-->
            <dependency>
                <groupId>net.unicon.cas</groupId>
                <artifactId>cas-client-autoconfig-support</artifactId>
            </dependency>
            <!--cas依赖-->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-cas</artifactId>
            </dependency>
            <!--taglibs依赖-->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-taglibs</artifactId>
            </dependency>
        </dependencies>
    </project>
    

    其余两个应用和上述差不多,换个端口号就行;
    确保三个sringboot服务可以正常运行

    第一个服务的目录结构(其他两个雷同):


    校园贴吧目录结构

    templates/index.html模板

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>黄师校园贴吧</title>
    </head>
    <body>
    <h1>黄师校园贴吧</h1>
    </body>
    </html>
    

    application.yml配置文件

    server:
     port: 8888
    
    cas:
     server-url-prefix: https://hgnu.edu:8443/cas
     server-login-url: https://hgnu.edu:8443/cas/login
     #client-host-url: https://hgnu.edu:8888  如果springboot项目是https就用这个
     client-host-url: http://hgnu.edu:8888
     validation-type: cas3
    

    在启动类开启CAS @EnableCasClient

    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    @EnableCasClient
    public class TiebaApp {
        public static void main(String[] args) {
            SpringApplication.run(TiebaApp.class,args);
        }
    }
    

    2.尝试启动App,并尝试在此访问三个App

    访问被拦截,提示未认证授权的服务


    错误页面

    原因:cas默认是https协议,而springboot应用没有配置https

    3.springboot项目默认支持http协议,不支持https

    有两种解决方案:
    1.将springboot添加ssl证书,让项目支持https访问;
    2.配置cas授权认证,

    此处贴出第二种解决方案,(第一种自行百度,有证书就行了)
    第一步修改tomcat-9.0\webapps\cas\WEB-INF\classes\services\目录下
    HTTPSandIMAPS-xxxxx.json的json文件(名字可能不完全相同)

    "serviceId" : "^(https|imaps|http)://."
    改为:
    "serviceId" : "^(https|imaps|http)://.
    "

    第二步,修改配置文件tomcat-9.0\webapps\cas\WEB-INF\classes\application.properties

    #新增支持http页面认证
    cas.tgc.secure=false
    cas.serviceRegistry.initFromJson=true
    
    

    4.启动项目

    坑1: 如果报错,java.security.cert.CertificateException: No name matching hgnu.edu found;
    nested exception is javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching hgnu.edu found
      原因:在调用hgnu.edu的时候,我们使用的是https的方式,正常情况下应该是使用hgnu.edu的证书,但由于某些原因,我们只能使用自己的证书,导致在验证证书的时候,就报了这个错误。

    所以在生成hgnu.keystore证书的时候应该让cn=hgnu.edu,否则就算证书加入jdk证书库也会匹配不到域名;

    正常启动项目,一处登录,处处通行

    cas认证中心 服务1-黄师二手 服务2-黄师校园贴吧 服务3-黄师校园问答

    相关文章

      网友评论

          本文标题:client+springboot客户端整合

          本文链接:https://www.haomeiwen.com/subject/incvultx.html