Cas5.2之存储Service信息到数据库-yellowcong

刚刚开始的时候,我们存储service是通过json的方式,后来发现,这种方式不方便管理,每次需要添加后,重启服务器,这种做法不咋好,所以需要有一个有效的方式,通过将信息存储到数据库中,这样就就可以搞定这个问题了。1、在pom.xml中,添加cas-server-support-jpa-service-registry的依赖。2、配置application.properties ,添加数据库配置,3、启动服务器,验证。

客户端搭建

1、添加驱动到pom.xml


   <dependency>
    <groupId>mysqlgroupId>
    <artifactId>mysql-connector-javaartifactId>
    <version>5.1.21version>
dependency> 


<dependency>
    <groupId>org.apereo.casgroupId>
    <artifactId>cas-server-support-jdbcartifactId>
    <version>${cas.version}version>
dependency>

      
<dependency>
    <groupId>org.apereo.casgroupId>
    <artifactId>cas-server-support-jpa-service-registryartifactId>
    <version>${cas.version}version>
    <scope>systemscope>
  <optional>trueoptional>
  <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cas-server-support-jpa-service-registry-5.2.2.jarsystemPath>
dependency>

2、配置application.properties

cas.serviceRegistry.initFromJson=true 这一段表示从json文件里面初始化服务,如果我们配置了这个,就会将这写json里面的数据,都会自动导入到数据库中。

################################################
cas.serviceRegistry.watcherEnabled=true
cas.serviceRegistry.repeatInterval=120000
cas.serviceRegistry.startDelay=15000
#开启识别json文件,默认false
cas.serviceRegistry.initFromJson=true
#设置配置的服务,一直都有,不会给清除掉 , 第一次使用,需要配置为 create-drop
#create-drop 重启cas服务的时候,就会给干掉
#create  没有表就创建,有就不创建
#none 一直都有
#update 更新
cas.serviceRegistry.jpa.ddlAuto=update

#配置将service配置到数据库中
cas.authn.jdbc.query[0].healthQuery=SELECT 1
cas.serviceRegistry.jpa.isolateInternalQueries=false
cas.serviceRegistry.jpa.url=${cas.authn.jdbc.query[0].url}
cas.serviceRegistry.jpa.user=${cas.authn.jdbc.query[0].user}
cas.serviceRegistry.jpa.password=${cas.authn.jdbc.query[0].password}
#这个必须是org.hibernate.dialect.MySQL5Dialect ,我就是这个问题导致表创建失败
cas.serviceRegistry.jpa.dialect=org.hibernate.dialect.MySQL5Dialect
cas.serviceRegistry.jpa.driverClass=${cas.authn.jdbc.query[0].driverClass}
cas.serviceRegistry.jpa.leakThreshold=${cas.authn.jdbc.query[0].leakThreshold}
cas.serviceRegistry.jpa.batchSize=1
cas.serviceRegistry.jpa.failFast=true
# cas.serviceRegistry.jpa.defaultCatalog=
# cas.serviceRegistry.jpa.defaultSchema=

cas.serviceRegistry.jpa.autocommit=true
cas.serviceRegistry.jpa.idleTimeout=5000

# cas.serviceRegistry.jpa.pool.suspension=false
# cas.serviceRegistry.jpa.pool.minSize=6
# cas.serviceRegistry.jpa.pool.maxSize=18
# cas.serviceRegistry.jpa.pool.maxIdleTime=1000
# cas.serviceRegistry.jpa.pool.maxWait=2000

#配置结束
################################################

Cas5.2之存储Service信息到数据库-yellowcong_第1张图片

完整配置

##
# CAS Server Context Configuration
#
server.context-path=/
server.port=80

#SSL配置 开启https
server.ssl.enabled=false

cas.tgc.secure=false
cas.warningCookie.secure=false

#配置oauth2.0授权服务的信息
#配置授权,需要配置 server name的信息,不然就会跑到
cas.server.name=http://yellowcong.com:${server.port}
cas.server.prefix=${cas.server.name}

server.max-http-header-size=2097152
server.use-forward-headers=true
server.connection-timeout=20000
server.error.include-stacktrace=ALWAYS

server.compression.enabled=true
server.compression.mime-types=application/javascript,application/json,application/xml,text/html,text/xml,text/plain

server.tomcat.max-http-post-size=2097152
server.tomcat.basedir=build/tomcat
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%t %a "%r" %s (%D ms)
server.tomcat.accesslog.suffix=.log
server.tomcat.max-threads=10
server.tomcat.port-header=X-Forwarded-Port
server.tomcat.protocol-header=X-Forwarded-Proto
server.tomcat.protocol-header-https-value=https
server.tomcat.remote-ip-header=X-FORWARDED-FOR
server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

##
# CAS Cloud Bus Configuration
#
spring.cloud.bus.enabled=false
# spring.cloud.bus.refresh.enabled=true
# spring.cloud.bus.env.enabled=true
# spring.cloud.bus.destination=CasCloudBus
# spring.cloud.bus.ack.enabled=true

endpoints.enabled=false
endpoints.sensitive=true

endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

management.security.enabled=true
management.security.roles=ACTUATOR,ADMIN
management.security.sessions=if_required
management.context-path=/status
management.add-application-context-header=false

security.basic.authorize-mode=role
security.basic.enabled=false
security.basic.path=/cas/status/**

##
# CAS Web Application Session Configuration
#
server.session.timeout=300
server.session.cookie.http-only=true
server.session.tracking-modes=COOKIE

##
# CAS Thymeleaf View Configuration
#
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=true
spring.thymeleaf.mode=HTML
##
# CAS Log4j Configuration
#
# logging.config=file:/etc/cas/log4j2.xml
server.context-parameters.isLog4jAutoInitializationDisabled=true

##
# CAS AspectJ Configuration
#
spring.aop.auto=true
spring.aop.proxy-target-class=true


##
# CAS Authentication Credentials
#
#记住我
cas.ticket.tgt.rememberMe.enabled=true
cas.ticket.tgt.rememberMe.timeToKillInSeconds=3600

#退出后转发到对应的service
cas.logout.followServiceRedirects=true

#-----------------------------数据库认证--------------------------------
#SELECT * FROM cas_auth_user;
cas.authn.jdbc.query[0].sql=SELECT * FROM cas_auth_user WHERE user_name = ?   
#select * from cms_auth_user where user_name=?
cas.authn.jdbc.query[0].healthQuery=SELECT 1
cas.authn.jdbc.query[0].isolateInternalQueries=false
#cas.authn.jdbc.query[0].url=jdbc:mysql://192.168.156.43:3306/cas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/cas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].failFast=true
cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].leakThreshold=10
cas.authn.jdbc.query[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.jdbc.query[0].batchSize=1
cas.authn.jdbc.query[0].user=cas
#cas.authn.jdbc.query[0].ddlAuto=create-drop
cas.authn.jdbc.query[0].maxAgeDays=180
cas.authn.jdbc.query[0].password=cas
cas.authn.jdbc.query[0].autocommit=false
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].idleTimeout=5000
# cas.authn.jdbc.query[0].credentialCriteria=
# cas.authn.jdbc.query[0].name=
# cas.authn.jdbc.query[0].order=0
# cas.authn.jdbc.query[0].dataSourceName=
# cas.authn.jdbc.query[0].dataSourceProxy=false
#密码字段的信息
cas.authn.jdbc.query[0].fieldPassword=PASSWORD
#加密策略
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
#MD5加密策略
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5

#####################################
#密码管理开始
cas.authn.pm.enabled=true

#发送邮件
spring.mail.host=smtp.exmail.qq.com
spring.mail.port=465
#邮箱用户名(需要修改)
[email protected]
#邮箱授权码(需要修改)dd
spring.mail.password=yellowcongejk01
spring.mail.testConnection=false
spring.mail.properties.mail.smtp.auth=true

#必须ssl
spring.mail.properties.mail.smtp.ssl.enable=true

cas.authn.pm.reset.from=${spring.mail.username}
#发送邮件标题
cas.authn.pm.reset.subject=yellowcong mobbile 重置密码
#邮件内容,必须要有%s,因为会生成一个连接并且带了token,否则无法打开链接,当然这个链接也和cas.server.prefix有关系
cas.authn.pm.reset.text=打开以下链接重置您的密码(yellowcong mobbile): %s
#token失效分钟数
cas.authn.pm.reset.expirationMinutes=10
cas.authn.pm.reset.emailAttribute=mail

#新密码必须匹配表达式
cas.authn.pm.policyPattern=\\w{6,24}


#邮箱查找
#根据用户名查找邮箱
cas.authn.pm.jdbc.sqlFindEmail=SELECT email FROM cas_auth_user WHERE user_name = ?
#修改密码
cas.authn.pm.jdbc.sqlChangePassword=update cas_auth_user set password=? where user_name=?
cas.authn.pm.jdbc.url=${cas.authn.jdbc.query[0].url}
cas.authn.pm.jdbc.user=${cas.authn.jdbc.query[0].user}
cas.authn.pm.jdbc.password=${cas.authn.jdbc.query[0].password}
cas.authn.pm.jdbc.dialect=${cas.authn.jdbc.query[0].dialect}
cas.authn.pm.jdbc.driverClass=${cas.authn.jdbc.query[0].driverClass}
#密码修改加密规则,这个必须要和原始密码加密规则一致
cas.authn.pm.jdbc.passwordEncoder.type=${cas.authn.jdbc.query[0].passwordEncoder.type}
cas.authn.pm.jdbc.passwordEncoder.characterEncoding=${cas.authn.jdbc.query[0].passwordEncoder.characterEncoding}
cas.authn.pm.jdbc.passwordEncoder.encodingAlgorithm=${cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm}
cas.authn.pm.jdbc.passwordEncoder.secret=${cas.authn.jdbc.query[0].passwordEncoder.secret}
#默认是false,不会提交update语句
cas.authn.pm.jdbc.autocommit=true

#是否开启问题回答
cas.authn.pm.reset.securityQuestionsEnabled=false
#密码管理 结束

################################################
cas.serviceRegistry.watcherEnabled=true
cas.serviceRegistry.repeatInterval=120000
cas.serviceRegistry.startDelay=15000
#开启识别json文件,默认false
cas.serviceRegistry.initFromJson=true
#设置配置的服务,一直都有,不会给清除掉 , 第一次使用,需要配置为 create-drop
#create-drop 重启cas服务的时候,就会给干掉
#create  没有表就创建,有就不创建
#none 一直都有
#update 更新
cas.serviceRegistry.jpa.ddlAuto=update

#配置将service配置到数据库中
cas.authn.jdbc.query[0].healthQuery=SELECT 1
cas.serviceRegistry.jpa.isolateInternalQueries=false
cas.serviceRegistry.jpa.url=${cas.authn.jdbc.query[0].url}
cas.serviceRegistry.jpa.user=${cas.authn.jdbc.query[0].user}
cas.serviceRegistry.jpa.password=${cas.authn.jdbc.query[0].password}
#这个必须是org.hibernate.dialect.MySQL5Dialect ,我就是这个问题导致表创建失败
cas.serviceRegistry.jpa.dialect=org.hibernate.dialect.MySQL5Dialect
cas.serviceRegistry.jpa.driverClass=${cas.authn.jdbc.query[0].driverClass}
cas.serviceRegistry.jpa.leakThreshold=${cas.authn.jdbc.query[0].leakThreshold}
cas.serviceRegistry.jpa.batchSize=1
cas.serviceRegistry.jpa.failFast=true
# cas.serviceRegistry.jpa.defaultCatalog=
# cas.serviceRegistry.jpa.defaultSchema=

cas.serviceRegistry.jpa.autocommit=true
cas.serviceRegistry.jpa.idleTimeout=5000

# cas.serviceRegistry.jpa.pool.suspension=false
# cas.serviceRegistry.jpa.pool.minSize=6
# cas.serviceRegistry.jpa.pool.maxSize=18
# cas.serviceRegistry.jpa.pool.maxIdleTime=1000
# cas.serviceRegistry.jpa.pool.maxWait=2000

#配置结束
################################################

pom.xml完整依赖


<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.0modelVersion>
    <groupId>org.apereo.casgroupId>
    <artifactId>cas-serverartifactId>
    <packaging>warpackaging>
    <version>1.0version>

    <build>
        <plugins>
            <plugin>
                <groupId>com.rimerosolutions.maven.pluginsgroupId>
                <artifactId>wrapper-maven-pluginartifactId>
                <version>0.0.4version>
                <configuration>
                    <verifyDownload>trueverifyDownload>
                    <checksumAlgorithm>MD5checksumAlgorithm>
                configuration>
            plugin>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>${springboot.version}version>
                <configuration>
                    <mainClass>${mainClassName}mainClass>
                    <addResources>trueaddResources>
                    <executable>${isExecutable}executable>
                    <layout>WARlayout>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>2.6version>
                <configuration>
                    
                    <warName>ROOTwarName>
                    <failOnMissingWebXml>falsefailOnMissingWebXml>
                    <recompressZippedFiles>falserecompressZippedFiles>
                    <archive>
                        <compress>falsecompress>
                        <manifestFile>${manifestFileToUse}manifestFile>
                    archive>
                    <overlays>
                        <overlay>
                            <groupId>org.apereo.casgroupId>
                            <artifactId>cas-server-webapp${app.server}artifactId>
                        overlay>
                    overlays>
                    
                    <dependentWarExcludes>
                        **/services/*.json
                    dependentWarExcludes>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.3version>
            plugin>
        plugins>
        <finalName>casfinalName>
    build>

    <dependencies>
      <dependency>
            <groupId>org.apereo.casgroupId>
            <artifactId>cas-server-webapp${app.server}artifactId>
            <version>${cas.version}version>
            <type>wartype>
            <scope>systemscope>
            <optional>trueoptional>
            <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cas-server-webapp-tomcat-${cas.version}.warsystemPath>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.21version>
        dependency> 

        
        <dependency>
            <groupId>org.apereo.casgroupId>
            <artifactId>cas-server-support-jdbcartifactId>
            <version>${cas.version}version>
        dependency>

        
         <dependency>
             <groupId>org.apereo.casgroupId>
             <artifactId>cas-server-support-restartifactId>
             <version>${cas.version}version>
         dependency>

        
        <dependency>
          <groupId>org.apereo.casgroupId>
          <artifactId>cas-server-support-oauth-webflowartifactId>
          <version>${cas.version}version>
        dependency>
          
        <dependency>
            <groupId>org.apereo.casgroupId>
            <artifactId>cas-server-support-jpa-service-registryartifactId>
            <version>${cas.version}version>
            <scope>systemscope>
          <optional>trueoptional>
          <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cas-server-support-jpa-service-registry-5.2.2.jarsystemPath>
        dependency>

        
        <dependency>
          <groupId>org.apereo.casgroupId>
          <artifactId>cas-server-support-pm-jdbc-5.2.2artifactId>
          <version>${cas.version}version>
          <scope>systemscope>
          <optional>trueoptional>
          <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/cas-server-support-pm-jdbc-5.2.2.jarsystemPath>
        dependency> 
    dependencies>

    <properties>
        <cas.version>5.2.2cas.version>
        <springboot.version>1.5.8.RELEASEspringboot.version>
        
        <app.server>-tomcatapp.server> 

        <mainClassName>org.springframework.boot.loader.WarLaunchermainClassName>
        <isExecutable>falseisExecutable>
        <manifestFileToUse>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MFmanifestFileToUse>

        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <repositories>
        <repository>
          <id>aliyunmavenid>
          <url>http://maven.aliyun.com/nexus/content/groups/public/url>
        repository>

        <repository>
            <id>sonatype-releasesid>
            <url>http://oss.sonatype.org/content/repositories/releases/url>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
            <releases>
                <enabled>trueenabled>
            releases>
        repository>
        <repository>
            <id>sonatype-snapshotsid>
            <url>https://oss.sonatype.org/content/repositories/snapshots/url>
            <snapshots>
                <enabled>trueenabled>
            snapshots>
            <releases>
                <enabled>falseenabled>
            releases>
        repository>
        <repository>
            <id>shibboleth-releasesid>
            <url>https://build.shibboleth.net/nexus/content/repositories/releasesurl>
        repository>
    repositories>

    <profiles>
        <profile>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
            <id>execid>
            <properties>
                <mainClassName>org.apereo.cas.web.CasWebApplicationmainClassName>
                <isExecutable>trueisExecutable>
                <manifestFileToUse>manifestFileToUse>
            properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.soebes.maven.pluginsgroupId>
                        <artifactId>echo-maven-pluginartifactId>
                        <version>0.3.0version>
                        <executions>
                            <execution>
                                <phase>prepare-packagephase>
                                <goals>
                                    <goal>echogoal>
                                goals>
                            execution>
                        executions>
                        <configuration>
                            <echos>
                            <echo>Executable profile to make the generated CAS web application executable.echo>echos>
                        configuration>
                    plugin>
                plugins>
            build>
        profile>

        <profile>
            <activation>
                <activeByDefault>falseactiveByDefault>
            activation>
            <id>pgpid>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.github.s4u.pluginsgroupId>
                        <artifactId>pgpverify-maven-pluginartifactId>
                        <version>1.1.0version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>checkgoal>
                                goals>
                            execution>
                        executions>
                        <configuration>
                            <pgpKeyServer>hkp://pool.sks-keyservers.netpgpKeyServer>
                            <pgpKeysCachePath>${settings.localRepository}/pgpkeys-cachepgpKeysCachePath>
                            <scope>testscope>
                            <verifyPomFiles>trueverifyPomFiles>
                            <failNoSignature>falsefailNoSignature>
                        configuration>
                    plugin>
                plugins>
            build>
        profile>
    profiles>
project>

常见问题

1、Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘cas.regexregisteredservice’ doesn’t exist

Cas5.2之存储Service信息到数据库-yellowcong_第2张图片

我后来发现,我的这个我问题,是由于这个配置,导致的,我之前配置的是org.hibernate.dialect.MySQLDialect ,需要改成org.hibernate.dialect.MySQL5Dialect,才可以自动生成表数据。

#这个必须是org.hibernate.dialect.MySQL5Dialect ,我就是这个问题导致表创建失败
cas.serviceRegistry.jpa.dialect=org.hibernate.dialect.MySQL5Dialect

https://apereo.github.io/cas/5.2.x/installation/JDBC-Drivers.html
官网上的说明
Cas5.2之存储Service信息到数据库-yellowcong_第3张图片

2、重启服务 ,service丢失

导致这个问题的原因是,cas.serviceRegistry.jpa.ddlAuto的配置中,配置成了create-drop,重启服务后,就导致数据库配置的服务全部丢失了。

#设置配置的服务,一直都有,不会给清除掉 , 第一次使用,需要配置为 create-drop
#create-drop 重启cas服务的时候,就会给干掉
#create  没有表就创建,有就不创建
#none 一直都有
#update 更新
cas.serviceRegistry.jpa.ddlAuto=update

参考文章

https://apereo.github.io/cas/5.0.x/installation/Configuration-Properties.html#database-3
https://apereo.github.io/cas/5.0.x/installation/Configuration-Properties.html#Database%20Service%20Registry
https://groups.google.com/a/apereo.org/forum/#!topic/cas-user/k4LfFxB-Gqk
https://github.com/lansheng228/cas-management-jpa
https://apereo.github.io/cas/5.2.x/installation/Service-Management.html
https://oss.sonatype.org/content/repositories/releases/org/apereo/cas
https://groups.google.com/a/apereo.org/forum/#!topic/cas-user/g_HPCmx6xug
https://groups.google.com/a/apereo.org/forum/#!topic/cas-user/T2kPs69S2h0

你可能感兴趣的:(CAS单点登录,单点登录)