CAS Server and Client - https

CAS Server and Client

cas server

cas-overlay-template

从 https://github.com/apereo/cas-overlay-template/tree/5.2 下载 cas-overlay-template 包,这里选择5.2版本。

配置与打包

build copy

修改build.cmd文件,将其中 cas.example.org 改为 localhost。

build gencert
build package

修改 cas.war 文件中的 WEB-INF/classes/application.properties 文件,添加

cas.tgc.secure=false
cas.serviceRegistry.initFromJson=true

修改 cas.war 文件中的 WEB-INF/classes/services/HTTPSandIMAPS-10000001.json 文件

{
    
    
  "@class" : "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "^(http|https|imaps)://.*",
  "name" : "HTTPS and IMAPS",
  "id" : 10000001,
  "description" : "This service definition authorizes all application urls that support HTTPS and IMAPS protocols.",
  "evaluationOrder" : 10000
}

启动 cas server

build run

访问 https://localhost:8443/cas,使用 casuser/Mellon 登录 CAS。

cas client

  1. 创建一个spring boot工程。

  2. 添加 Cas 配置类 CasConfigure.java,内容如下:

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Configuration;

import net.unicon.cas.client.configuration.CasClientConfigurerAdapter;
import net.unicon.cas.client.configuration.EnableCasClient;

@Configuration
@EnableCasClient
public class CasConfigure extends CasClientConfigurerAdapter {
    
    
	@Override
	public void configureAuthenticationFilter(FilterRegistrationBean authenticationFilter) {
    
    
		super.configureAuthenticationFilter(authenticationFilter);
		authenticationFilter.getInitParameters().put("authenticationRedirectStrategyClass",
				"com.patterncat.CustomAuthRedirectStrategy");
	}
}
  1. 添加一个Controller类TestController.java,内容如下:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@RestController
public class TestController {
    
    
	
	@GetMapping("/auth/test")
	public Object test() {
    
    
		ObjectMapper mapper = new ObjectMapper();
		ObjectNode jsonObj = mapper.createObjectNode();
		jsonObj.put("message", "hello world");
		return jsonObj;
	}

}
  1. 添加配置文件 application.properties,内容如下
server.port=9090
cas.server-url-prefix: https://localhost:8443/cas/
cas.server-login-url: https://localhost:8443/cas/login
cas.client-host-url: http://localhost:9090
cas.validation-type: cas
  1. 导入证书
keytool -import -alias cas -file \etc\cas\cas.cer -keypass changeit -keystore "C:\Program Files\Java\jdk1.8.0_211\jre\lib\security\cacerts"
  1. 运行工程
mvn spring-boot:run

访问 http://localhost:9090/myapp/test, 页面会被转到 cas 登录页,登录会跳回到 test 页面。

FAQ

如果测试过程中出现

未认证授权的服务
CAS的服务记录是空的,没有定义服务。 希望通过CAS进行认证的应用程序必须在服务记录中明确定义。
未认证授权的服务
不允许使用CAS来认证您访问的目标应用。

那应该是 “\WEB-INF\classes\services\HTTPSandIMAPS-10000001.json” 和 “\WEB-INF\classes\application.properties” 文件没改导致的。

猜你喜欢

转载自blog.csdn.net/kongxx/article/details/123307326