preface

CAS Single Sign-on infrastructure

Next: CAS SSO – HTTPS Configuration

Configuration center

What is a configuration center

Distributed configuration center, convenient service configuration file unified management, it supports the configuration service in the configuration service memory (local), also supports the remote Git repository. In the Spring Cloud Config component, there are two roles: Config Server and Config client.

Key points:

The service uses the configuration property spring.application.name to determine the id of the service, which tells the configuration center who I am

Spring.profiles. Active this is which profile to take, so this way we can distinguish between multiple dimensions or environments, which can be multiple, comma separated

Access policy:

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties

If the value is:

spring.application.name=sso spring.profiles.active=dev

It looks for a configuration file named: sso-dev.properties, which could also be sso-dev.yml or something else

Setting up the Configuration Center

Initialization framework

Download using Spring Initializr

Add the SSO-config module to the master POM.xml

<modules> <module>.. /sso-server</module> <module>.. /sso-config</module> </modules>Copy the code

Modify pom.xml in sso-config

  1. Change spring-cloud-starter-config to spring-cloud-config-server
  2. Add a domestic warehouse agent
<? 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 projectforSpring Boot</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the 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> <! > <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>

Copy the code

SsoConfigApplication.java

Add @enableconFigServer to the Application class to enable the configuration of the server

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); }}Copy the code

application.yml

I changed application.properties to Application.yml to make it more intuitive

# specify log output file
logging:
  file: "logs/sso-config.log"
info:
  name : "Configuration Center"

---
server:
  # service port
  port: 8888
  Access path
  context-path: /config
spring:
  profiles:
    # Local configuration file
    active:
      Localize the configuration file
      - native
  application:
    # Specify the application name
    name: sso-config
Copy the code

Start the

mvn spring-boot:run
Copy the code

If the following information is displayed, the configuration is successful

The 2018-12-11 11:46:17. 10464-895 the INFO [Thread – 6] O.S.C.S upport. DefaultLifecycleProcessor: Stopping beans in phase 0 11:46:17 2018-12-11. 10464-904 the INFO/Thread – 6 O.S.J.E.A.A nnotationMBeanExporter: 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

Visit: http://localhost:8888/config

Success!

The CAS connection configuration center

  1. We mentioned above that the configuration is placed in the configuration center, not in the service
  2. Since the configuration is placed locally, spring’s default configuration is in the directoryresources/config

Create sso – dev. Properties

The files are placed under the resources/config directory

Sso-dev.properties means the CAS configuration should be:

spring.application.name=sso
spring.profiles.active=dev
Copy the code

Modify the sso-dev.properties configuration

Change the sso-dev.properties file to the configuration required by SSO-server (copy the application.properties content)

# #
# 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
Copy the code

Start the

Restart the sso – config

Visit: http://localhost:8888/config/sso/dev

sso-server

  1. Empty the application. The properties
  2. Add bootstrap.properties to directory /resources
# specify a log fileLogging.file =logs/cas.log info.name= single sign-on system# define the id of application.name
spring.application.name=sso
# Find the configuration center for sso-dev.properties
spring.profiles.active=dev
Configure center address
spring.cloud.config.uri=http://localhost:8888/config
# Enable configuration center
spring.cloud.config.enabled=true
Support automatic task to configuration center to refresh the configuration
spring.cloud.config.watch.enabled=true
Refresh every 30 seconds
spring.cloud.config.watch.initialDelay=30000
Request to configure the central supermarket
spring.cloud.config.watch.delay=1000
Check configuration health
health.config.enabled=true
Copy the code
  1. Adjust the log4j2. XML
<? xml version="1.0" encoding="UTF-8"? > <! -- Specify the refresh internalin 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 mode output console --> <AsyncLogger name="org.apereo.cas.config" level="debug" additivity="false">
            <AppenderRef ref="casConsole"/> </AsyncLogger> <! -- Multi-attribute Debug mode output console --> <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>

Copy the code
  1. Run the sso server. –
build.cmd run
Copy the code

Visit: http://localhost:8443/cas

User name/password: casuser/Mellon

This article refers to others blog learning arrangement, need to see the original blog or view the follow-up article can be click