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

devtools/2024/9/21 8:50:03/

修改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/devtools/114896.html

相关文章

reg和wire的区别 HDL语言

文章目录 数据类型根本区别什么时候要定义wire小结 数据类型 HDL语言有三种数据类型&#xff1a;寄存器数据类型&#xff08;reg&#xff09;、线网数据类型&#xff08;wire&#xff09;、参数数据类型&#xff08;parameter&#xff09;。 根本区别 reg&#xff1a; 寄存器…

Java | Leetcode Java题解之第420题强密码检验器

题目&#xff1a; 题解&#xff1a; class Solution {public int strongPasswordChecker(String password) {int n password.length();int hasLower 0, hasUpper 0, hasDigit 0;for (int i 0; i < n; i) {char ch password.charAt(i);if (Character.isLowerCase(ch))…

微信小程序06-综合项目点餐系统

零、文章目录 微信小程序06-综合项目点餐系统 1、项目开发准备 &#xff08;1&#xff09;开发背景 现如今&#xff0c;相比以服务员为中介完成点餐、送餐、买单的传统点餐方式&#xff0c;越来越多的餐厅开始使用微信小程序进行点餐。商家可以在微信小程序中添加点餐和收款…

将阮一峰老师的《ES6入门教程》的源码拷贝本地运行和发布

你好同学&#xff0c;我是沐爸&#xff0c;欢迎点赞、收藏、评论和关注。 阮一峰老师的《ES6入门教程》应该是很多同学学习 ES6 知识的重要参考吧&#xff0c;应该也有很多同学在看该文档的时候&#xff0c;想知道这个教程的前端源码是怎么实现的&#xff0c;也可能有同学下载…

用SpringBoot进行通义千问接口调用同步方法和异步流式多轮回复方法

同步效果就不展示了,这里展示更常用的异步,多轮异步流式效果展示如下: 第一轮回答,此时没有会话id,需要雪花算法生成插入数据库 第二轮问题以及结果内容组合 1、同步版本环境准备以及代码 需要开通阿里大模型服务,如果没有开通服务,单独的去生成 key 是无效的。 阿里…

代码随想录刷题day32丨动态规划理论基础,509. 斐波那契数, 70. 爬楼梯, 746. 使用最小花费爬楼梯

代码随想录刷题day32丨动态规划理论基础&#xff0c;509. 斐波那契数&#xff0c; 70. 爬楼梯&#xff0c; 746. 使用最小花费爬楼梯 1.动态规划理论基础 动态规划&#xff0c;英文&#xff1a;Dynamic Programming&#xff0c;简称DP&#xff0c;如果某一问题有很多重叠子问题…

企业如何使用数据分析管理系统

在数字化时代&#xff0c;数据成为企业发展新的增长方向&#xff0c;如何利用数据分析管理系统高效管理和运用这些数据&#xff0c;已成为企业决策者们亟待解决的关键所在。数聚股份将通过多年的实践经验来深入探讨企业如何通过数据分析管理系统实现智能决策&#xff0c;增强竞…

[网络][知识]TCP-IP各协议的RFC编号和RFC原始文档的获取地址

TCP/IP协议族包括很多个子协议,下面是TCP/IP 协议和支持服务所支持的 RFC。 RFC768 用户数据报协议 (UDP) RFC783 简单文件传输协议 (TFTP) RFC791 Internet 协议 (IP) RFC792 Internet 控制消息协议 (ICMP) RFC793 传输控制协议 (TCP) RFC816 故障隔离和恢复 RFC…