- 启动类加@EnableCasClient
<!--cas客户端-->
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.7.0-GA</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-support-springboot</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
//配置文件加入以下配置
cas:
server-url-prefix: https://testsso.com/cas #cas前缀
server-login-url: https://testsso.com/cas/login #cas登录地址
client-host-url: http://localhost:8800 #业务系统地址
//Ticket校验器 支持三种 CAS, CAS3, SAML。 cas,只能返回当前用户名。cas3,返回attributes信息。
validation-type: CAS3
authentication-url-patterns: /casAuth #设置过滤的url地址 认证url,默认*
//是否启用单点登出,默认true
single-logout.enabled
//校验url,默认 /*
validation-url-patterns
# 对url 进行包装,之后就可在request中获取到用户信息,默认*
request-wrapper-url-patterns
public Response backLogin(HttpServletRequest request){
//获取cas信息:
Object constCasAssertion = request.getSession().getAttribute("_const_cas_assertion_");
Assertion assertion=(Assertion) constCasAssertion;
//获取公开唯一标识属性
String casName = assertion.getPrincipal().getName();
//获取公开的Attributes部分
Map<String, Object> attributes = assertion.getPrincipal().getAttributes();
}
2.配置casConfig
@Configuration
public class CasConfig{
@Value("${cas.casServerUrlPrefix:https://sso.com/cas}")
private String casServerUrlPrefix;
@Value("${cas.casServerUrlPrefix:https://sso.com/cas/login}")
private String casServerLoginUrl;
@Value("${cas.serverName:http://xxx:8800}")
private String serverName;
/**
*将未携带票据与会话中无票据的请求都重定向到CAS登录地址
*/
@Bean
@Order(1)
public FilterRegistrationBean<AuthenticationFilter> casAuthenticationFilter() {
FilterRegistrationBean<AuthenticationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new AuthenticationFilter());
registration.setName("CAS Authentication Filter");
Map<String, String> initParams = new HashMap<>();
initParams.put("casServerUrlPrefix", casServerUrlPrefix); // CAS服务端地址
initParams.put("casServerLoginUrl", casServerLoginUrl); // CAS登录地址
initParams.put("serverName", serverName); // 本地地址
registration.setInitParameters(initParams);
registration.addUrlPatterns("/dmeeting/casAuth");
return registration;
}
/**
* 向CAS服务端发起校验票据请求
*/
@Bean
@Order(2)
public FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter> cas30TicketValidationFilter() {
FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
registration.setName("CAS30 Ticket Validation Filter");
Map<String, String> initParams = new HashMap<>();
initParams.put("casServerUrlPrefix", casServerUrlPrefix); // CAS服务端地址,会拼接为服务校验地址
initParams.put("serverName", serverName);
registration.setInitParameters(initParams);
registration.addUrlPatterns("/*");
return registration;
}
/**
*CAS登录成功的信息存入请求中
*/
@Bean
@Order(3)
public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter() {
FilterRegistrationBean<HttpServletRequestWrapperFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(new HttpServletRequestWrapperFilter());
registration.setName("HttpServletRequest Wrapper Filter");
registration.addUrlPatterns("/*");
// 设定加载的顺序
//registration.setOrder(3);
return registration;
}
}
3.cas的restFul协议访问
cas支持rest协议方式进行访问,格式和参数如下:
1. 获取TGT
请求方式,路径,http协议及请求参数:
POST /cas/v1/tickets HTTP/1.0
username=xxx&password=pwd&additionalParam1=paramvalue
请求响应
201 Created
Location: http://xxx.com/cas/v1/tickets/{TGT id}
2. 获取ST //service必须编码,否则会报错:票根XXX 不符合目标服务
请求方式,路径,http协议及请求参数:
POST /cas/v1/tickets/{TGT id} HTTP/1.0
service={form encoded parameter for the service url}
请求响应
200 OK
ST-1-FFDFHDSJKHSDFJKSDHFJKRUEYREWUIFSD2132
3. 校验ST
请求方式及请求路径
GET /cas/p3/serviceValidate?service={service url}&ticket={service ticket}
请求响应
状态码,200成功;200请求失败;415不支持的媒体类型;502网络代理资源问题
4. 登出
请求方式,请求路径和http协议
DELETE /cas/v1/tickets/{TGT} HTTP/1.0
请求响应
返回注销的TGT
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/260344.html