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

news/2024/9/24 19:02:11/

修改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

java">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 配置忽略拦截

java">@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://前端页面")

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


http://www.ppmy.cn/news/1529932.html

相关文章

前端CSS学习框架

⭐️ CSS &#x1f4ac; 描述&#xff1a;层叠样式表&#xff0c;用于设计风格和布局。 &#x1f4da; 资源&#xff1a;学习使用 CSS 为 HTML 添加样式 - 学习 Web 开发 | MDN ⭐️ 基本语法 ⭐️ 引入方式 行内样式 内部样式表 外部样式表 ⭐️ 选择器 通用选择器 标签…

排序算法Java实现

文章目录 排序算法概述比较排序算法非比较排序算法稳定 vs 不稳定Java 中的排序 外部排序1) 冒泡排序2) 选择排序3) 堆排序4) 插入排序5) 希尔排序6) 归并排序递归实现时间复杂度非递归实现 7) 归并插入8) 快速排序随机基准点处理重复值 9) 计数排序10) 桶排序11) 基数排序 排序…

Spring MVC 启动与请求处理流程解析

Spring MVC 是一个基于 Java 的实现 MVC 设计模式的 Web 框架&#xff0c;它是 Spring 框架的一个模块&#xff0c;用于构建 Web 应用程序。Spring MVC 的启动过程主要涉及以下几个步骤&#xff1a; &#xff08;看流程图会比较直观一些&#xff09; #mermaid-svg-KLVe3JEll2OL…

1466C/D/E/G/H信号发生器

1466C/D/E/G/H 信号发生器 频率范围&#xff1a;6kHz~110GHz 产品综述 Ceyear 1466系列信号发生器是一款面向微波毫米波尖端测试的通用测试仪器&#xff0c;频率范围覆盖宽、信号频谱纯度高&#xff0c;具有高准确度和大动态范围的功率输出&#xff0c;搭配单机双射频通道的…

手机上轻松解压并处理 JSON 文件

JSON&#xff08;JavaScript Object Notation&#xff09;是一种轻量级的数据交换格式&#xff0c;在手机上有着广泛的应用场景。 首先&#xff0c;在数据传输方面&#xff0c;许多移动应用程序通过网络请求与后端服务器进行交互&#xff0c;而服务器端的 API 接口通常使用 JS…

【补充篇】Davinci工具要求的dbc格式

1 简介 目前国内一般E/E架构使用的建模软件多为PREEvision、EnterPrise Architect、Rhapsody等,每个工具都有各自的优势,但对AUTOSAR的适配也不全都尽如人意。比方说,EA本身的输出物是XML格式,做通信系统设计的话需要基于C#做一定的二次开发来转换格式。虽然PREEvision支持…

【C/C++】涉及string类的经典OJ编程题

【C/C】涉及string类的经典OJ编程题 一. 把字符串转化成整数&#xff08;atoi&#xff09;解法一&#xff1a;&#xff08;不用long&#xff09;完整代码&#xff1a;解法二&#xff1a;&#xff08;用long&#xff09; 二.字符串相加代码实现&#xff08;含注释&#xff09;&a…

AutoX.js向后端传输二进制数据

android的JavaScript自动化软件用过Hamibot和AutoX.js 不过在向后端传输二进制数据时都有些限制&#xff0c;不如浏览器前端那么自由。Hamibot的http按文档应该时能支持传字节数组&#xff0c;但是实际上应该还没有支持。AutoX.js的http也是这样&#xff0c;但是AutoX.js还支持…