一、什么是servers
cas的分为服务端和客户端,如果客户端要使用cas需要把自己的域名或ip注册到cas服务端才可以使用。默认的servers为静态的
src\main\resources\services\HTTPSandIMAPS-.json下边
命名规则名字-版本号.json
内容如下
{
"@class" : "org.apereo.cas.services.RegexRegisteredService", "serviceId" : "^(https|imaps|http)://app1.cas.com.*", "name" : "测试客户端", "id" : , "description" : "这是一个测试客户端的服务,所有的https访问都允许通过", "evaluationOrder" : 10000 }
静态的不方便维护,也不可能每增加一个客户端就编写一个json放到服务端下边,因此引入动态servers,动态servers存储在数据库中,本文采用mysql数据库+jpa
引入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> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-jdbc</artifactId> <version>${cas.version}</version> </dependency> <dependency> <groupId>org.apereo.cas</groupId> <artifactId>cas-server-support-jdbc-drivers</artifactId> <version>${cas.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency>
application配置
#数据库用户名 cas.serviceRegistry.jpa.user=cas-uac #数据库密码 cas.serviceRegistry.jpa.password=ehC5SNnpSnKccR4X #mysql驱动 cas.serviceRegistry.jpa.driverClass=com.mysql.jdbc.Driver #数据库连接 cas.serviceRegistry.jpa.url=jdbc:mysql://39.99.234.10:3306/cas-uac?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服务 cas.serviceRegistry.jpa.ddlAuto=update
controller
package com.itcode.cas.controller; import com.itcode.cas.DTO.ServiceDTO; import lombok.Data; import org.apache.commons.lang3.StringUtils; 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.hibernate.validator.constraints.NotBlank; 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.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.List; / * @author 夏天 * @email @.com * @date 2021年06月13日 14:14 */ @RestController public class ServiceController {
private Logger logger = LoggerFactory.getLogger(ServiceController.class); @Autowired @Qualifier("servicesManager") private ServicesManager servicesManager; / * 注册service * * @return */ @RequestMapping(value = "/addClient", method = RequestMethod.GET) public ReturnMessage addClient(ServiceDTO serviceDTO) {
RegisteredService registeredService = findByServiceId(serviceDTO.getServiceId()); if (registeredService != null) {
ReturnMessage returnMessage = new ReturnMessage(); returnMessage.setCode(500); returnMessage.setMessage("serviceId:" + serviceDTO.getServiceId() + " 已存在"); return returnMessage; } try {
RegexRegisteredService regexRegisteredService = covertService(serviceDTO); servicesManager.save(regexRegisteredService); //执行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 服务id * @return */ @ResponseBody @GetMapping(value = "/deleteClient") public ReturnMessage deleteClient( String serviceId) {
ReturnMessage returnMessage = new ReturnMessage(); RegisteredService registeredService = findByServiceId(serviceId); if (registeredService != null) {
try {
servicesManager.delete(registeredService); } catch (Exception e) {
//这里会报审计错误,直接进行捕获即可,不影响删除逻辑 logger.error(e.getMessage()); } if (null == findByServiceId(serviceId)) {
servicesManager.load(); returnMessage.setCode(200); returnMessage.setMessage("删除成功"); } else {
returnMessage.setCode(500); returnMessage.setMessage("删除失败" + serviceId); } } else {
returnMessage.setCode(500); returnMessage.setMessage("删除失败" + serviceId + " 不存在"); } return returnMessage; } @GetMapping("/all") public ReturnMessage getAllService() {
Collection<RegisteredService> allServices = servicesManager.getAllServices(); ReturnMessage returnMessage = new ReturnMessage(); returnMessage.setData(covertRegisteredServiceList(allServices)); return returnMessage; } private List<ServiceDTO> covertRegisteredServiceList(Collection<RegisteredService> registeredServices) {
if (CollectionUtils.isEmpty(registeredServices)) {
return null; } List<ServiceDTO> services = new ArrayList<>(); for (RegisteredService registeredService : registeredServices) {
services.add(covertRegisteredService(registeredService)); } return services; } private ServiceDTO covertRegisteredService(RegisteredService registeredService) {
ServiceDTO service = new ServiceDTO(); service.setServiceId(registeredService.getServiceId()); service.setDescription(registeredService.getDescription()); service.setEvaluationOrder(registeredService.getEvaluationOrder()); service.setId(registeredService.getId()); service.setName(registeredService.getName()); service.setTheme(registeredService.getTheme()); return service; } private RegexRegisteredService covertService(ServiceDTO service) throws Exception {
RegexRegisteredService regexRegisteredService = new RegexRegisteredService(); String serviceId = "^(https|imaps|http)://" + service.getServiceId() + ".*"; ReturnAllAttributeReleasePolicy returnAllAttributeReleasePolicy = new ReturnAllAttributeReleasePolicy(); regexRegisteredService.setServiceId(serviceId); regexRegisteredService.setId(service.getId()); regexRegisteredService.setDescription(service.getDescription()); regexRegisteredService.setEvaluationOrder(service.getEvaluationOrder()); if (StringUtils.isNotBlank(service.getTheme())) {
regexRegisteredService.setTheme(service.getTheme()); } regexRegisteredService.setAttributeReleasePolicy(returnAllAttributeReleasePolicy); regexRegisteredService.setName(service.getName()); regexRegisteredService.setLogoutUrl(new URL("http://" + service.getServiceId())); return regexRegisteredService; } private RegisteredService findByServiceId(String serviceId) {
RegisteredService service = null; serviceId = "http://" + serviceId; try {
service = servicesManager.findServiceBy(serviceId); } catch (Exception e) {
logger.error(e.getMessage()); } return service; } @Data public class ReturnMessage {
private Integer code; private String message; private List<?> data; } }
ServiceDTO
@Data public class ServiceDTO {
@NotBlank(message = "服务id不可以为空") private String serviceId; private String description; private Integer evaluationOrder; private Long id; private String name; private String theme; }
今天的文章
cas服务端动态servers分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/99625.html