CAS单点登录-动态添加services(七)
前面我们整合客户端的时候,需要在cas服务端注册,使用的是json文件的方式,直接通过配置文件完成配置,但是也存在一定的不方便性。
假如,我们以域名配置的,比如:http://app1.cas.com 注册,那么又有新的模块为 http://app2.cas.com 我们总不能每次修改配置,重启cas服务吧。这很不现实,官网给出了如下的解决方式,将数据库来存储这些数据。
具体参考官网
https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties.html#database-service-registry
https://apereo.github.io/cas/5.3.x/installation/JPA-Service-Management.html
https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties-Common.html#database-settings
pom添加依赖
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-support-jpa-service-registry</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-services-api</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-core-authentication-attributes</artifactId>
<version>${cas.version}</version>
</dependency>
application.properties添加以下属性
##
# 动态service 注册配置
#
#数据库用户名
cas.serviceRegistry.jpa.user=root
#数据库密码
cas.serviceRegistry.jpa.password=123456
#mysql驱动
cas.serviceRegistry.jpa.driverClass=com.mysql.jdbc.Driver
#数据库连接
cas.serviceRegistry.jpa.url=jdbc:mysql://127.0.0.1:3306/sso?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.serviceRegistry.jpa.dialect=org.hibernate.dialect.MySQL5Dialect
#连接池配置
cas.serviceRegistry.jpa.pool.suspension=false
cas.serviceRegistry.jpa.pool.minSize=6
cas.serviceRegistry.jpa.pool.maxSize=18
cas.serviceRegistry.jpa.pool.maxWait=2000
cas.serviceRegistry.jpa.pool.timeoutMillis=1000
#默认为create-drop,表示每次启动服务都会清除你之前注册的cas服务,生产环境生成表结构后需要修改配置为update
cas.serviceRegistry.jpa.ddlAuto=update
停止服务,将会删除之前创建的service
为了避免重启服务,导致之前的services丢失,需要将
cas.serviceRegistry.jpa.ddlAuto=update
每次启动之后,会在mysql中自动生成以下表格
增加http接口,操作数据库添加或删除service
package com.thtf.cas.controller;
import org.apereo.cas.services.RegexRegisteredService;
import org.apereo.cas.services.RegisteredService;
import org.apereo.cas.services.ReturnAllAttributeReleasePolicy;
import org.apereo.cas.services.ServicesManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.net.URL;
/**
* ========================
* Created with IntelliJ IDEA.
* User:pyy
* Date:2019/6/27
* Time:14:56
* Version: v1.0
* ========================
*/
@RestController
public class ServiceController {
private Logger logger = LoggerFactory.getLogger(ServiceController.class);
@Autowired
@Qualifier("servicesManager")
private ServicesManager servicesManager;
/**
* 注册service
* @param serviceId 域名
* @param id 顺序
* @return
*/
@GetMapping("/addClient/{serviceId}/{id}")
public Object addClient(@PathVariable("serviceId") String serviceId, @PathVariable("id") int id) {
try {
String a="^(https|imaps|http)://"+serviceId+".*";
RegexRegisteredService service = new RegexRegisteredService();
ReturnAllAttributeReleasePolicy re = new ReturnAllAttributeReleasePolicy();
service.setServiceId(a);
service.setId(id);
service.setAttributeReleasePolicy(re);
service.setName("login");
//这个是为了单点登出而作用的
service.setLogoutUrl(new URL("http://"+serviceId));
servicesManager.save(service);
//执行load让他生效
servicesManager.load();
ReturnMessage returnMessage = new ReturnMessage();
returnMessage.setCode(200);
returnMessage.setMessage("添加成功");
return returnMessage;
} catch (Exception e) {
logger.error("注册service异常",e);
ReturnMessage returnMessage = new ReturnMessage();
returnMessage.setCode(500);
returnMessage.setMessage("添加失败");
return returnMessage;
}
}
/**
* 删除service异常
* @param serviceId
* @return
*/
@GetMapping("/deleteClient/{serviceId}/{id}")
public Object deleteClient(@PathVariable("serviceId") String serviceId,@PathVariable("id") int id) {
try {
// String a="^(https|imaps|http)://"+serviceId+".*";
// String a="^(https|imaps|http)://"+serviceId+".*";
// RegexRegisteredService service = new RegexRegisteredService();
// ReturnAllAttributeReleasePolicy re = new ReturnAllAttributeReleasePolicy();
// service.setServiceId(a);
// service.setId(id);
// service.setAttributeReleasePolicy(re);
// service.setName("login");
// //这个是为了单点登出而作用的
// service.setLogoutUrl(new URL("http://"+serviceId));
String aa = "http://app2.cas.com:8082";
RegisteredService service = servicesManager.findServiceBy(aa);
servicesManager.delete(service);
//执行load生效
servicesManager.load();
ReturnMessage returnMessage = new ReturnMessage();
returnMessage.setCode(200);
returnMessage.setMessage("删除成功");
return returnMessage;
} catch (Exception e) {
logger.error("删除service异常",e);
ReturnMessage returnMessage = new ReturnMessage();
returnMessage.setCode(500);
returnMessage.setMessage("删除失败");
return returnMessage;
}
}
public class ReturnMessage{
private Integer code;
private String message;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}
配置集成SwaggerAPI集成
- 第一种方式:
引入依赖包即可:
<dependency>
<groupId>org.apereo.cas</groupId>
<artifactId>cas-server-documentation-swagger</artifactId>
<version>${cas.version}</version>
</dependency>
cas已经配置好swagger,启动即可使用
- 第二种方式:
手动配置:
引入依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
编写swagger配置类:
package com.thtf.cas.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration("casSwaggerConfiguration")
@EnableSwagger2
public class CasSwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder()
.title("CAS Swagger API Documentation")
.license("Apache v2")
.licenseUrl("https://github.com/apereo/cas/blob/master/LICENSE")
.description("CAS Swagger API Documentation")
.version("1.0.0")
.build());
}
}
添加ServiceController类和Swagger配置类到:META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.thtf.cas.config.CustomAuthenticationConfiguration,\
com.thtf.cas.config.CasSwaggerConfiguration,\
com.thtf.cas.controller.ServiceController
注意:
如果不添加,这个两个类不会被系统识别
以下Swagger端点可用于分析和测试API:
描述 | 网址 |
---|---|
Swagger API规范 | http://localhost/cas/v2/api-docs |
Swagger UI | http://localhost/cas/swagger-ui.html |
启动
访问:http://localhost:8443/cas/swagger-ui.html
测试
-
此时我们就可以通过接口完成service的添加,删除等操作
-
查看数据库表
总结
此中方式可以更加方便管理service,而不用每次有新的应用接入时重启cas-server服务器。
而且后面我们可以通过操作regexregisterdservice
表 完成对service的管理