当前位置: 首页 > news >正文

前后端分离集成CAS单点登录

修改nginx

worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;location /api/ {proxy_pass http://127.0.0.1:9001/; # 后端}location / {proxy_pass http://127.0.0.1:3000/; # 前端}}
}

改写redirect返回401

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.jasig.cas.client.authentication.AuthenticationRedirectStrategy;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;/*** <p>Title: </p>* <p>Description: CAS统一身份认证集成配置,session过期或未认证时返回结果处理</p>* <p>Copyright: Copyright  (c) 2023</p>* <p>Company: </p>** @author yanfh* @version 1.0* @date 2023/4/14  10:11*/
@Component
public class CustomAuthRedirectStrategy implements AuthenticationRedirectStrategy {/*** 重定向策略,由原来自动跳转url,改为返回json** @param httpServletRequest request请求* @param httpServletResponse response请求* @param potentialRedirectUrl 重定向URL* @throws IOException IO异常*/@Overridepublic void redirect(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String potentialRedirectUrl) throws IOException {httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());httpServletResponse.setHeader("content-type", "text/html;charset=UTF-8");httpServletResponse.setCharacterEncoding("UTF-8");PrintWriter out = httpServletResponse.getWriter();ObjectMapper om = new ObjectMapper();ObjectNode node = om.createObjectNode();node.put("code", HttpStatus.UNAUTHORIZED.value());node.put("message", "Unauthorized");out.write(om.writeValueAsString(node));}
}

cas 配置忽略拦截

@Beanpublic FilterRegistrationBean filterAuthenticationRegistration() {final FilterRegistrationBean registration = new FilterRegistrationBean();registration.setFilter(new AuthenticationFilter());// 设定匹配的路径registration.addUrlPatterns("/*");Map<String, String> initParameters = new HashMap<String, String>();initParameters.put("casServerLoginUrl", serverLoginUrl);initParameters.put("serverName", clientHostUrl);if (ignorePattern != null && !"".equals(ignorePattern)) {initParameters.put("ignorePattern", ignorePattern);}//自定义UrlPatternMatcherStrategy 验证规则if (ignoreUrlPatternType != null && !"".equals(ignoreUrlPatternType)) {initParameters.put("ignoreUrlPatternType", ignoreUrlPatternType);}initParameters.put("authenticationRedirectStrategyClass", CustomAuthRedirectStrategy.class.getName());registration.setInitParameters(initParameters);// 设定加载的顺序registration.setOrder(2);return registration;}

前端请求后端接口判断是否返回401,若返回401,手动拼接认证地址跳转window.location.href='http://CAS服务端/cas/login?service='+encodeURIComponent('http://后端/api/login'),由后端 response.sendRedirect("http://前端页面")

@GetMapping("/login")public void casRedirect(HttpServletRequest request, HttpServletResponse response) {try {response.sendRedirect(clientUrl);} catch (java.io.IOException e) {throw new RuntimeException(e);}}

相关文章:

  • Redis配置文件详解(上)
  • 【C++】——vector深度剖析模拟实现
  • 命名导出和默认导出的用法区别
  • RabbitMQ08_保证消息可靠性
  • 2、在LVGL模拟器中了解部件的基础属性
  • 智慧城市主要运营模式分析
  • 【后端开发】JavaEE初阶——计算机是如何工作的???
  • 常见的TTL,RS232,RS485,IIC,SPI,UART之间的联系和区别
  • YOLOv8 Flask整合问题
  • 什么是Agent智能体?
  • 2024年信息安全企业CRM选型与应用研究报告
  • 探索 Android DataBinding:实现数据与视图的完美融合
  • 中间件:maxwell、canal
  • SpinalHDL之语义(Semantic)(一)
  • Python办公自动化案例:将Excel数据批量保存到Word表格中
  • $translatePartialLoader加载失败及解决方式
  • .pyc 想到的一些问题
  • 【前端学习】-粗谈选择器
  • Android组件 - 收藏集 - 掘金
  • GitUp, 你不可错过的秀外慧中的git工具
  • HTTP--网络协议分层,http历史(二)
  • Making An Indicator With Pure CSS
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等
  • vue-router 实现分析
  • 工作手记之html2canvas使用概述
  • 关于springcloud Gateway中的限流
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 漂亮刷新控件-iOS
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 王永庆:技术创新改变教育未来
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • ​io --- 处理流的核心工具​
  • $(function(){})与(function($){....})(jQuery)的区别
  • $.ajax,axios,fetch三种ajax请求的区别
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (vue)页面文件上传获取:action地址
  • (阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo
  • (二)Kafka离线安装 - Zookeeper下载及安装
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (七)Knockout 创建自定义绑定
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • ******之网络***——物理***
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET Core Web APi类库如何内嵌运行?
  • .Net Core 微服务之Consul(三)-KV存储分布式锁
  • .net framework profiles /.net framework 配置
  • .Net Memory Profiler的使用举例
  • .NET Standard 支持的 .NET Framework 和 .NET Core