上一篇:CAS單點登陸-基礎搭建java
下一篇:CAS單點登陸-https配置web
分佈式配置中心,方便服務配置文件統一管理,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是config server,二是config client。spring
重點:apache
服務是以spring.application.name的配置屬性來決定這個服務的id,也就是告訴配置中心,我是誰bootstrap
spring.profiles.active這是要拿哪一個配置文件,那麼這樣咱們就能夠區分多個維度或者說環境,這裏能夠是多個,能夠逗號分隔tomcat
訪問策略:bash
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties服務器
若配置爲:cookie
spring.application.name=sso spring.profiles.active=devsession
會尋找配置文件名爲:sso-dev.properties文件,固然也能夠爲sso-dev.yml或其餘
<modules>
<module>../sso-server</module>
<module>../sso-config</module>
</modules>
複製代碼
<?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.cxy.auth</groupId>
<artifactId>sso-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sso-config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!--爲了加快速度,修改爲國內的代理-->
<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<id>maven2-release</id>
<url>http://uk.maven.org/maven2/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
<!--快照版本庫兩個小時檢查更新一遍-->
<updatePolicy>interval:120</updatePolicy>
</snapshots>
<id>oss-snapshots</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled>true</enabled>
</releases>
<id>maven2-release</id>
<url>http://uk.maven.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
</project>
複製代碼
在程序的入口Application類加上@EnableConfigServer註解開啓配置服務器的功能
package com.cxy.auth.ssoconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SsoConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SsoConfigApplication.class, args);
}
}
複製代碼
我這裏將application.properties
改爲了application.yml
,比較直觀
#指定日誌輸出文件
logging:
file: "logs/sso-config.log"
info:
name : "配置中心"
---
server:
#服務端口
port: 8888
#訪問路徑
context-path: /config
spring:
profiles:
#本地配置文件
active:
#配置文件本地化
- native
application:
#指定應用名稱
name: sso-config
複製代碼
mvn spring-boot:run
複製代碼
看到以下結果,配置成功
2018-12-11 11:46:17.895 INFO 10464 --- [ Thread-6] o.s.c.support.DefaultLifecycleProcessor : Stopping beans in phase 0 2018-12-11 11:46:17.904 INFO 10464 --- [ Thread-6] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown 2018-12-11 11:46:17.907 INFO 10464 --- [ Thread-6] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans
訪問:http://localhost:8888/config
成功!
resources/config
文件置於resources/config
目錄下
sso-dev.properties
意味着cas的配置應該爲:
spring.application.name=sso
spring.profiles.active=dev
複製代碼
把sso-dev.properties文件修改爲sso-server須要的配置(把application.properties內容拷貝過來便可)
##
# CAS Server Context Configuration
#
server.context-path=/cas
server.port=8443
server.ssl.enabled=false
server.max-http-header-size=2097152
server.use-forward-headers=true
server.connection-timeout=20000
server.error.include-stacktrace=NEVER
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
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.authn.accept.users=casuser::Mellon
複製代碼
重啓sso-config
訪問:http://localhost:8888/config/sso/dev
#指定日誌文件
logging.file=logs/cas.log
info.name=單點登陸系統
#定義application.name的id
spring.application.name=sso
#尋找配置中心爲sso-dev.properties
spring.profiles.active=dev
#指定配置中心地址
spring.cloud.config.uri=http://localhost:8888/config
#開啓配置中心
spring.cloud.config.enabled=true
#支持自動任務去配置中心刷新配置
spring.cloud.config.watch.enabled=true
#30秒刷新一次
spring.cloud.config.watch.initialDelay=30000
#請求配置中心超市
spring.cloud.config.watch.delay=1000
#檢查配置健康
health.config.enabled=true
複製代碼
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Specify the refresh internal in seconds. -->
<Configuration monitorInterval="5" packages="org.apereo.cas.logging">
<Properties>
<Property name="baseDir">logs</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p [%c] - <%m>%n"/>
</Console>
<RollingFile name="file" fileName="${baseDir}/sso/cas.log" append="true"
filePattern="${baseDir}/cas-%d{yyyy-MM-dd-HH}-%i.log">
<PatternLayout pattern="%highlight{%d %p [%c] - <%m>}%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB"/>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="5" compressionLevel="9">
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="auditlogfile" fileName="${baseDir}/cas_audit.log" append="true"
filePattern="${baseDir}/cas_audit-%d{yyyy-MM-dd-HH}-%i.log">
<PatternLayout pattern="%d %p [%c] - %m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB"/>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="5" compressionLevel="9">
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="perfFileAppender" fileName="${baseDir}/perfStats.log" append="true"
filePattern="${baseDir}/perfStats-%d{yyyy-MM-dd-HH}-%i.log">
<PatternLayout pattern="%m%n"/>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB"/>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="5" compressionLevel="9">
<Delete basePath="${baseDir}" maxDepth="2">
<IfFileName glob="*/*.log.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<CasAppender name="casAudit">
<AppenderRef ref="auditlogfile" />
</CasAppender>
<CasAppender name="casFile">
<AppenderRef ref="file" />
</CasAppender>
<CasAppender name="casConsole">
<AppenderRef ref="console" />
</CasAppender>
<CasAppender name="casPerf">
<AppenderRef ref="perfFileAppender" />
</CasAppender>
</Appenders>
<Loggers>
<AsyncLogger name="com.couchbase" level="warn" additivity="false" includeLocation="true">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apereo.cas.web.CasWebApplication" level="info" additivity="false" includeLocation="true">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.security" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.jdbc" level="debug" additivity="false">
<AppenderRef ref="casConsole"/>
</AsyncLogger>
<AsyncLogger name="org.apereo.cas.logout" level="debug" additivity="false">
<AppenderRef ref="casConsole"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.boot.autoconfigure.security" level="info" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.jasig.cas.client" level="info" additivity="false" includeLocation="true">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apereo" level="info" additivity="false" includeLocation="true">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apereo.services.persondir" level="warn" additivity="false" includeLocation="true">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apache" level="error" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.cloud" level="info" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<!--配置信息debug模式輸出控制檯-->
<AsyncLogger name="org.apereo.cas.config" level="debug" additivity="false">
<AppenderRef ref="casConsole"/>
</AsyncLogger>
<!--多屬性debug模式輸出控制檯-->
<AsyncLogger name="org.apereo.services.persondir" level="debug" additivity="false">
<AppenderRef ref="casConsole"/>
</AsyncLogger>
<AsyncLogger name="org.apereo.cas.authentication" level="debug" additivity="false">
<AppenderRef ref="casConsole"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.cloud.context" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.boot" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.aop" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.boot.actuate.autoconfigure" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.webflow" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.session" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.amqp" level="off" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.integration" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.messaging" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.web" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.orm.jpa" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.scheduling" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.thymeleaf" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.pac4j" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.opensaml" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="net.sf.ehcache" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="com.ryantenney.metrics" level="warn" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</AsyncLogger>
<AsyncLogger name="net.jradius" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.openid4java" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.ldaptive" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="com.hazelcast" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.context.annotation" level="off" additivity="false" />
<AsyncLogger name="org.springframework.boot.devtools" level="off" additivity="false" />
<AsyncLogger name="org.jasig.spring" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.springframework.web.socket" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apache.cxf" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apache.http" level="warn" additivity="false">
<AppenderRef ref="casConsole"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="perfStatsLogger" level="info" additivity="false" includeLocation="true">
<AppenderRef ref="casPerf"/>
</AsyncLogger>
<AsyncLogger name="org.apereo.cas.web.flow" level="info" additivity="true" includeLocation="true">
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncLogger name="org.apereo.inspektr.audit.support" level="info" includeLocation="true">
<AppenderRef ref="casAudit"/>
<AppenderRef ref="casFile"/>
</AsyncLogger>
<AsyncRoot level="error">
<AppenderRef ref="casConsole"/>
</AsyncRoot>
</Loggers>
</Configuration>
複製代碼
build.cmd run
複製代碼
用戶名/密碼:casuser/Mellon
本文參考他人博客學習整理,須要看原博客的或者查看後續文章的能夠點擊