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

  1. YudaoWebSecurityConfigurerAdapter中:

注释掉

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

新增

java 复制代码
// 配置Admin路径的过滤器链(高优先级)
    @Order(Ordered.HIGHEST_PRECEDENCE)
    @Bean
    public 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();
    }

修改:

java 复制代码
@Bean
@Order(Ordered.LOWEST_PRECEDENCE) // 新增:优先级低
protected SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {...}
  1. Infra 模块中SecurityConfiguration中:

注释掉:

java 复制代码
// Spring Boot Admin Server 的安全配置
registry.requestMatchers(adminSeverContextPath).permitAll()
        .requestMatchers(adminSeverContextPath + "/**").permitAll();
  1. TokenAuthenticationFilter中:
java 复制代码
@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;
        }
   
    ...
  1. application.yaml
java 复制代码
spring:
  security:
    user:
      name: admin
      password: $2a$10$12i5oKpeTFgyziHNeSGhOeJJy6 # bcrypt加密
      roles: ADMIN
  1. application-local.yaml
java 复制代码
# 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: admin
        password: ***
      # 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: admin
        password: ***
      # Spring Boot Admin Server 服务端的相关配置
      context-path: /wz-admin # 配置 Spring
相关推荐
她的男孩28 分钟前
从零搭一个企业后台,为什么我把能力拆成 Starter 和 Plugin
java·后端·架构
RainCity30 分钟前
Java Swing 自定义组件库分享(七)
java·笔记·后端
Sam_Deep_Thinking35 分钟前
连锁门店的外卖订单平台对接
java·微服务·架构·系统架构
_遥远的救世主_1 小时前
从一次结果集密集型查询 OOM 看 Java 服务的稳定性架构治理
java·后端
一楼的猫1 小时前
从工具链视角对比:番茄作家助手 vs 第三方写作辅助方案
java·服务器·开发语言·前端·学习·chatgpt·ai写作
likerhood2 小时前
Java static 关键字从浅入深
java·开发语言
_院长大人_2 小时前
Java Excel导出:如何实现自定义表头与字段顺序的完全控制
java·开发语言·后端·excel
磊 子2 小时前
1.4CPU缓存一致性
java·spring cloud·缓存·系统
周末也要写八哥2 小时前
Eclipse 2024全流程网盘下载与安装配置教程详解
java·ide·eclipse
来恩10033 小时前
JSTL的标签库种类
java·开发语言