CAS5.3实战
使用CAS5实现单点登录,同时需要集成其他OAuth认证方式。对外提供OAuth2.0和OIDC服务,支持Client_ID及secret的动态管理,同时与业务系统进行关联。
文章目录
前言
CAS在收到Oauth请求后,如果没有登录,会将源url保存起来,然后只保留Oauth协议必须要求的参数,重定向到登录页面,类似:
login?service=OAUTH处理URL&client_name=CasOAuthClient的页面。
我这边的需求是要根据不同的用户类别显示不同的登录界面,调用第三方登录或采用自己的登录校验。处理方式一是使用不同的client_id和client_secrect,第二种是增加一个scope参数。然后修改源代码将其他参数也一并附在后面。
修改了源文件:OAuth20DefaultCasClientRedirectActionBuilder.java
/**
* Build with predefined renew and gateway parameters.
*
* @param casClient the cas client config
* @param context the context
* @param renew ask for credentials again
* @param gateway skip asking for credentials
* @return the redirect action
*/
protected RedirectAction build(final CasClient casClient, final WebContext context, final boolean renew, final boolean gateway) {
final String redirectionUrl = CommonUtils.constructRedirectUrl(casClient.getConfiguration().getLoginUrl(),
CasProtocolConstants.PARAMETER_SERVICE,
casClient.computeFinalCallbackUrl(context),
renew, gateway);
//定义附加url
String scope = "";
Map<String,String[]> map = context.getRequestParameters();
for(String key : map.keySet()){
//跳过3个固定参数
if("response_type".equals(key) || "client_id".equals(key) || "redirect_uri".equals(key))
continue;
scope +="%26"+key+"%3D"+map.get(key)[0];
}
if(scope != "")
LOGGER.warn("Final redirect url is [{}]",redirectionUrl+scope);
/* if(StringUtils.isNotBlank(context.getRequestParameter("scope")))
scope = "%26scope%3D"+context.getRequestParameter("scope");*/
LOGGER.debug("Final redirect url is [{}]", redirectionUrl);
return RedirectAction.redirect(redirectionUrl+scope);
}
2.对外提供不同的入口
在nginx中增加如下配置:
location /cas { proxy_pass http://10.38.45.159:8008/cas; } location /cas4a { set $args 'args&scope=4a'; proxy_pass http://10.38.45.159:8008/cas; }
这样,不同的用户可以暴露不同的地址:比如内部用户使用server.cas.com/cas访问,集团用户使用server.cas.co/cas4a访问。