当前位置:网站首页>CAS客户端对接
CAS客户端对接
2022-08-10 17:13:00 【被秃头支配的恐惧】
**需求:**输入项目地址(例:127.0.0.1:8080)时,判断有没有登录CAS,没有则跳转CAS登录,登录完成再返回项目地址,且需要获取到CAS登录用户的用户名。
实现:
第一步:搭建好CAS服务,这个在上一篇博客写了如何搭建,传送门CAS服务部署以及配置登陆成功跳转地址
第二步:接入客户端可以常用第三方的库cas-client-autoconfig-support来对接,比较快捷,迅速实现,或者可以用cas-client-support-springboot集成到boot项目
首先pom文件中添加依赖
<!-- CAS依赖包 -->
<dependency>
<groupId>net.unicon.cas</groupId>
<artifactId>cas-client-autoconfig-support</artifactId>
<version>1.5.0-GA</version>
</dependency>
然后在application.properties中添加配置
#cas服务端的登录地址
cas.server-login-url=http://127.0.0.1:8070/login
#cas服务端的地址
cas.server-url-prefix: http://127.0.0.1:8070
#当前服务器的地址(客户端)
cas.client-host-url: http://127.0.0.1:8801
然后自定义一个重定向策略类,这里还是和默认的策略一样,可以根据项目需要自行更改
import org.jasig.cas.client.authentication.AuthenticationRedirectStrategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * @ClassName: CustomAuthticationRedirectStrategy * @Description: * @author: zhang zihao * @date: 2022/8/6 11:05 */
public class CustomAuthticationRedirectStrategy implements AuthenticationRedirectStrategy {
@Override
public void redirect(HttpServletRequest request, HttpServletResponse response, String potentialRedirectUrl) throws IOException {
// response.setCharacterEncoding("utf-8");
// response.setContentType("application/json; charset=utf-8");
// PrintWriter out = response.getWriter();
// out.write("401");
//response重定向
response.sendRedirect(potentialRedirectUrl);
}
}
最后编写拦截器,其中@EnableCasClient注解一定要加,开启CAS支持,这里我的CAS登录地址和客户端地址(当前项目地址)我都写在application.properties配置文件里面
import ktw.micro.service.proxy.center.feign.AuthFeign;
import net.unicon.cas.client.configuration.CasClientConfigurerAdapter;
import net.unicon.cas.client.configuration.EnableCasClient;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/** * @ClassName: CASConfig * @Description: * @author: zhang zihao * @date: 2022/8/6 11:06 */
@Configuration
@EnableCasClient
public class CASConfig extends CasClientConfigurerAdapter {
@Value("${cas.server-login-url}")
private String CAS_SERVER_URL_LOGIN;
@Value("${cas.client-host-url}")
private String SERVER_NAME;
private static final String AUTHENTICATION_REDIRECT_STRATEGY_CLASS = "org.muses.jeeplatform.oa.cas.CustomAuthticationRedirectStrategy";
@Override
public void configureAuthenticationFilter(FilterRegistrationBean authenticationFilter) {
super.configureAuthenticationFilter(authenticationFilter);
authenticationFilter.getInitParameters().put("authenticationRedirectStrategyClass",AUTHENTICATION_REDIRECT_STRATEGY_CLASS);
}
@Override
public void configureValidationFilter(FilterRegistrationBean validationFilter) {
Map<String, String> initParameters = validationFilter.getInitParameters();
initParameters.put("encodeServiceUrl", "false");
}
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new AuthenticationFilter());
registrationBean.addUrlPatterns("/*");
Map<String, String> initParameters = new HashMap<String,String>(4);
initParameters.put("casServerLoginUrl",CAS_SERVER_URL_LOGIN);
initParameters.put("serverName",SERVER_NAME);
initParameters.put("ignorePattern","/logoutSuccess/*");
// 自定义重定向策略
initParameters.put("authenticationRedirectStrategyClass", AUTHENTICATION_REDIRECT_STRATEGY_CLASS);
registrationBean.setInitParameters(initParameters);
registrationBean.setOrder(1);
return registrationBean;
}
}
第三步:上述步骤完成后,启动项目,请求项目接口,我这里使用的是以下这个接口(127.0.0.1:8080/proxy),CAS未登录的情况下,首先跳到CAS登录界面,登录完成后重定向到127.0.0.1:8080/proxy
@RestController
public class ProxyController {
@Resource
private ProxyService proxyService;
/** * @param servletResponse * @param url 去往系统的地址 * @description * @return void * @author zhang zihao * @date 2022/8/3 * http://host:port/proxy?url=xxxx **/
@GetMapping("/proxy")
public void proxy(HttpServletRequest request, HttpServletResponse servletResponse, String url) {
proxyService.proxy(request,servletResponse,url);
}
}
第四步:客户端如何获取到CAS登录用户,CAS5.3会在配置文件里面配置一个默认的用户casuser,以下代码就是获取到这个用户。
Principal principal=request.getUserPrincipal();
String name=principal.getName();
边栏推荐
猜你喜欢

从宠爱到嫌弃,蒸发1500亿后,这家明星企业为何陨落?

Pytorch GPU模型推理时间探讨

v-for指令:根据数据生成列表结构

雷达人体存在感应器,人体感知控制应用,为客户提供真实的感知方案

DGIOT平台实时展示OPC上报数据全流程代码剖析

网易云信亮相LiveVideoStackCon2022,解构基于WebRTC的开源低延时播放器实践

未来5年的9大技术趋势

【硬件架构的艺术】学习笔记(4)流水线的艺术

Embedded Development: Embedded Basics - Mapping Peripherals Using Arrays of Pointers

一张图快速了解 Istio 的 EnvoyFilter
随机推荐
v-on补充:自定义参数传递和事件修饰符
Alluxio on Amazon EMR 集成实践
router.afterEach()
长markdown文档的拆分与合并
【硬件架构的艺术】学习笔记(4)流水线的艺术
nacos服务注册
华为-坐标移动
leetcode:340.至多包含K个不同字符的最长子串
#夏日挑战赛#【ELT.ZIP】啃论文俱乐部——学术科研方法论沉淀辑
Your local docbook2man was found to work with SGML rather than XML
电力系统潮流计算(牛顿-拉夫逊法、高斯-赛德尔法、快速解耦法)【6节点 9节点 14节点 26节点 30节点 57节点】(Matlab代码实现)
讯飞翻译机抢镜背后,跨语种沟通迈入全新时代
v-show指令:切换元素的显示与隐藏
Moonbeam于Moonbase Alpha构建新式XCM对EVM跨链功能
dedecms支持PowerPoint粘贴
百度、四维图新、高德争“鲜”恐后
Oracle Install [email protected] 7.6
深度学习培训二笔记
「企业架构」企业架构师,解决方案架构师和软件架构师有何不同
node环境变量配置,npm环境变量配置