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

cas自定义返回信息和自定义认证

自定义返回信息

package com.demo.sso;import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;import java.io.Serializable;
import java.util.Map;@Data
public class SSOResult implements Serializable {private static final long serialVersionUID = 758218773320954800L;/** 用户名 */@JsonProperty("id")private String username;/** 需要返回实现org.apereo.cas.authentication.principal.Principal的类名接口 */@JsonProperty("@class")private String clazz = "org.apereo.cas.authentication.principal.SimplePrincipal";/*** 其它自定义信息*/@JsonProperty("attributes")private Map<String, Object> attributes;
}

自定义账号密码认证

package com.demo.sso.handler;import com.google.common.collect.Lists;
import com.demo.sso.LoginInfo;
import com.demo.sso.SSOResult;
import com.demo.sso.exception.AuthException;
import com.demo.sso.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.PreventedException;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.services.ServicesManager;
import org.apereo.cas.util.EncodingUtils;
import org.apereo.inspektr.common.web.ClientInfo;
import org.apereo.inspektr.common.web.ClientInfoHolder;import javax.security.auth.login.FailedLoginException;
import java.security.GeneralSecurityException;/*** 自定义账号密码认证handler*/
@Slf4j
public class CustomUsernamePasswordAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {public CustomUsernamePasswordAuthenticationHandler(String name, ServicesManager servicesManager, PrincipalFactory principalFactory, Integer order) {super(name, servicesManager, principalFactory, order);}@Overrideprotected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential c, String originalPassword) throws GeneralSecurityException, PreventedException {try {/*** c.getUsername():loginname 明文* c.getPassword(): Base64(Base64(password):loginType:Base64(userAgent):Base64(uuid))*/String[] info = EncodingUtils.decodeBase64ToString(c.getPassword()).split(":");ClientInfo clientInfo = ClientInfoHolder.getClientInfo();log.info("==c.getUsername:{}===ip:{}", c.getUsername(), clientInfo.getClientIpAddress());LoginInfo loginInfo = new LoginInfo();loginInfo.setLoginName(c.getUsername());loginInfo.setPassword(EncodingUtils.decodeBase64ToString(info[0]));loginInfo.setLoginType(info[1]);loginInfo.setUserAgent(info[2]);loginInfo.setUuid(EncodingUtils.decodeBase64ToString(info[3]));loginInfo.setClientIp(clientInfo.getClientIpAddress());loginInfo.setServerIp(clientInfo.getServerIpAddress());// TODO 进行自定义认证// SSOResult result = authorizeService.login(loginInfo);SSOResult result = new SSOResult();log.info("==result===" + JsonUtils.toString(result));if (result.getUsername() != null) {// 登录成功final Principal principal = this.principalFactory.createPrincipal(result.getUsername(), result.getAttributes());return createHandlerResult(c, principal, Lists.newArrayList());}} catch (AuthException authException) {throw new GeneralSecurityException(authException.getMsg());} catch (Exception e) {throw new GeneralSecurityException(e.getMessage());}throw new FailedLoginException("CustomUsernamePasswordAuthenticationHandler:" + c.getUsername());}
}
package com.demo.sso.conf;import com.demo.sso.CustomUsernamePasswordAuthenticationHandler;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlan;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlanConfigurer;
import org.apereo.cas.authentication.AuthenticationHandler;
import org.apereo.cas.authentication.principal.DefaultPrincipalFactory;
import org.apereo.cas.services.ServicesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/***  配置类,注入自定义的账号密码认证handler*/
@Configuration
public class CustomAuthenticationEventExecutionPlanConfigurer implements AuthenticationEventExecutionPlanConfigurer {@Autowired@Qualifier("servicesManager")private ServicesManager servicesManager;@Bean@RefreshScopepublic AuthenticationHandler customUsernamePasswordAuthenticationHandler(){CustomUsernamePasswordAuthenticationHandler customUsernamePasswordAuthenticationHandler = new CustomUsernamePasswordAuthenticationHandler(CustomUsernamePasswordAuthenticationHandler.class.getSimpleName(), servicesManager, new DefaultPrincipalFactory(), 1);return customUsernamePasswordAuthenticationHandler;}@Overridepublic void configureAuthenticationExecutionPlan(AuthenticationEventExecutionPlan plan) {plan.registerAuthenticationHandler(customUsernamePasswordAuthenticationHandler());}
}
http://www.dtcms.com/a/307552.html

相关文章:

  • 考研408_数据结构笔记(第三章栈、队列和数组)
  • 解构衡石嵌入式BI:统一语义层与API网关的原子化封装架构
  • Vue 中使用 Dexie.js
  • 城市客运安全员证考试难不难?如何高效备考
  • CUDA系列之常用Runtime API简介
  • BatchNorm 一般放在哪里?
  • Ⅹ—6.计算机二级综合题19---22套
  • 接口自动化测试以及requests
  • TS语法最佳实践
  • 【笔记】热力学定律推导(6)热力学第二定律推导
  • 【MATLAB】(二)基础知识
  • Golang 指针
  • Valgrind终极指南:深入内存安全与性能瓶颈检测
  • 云原生运维与混合云运维:如何选择及 Wisdom SSH 的应用
  • Android依赖注入框架Hilt入门指南
  • 大白话畅谈:stm32中断和FreeRTOS的中断
  • 【源力觉醒 创作者计划】_巅峰对话文心 4.5 与通义千问、DeepSeek 能力对比解析
  • 【工具】NVM完全指南:Node.js版本管理工具的安装与使用详解
  • 如何将照片从 realme 手机传输到电脑?
  • MongoDB系列教程-第四章:MongoDB Compass可视化和管理MongoDB数据库
  • node.js之Koa框架
  • 蓝牙 BR/EDR 与 BLE PHY
  • Kafka在Springboot项目中的实践
  • vue3.0 + TypeScript 中使用 axios 同时进行二次封装
  • ESXI虚拟交换机 + H3C S5120交换机 + GR5200路由器组网笔记
  • 数据结构与算法:队列的表示和操作的实现
  • Linux 下自动化脚本安装Jdk、Nginx等软件
  • Java语言/Netty框架的新能源汽车充电桩系统平台
  • 《人工智能导论》(python版)第2章 python基础2.2编程基础
  • Rust视频处理开源项目精选