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

springboot 集成kerberos 用户认证 获取域账号

springboot 集成kerberos 用户认证 获取域账号

只能在linux环境获取

MyWebSecurityConfiguration 需要改这个两行代码

        ticketValidator.setServicePrincipal("设置名字一般是COM结尾");ticketValidator.setKeyTabLocation(new FileSystemResource("kerbtest05文件地址"));

1.springboot 启动类上添加

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})

2. DummyUserDetailsService.java

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;public class DummyUserDetailsService implements UserDetailsService {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {return new User(username, "notUsed", true, true, true, true,AuthorityUtils.createAuthorityList("ROLE_USER", "ROLE_ADMIN"));}
}

3. MyWebSecurityConfiguration


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.FileSystemResource;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.kerberos.authentication.KerberosAuthenticationProvider;
import org.springframework.security.kerberos.authentication.KerberosServiceAuthenticationProvider;
import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosClient;
import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator;
import org.springframework.security.kerberos.web.authentication.SpnegoAuthenticationProcessingFilter;
import org.springframework.security.kerberos.web.authentication.SpnegoEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;@Configuration
@EnableWebSecurity
@Order(Ordered.HIGHEST_PRECEDENCE)//可以加 可以不加看自己代码是否报错
public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().exceptionHandling().authenticationEntryPoint(spnegoEntryPoint())//.accessDeniedPage("/login").and().headers().frameOptions().sameOrigin().and().authorizeRequests().antMatchers("/resources/**","/test2/**").permitAll().anyRequest().authenticated().and().formLogin()//.loginPage("/login").permitAll().and().logout().permitAll().and().addFilterBefore(spnegoAuthenticationProcessingFilter(), BasicAuthenticationFilter.class);}@Overridepublic void configure(AuthenticationManagerBuilder auth) throws Exception {auth.authenticationProvider(kerberosAuthenticationProvider()).authenticationProvider(kerberosServiceAuthenticationProvider());}@Beanpublic KerberosAuthenticationProvider kerberosAuthenticationProvider() {KerberosAuthenticationProvider provider = new KerberosAuthenticationProvider();SunJaasKerberosClient client = new SunJaasKerberosClient();client.setDebug(true);provider.setKerberosClient(client);provider.setUserDetailsService(dummyUserDetailsService());return provider;}@Beanpublic SpnegoEntryPoint spnegoEntryPoint() {return new SpnegoEntryPoint();}@Beanpublic SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter() {SpnegoAuthenticationProcessingFilter filter = new SpnegoAuthenticationProcessingFilter();try {filter.setAuthenticationManager(authenticationManagerBean());} catch (Exception e) {}return filter;}@Beanpublic KerberosServiceAuthenticationProvider kerberosServiceAuthenticationProvider() {KerberosServiceAuthenticationProvider provider = new KerberosServiceAuthenticationProvider();provider.setTicketValidator(sunJaasKerberosTicketValidator());provider.setUserDetailsService(dummyUserDetailsService());return provider;}@Beanpublic SunJaasKerberosTicketValidator sunJaasKerberosTicketValidator() {SunJaasKerberosTicketValidator ticketValidator = new SunJaasKerberosTicketValidator();ticketValidator.setServicePrincipal("设置名字一般是COM结尾");ticketValidator.setKeyTabLocation(new FileSystemResource("kerbtest05文件地址"));ticketValidator.setDebug(true); //Turn off when it will works properly,return ticketValidator;}@Beanpublic DummyUserDetailsService dummyUserDetailsService() {return new DummyUserDetailsService();}}

获取域账号 方法

    public static String getCurrentUserId(HttpServletRequest req) {String userId = "";String sessionUserId = (String) req.getSession().getAttribute("userId");if (StringUtils.isNotEmpty(sessionUserId)) {userId = sessionUserId;} else {userId = req.getRemoteUser();if (userId != null && userId.indexOf("\\") > 0) {userId = userId.substring(userId.indexOf("\\") + 1).trim();} else if (userId != null && userId.indexOf("@") > 0) {userId = userId.substring(0, userId.indexOf("@")).trim();}req.getSession().setAttribute("userId", userId);}return userId;}
http://www.xdnf.cn/news/8244.html

相关文章:

  • 完整改进RIME算法,基于修正多项式微分学习算子Rime-ice增长优化器,完整MATLAB代码获取
  • 【数据结构】 栈和队列
  • 微软全新开源的Agentic Web网络项目:NLWeb,到底是什么 ?
  • 鸿蒙App开发学习路径
  • JAVA|后端编码规范
  • 仿腾讯会议——视频发送接收
  • 计算机发展史
  • 从零基础到最佳实践:Vue.js 系列(7/10):《常用内置 API 与插件》
  • scratch课后一练--事件模块
  • Linux系统编程 | IPC对象---消息队列
  • DeepSeek:开启IT领域人效管理新时代
  • Java-根据路径获取JSON字符串的value值
  • zabbix 常见问题
  • 深入解析JVM垃圾回收器:原理、实践与调优指南
  • 实用重复文件批量处理工具
  • 关于SQL SERVER中round函数的用法和示例
  • 一台机器怎么部署k8s集群
  • React-fiber架构
  • Python可视化设计原则
  • 【424. 替换后的最长重复字符】
  • docker-compose常用命令介绍
  • 已经 上线 Vue 项目 国际化 i18n 中译英
  • OpenCV 图像对象的创建与赋值
  • Apollo10.0学习——planning模块(9)之参数详解一
  • Vscode +Keil Assistant编译报错处理
  • C++ -- vector
  • 系统性能分析基本概念(5) : 何时开始性能分析
  • 【语法】C++的map/set
  • 平安健康2025年一季度深耕医养,科技赋能见成效
  • Android Service与BroadcastReceiver深度解析:从零到一的实现与优化