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

Yudao单体项目 springboot Admin安全验证开启

1. YudaoWebSecurityConfigurerAdapter中:

注释掉

    /*** 由于 Spring Security 创建 AuthenticationManager 对象时,没声明 @Bean 注解,导致无法被注入* 通过覆写父类的该方法,添加 @Bean 注解,解决该问题*/
//    @Bean
//    public AuthenticationManager authenticationManagerBean(AuthenticationConfiguration authenticationConfiguration) throws Exception {
//        return authenticationConfiguration.getAuthenticationManager();
//    }

新增

// 配置Admin路径的过滤器链(高优先级)@Order(Ordered.HIGHEST_PRECEDENCE)@Beanpublic SecurityFilterChain adminSecurityFilterChain(HttpSecurity http) throws Exception {String adminContextPath = "/admin";SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();successHandler.setTargetUrlParameter("redirectTo");successHandler.setDefaultTargetUrl(adminContextPath + "/");http.securityMatchers(matchers -> matchers.requestMatchers(adminContextPath + "/**")).authorizeHttpRequests(auth -> auth.requestMatchers(adminContextPath + "/assets/**").permitAll().requestMatchers(adminContextPath + "/login").permitAll().anyRequest().hasRole("ADMIN")).formLogin(form -> form.loginPage(adminContextPath + "/login").loginProcessingUrl(adminContextPath + "/login").successHandler(successHandler)).logout(logout -> logout.logoutUrl(adminContextPath + "/logout").logoutSuccessUrl(adminContextPath + "/login?logout")).httpBasic(withDefaults()).csrf(csrf -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).ignoringRequestMatchers(adminContextPath + "/instances",adminContextPath + "/actuator/**")).rememberMe(rememberMe -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600)).sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.ALWAYS));return http.build();}

修改:

@Bean
@Order(Ordered.LOWEST_PRECEDENCE) // 新增:优先级低
protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {...}

2. Infra 模块中SecurityConfiguration中:

注释掉:

// Spring Boot Admin Server 的安全配置
registry.requestMatchers(adminSeverContextPath).permitAll().requestMatchers(adminSeverContextPath + "/**").permitAll();

3. TokenAuthenticationFilter中:

@Override@SuppressWarnings("NullableProblems")protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws ServletException, IOException {String token = SecurityFrameworkUtils.obtainAuthorization(request,securityProperties.getTokenHeader(), securityProperties.getTokenParameter());// 新增以下代码    if (request.getRequestURI().startsWith("/admin/")) {chain.doFilter(request, response);return;}...

4. application.yaml

spring:security:user:name: adminpassword: $2a$10$12i5oKpeTFgyziHNeSGhOeJJy6 # bcrypt加密roles: ADMIN

5. application-local.yaml

# Spring Boot Admin 配置项
spring:boot:admin:# Spring Boot Admin Client 客户端的相关配置client:url: http://127.0.0.1:${server.port}/${spring.boot.admin.context-path} # 设置 Spring Boot Admin Server 地址instance:service-host-type: IP # 注册实例时,优先使用 IP [IP, HOST_NAME, CANONICAL_HOST_NAME]username: adminpassword: ***# Spring Boot Admin Server 服务端的相关配置context-path: /admin # 配置 Spring

注意,如果是配置的nginx反代https请求,则上面的application-local.yaml需要配置:

# Spring Boot Admin 配置项
spring:boot:admin:ui:public-url: https://xx.xxxxx.cn/${spring.boot.admin.context-path}# Spring Boot Admin Client 客户端的相关配置client:url: https://xx.xxxxx.cn/${spring.boot.admin.context-path} # 设置 Spring Boot Admin Server 地址instance:service-host-type: IP # 注册实例时,优先使用 IP [IP, HOST_NAME, CANONICAL_HOST_NAME]username: adminpassword: ***# Spring Boot Admin Server 服务端的相关配置context-path: /wz-admin # 配置 Spring

http://www.dtcms.com/a/461903.html

相关文章:

  • 电子商务网站建设风格网站建设技术的实现
  • 【Frida Android】基础篇2:Frida基础操作模式详解
  • 应用于ElasticSearch的C++ API——elasticlient
  • MyISAM存储引擎的特点
  • 伺服滑差补偿方案
  • 无锡网站建设排名安徽网站开发建设
  • 【C++】探秘string的底层实现
  • 建设卡开通网银网站学做网站 空间 域名
  • 基于Simulink的太阳能单极性移相控制光伏并网逆变器
  • 受欢迎的锦州网站建设wordpress取消默认图片
  • CUDA-Q Quake 规范详解:量子中间表示的技术深度解析
  • 包头教育平台网站建设吉化北建公司官网
  • LeetCode 3494.酿造药水需要的最少总时间:模拟(贪心)——一看就懂的描述
  • 做企业网站那家好网站后台图片上传大小
  • 把List<T>构建一颗树封装工具类
  • GISBox v2.0.0:新增功能、问题修复、性能优化三维度,强化GIS服务核心能力
  • Qt界面布局利器:QStackedWidget详细用法解析
  • ClickHouse 配置优化与问题解决
  • 宁波网站建设果核个人网页制作教程简单
  • 白塔网站建设iis .htaccess wordpress
  • 【计算机视觉】基于复杂环境下的车牌识别
  • 域名会跳转怎么进原网站wordpress注册没用
  • YOLO26 详解:面向边缘与低功耗的端到端(NMS‑free)目标检测新范式
  • 仿win8网站建电子商务网站需要多少钱
  • 网站右下角视频代码网站免费下载安装
  • 《低压配电数字化转型实战指南》13: 技术创新:下一代配电技术探索
  • 再见的数字怎么说好听
  • Cesium 军事标绘入门:用 Cesium-Plot-JS 快速实现标绘功能
  • 【ROS2快速学习】
  • Vue3源码runtime-core运行时核心模块之provide依赖和inject注入详解