当前位置: 首页 > 知识库问答 >
问题:

Spring Boot管理端口和Spring Security性

东方俊明
2023-03-14

我有一个带有安全配置的spring boot web应用程序,可以将所有未经授权的html" target="_blank">请求转发到/登录。我设置了一个与应用程序端口不同的Spring Boot管理端口。当我转到管理端口并尝试访问/运行状况时,它尝试将我发送到/登录到该端口,我得到以下响应:

''{“时间戳”:1435680239995,“状态”:404,“错误”:“未找到”,“消息”:“无可用消息”,“路径”:“/登录”}'''

我发现了这个问题,但在我的应用程序中无法实现:Spring Boot管理安全性与端口集的工作方式不同

当尝试设置单独的管理端口时,使这个非常基本的Spring Security配置与Spring Boot一起工作的正确方法是什么??

以下是我的Spring Security配置的相关部分:

```

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()       //temporary
        .authorizeRequests()
        .antMatchers("/public/**").permitAll()
        .antMatchers("/private*/**").access("hasRole('ADMIN')")
        .antMatchers("/**").access("hasRole('USER')");

    http
      .formLogin().failureUrl("/login?error")
      .defaultSuccessUrl("/")
      .loginPage("/login")
      .permitAll()
      .and()
      .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
      .permitAll();
}
}

```

非常感谢。

更新:

我无法使上述解决方案发挥作用,但我找到了一个变通方法,将我的管理endpoint置于spring security认为的公共(许可)路径下。然后将其暴露在另一个端口后面。这适用于我的目的,即能够在仅暴露于ELB的端口上向我的ELB公开健康检查。

管理:端口:8081上下文路径:/public security:启用:false

共有1个答案

薛英卫
2023-03-14

在您发布的另一个问题之后,我设法找到了我的解决方案,它与提供的基本相同。

因此,您将有2个类实现此WebSecurityConfigrerAdapter并比较这些请求集路径并对每个请求进行身份验证。确保使用@order(0),因为会有冲突。

@Order(0)
@Configuration
public class ManagementSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Autowired
  private ManagementServerProperties managementProperties;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().formLogin().disable()
        .httpBasic()
        .authenticationEntryPoint(new NoPopupBasicAuthenticationEntryPoint())
        .and()
        .requestMatchers()
        .requestMatchers(new RequestMatcher() {
          @Override
          public boolean matches(HttpServletRequest request) {
            return managementProperties.getServlet().getContextPath().equals(request.getContextPath());
          }
        })
        .and()
        .authorizeRequests()
        .anyRequest().hasRole("ADMIN")
        .and()
        .sessionManagement().maximumSessions(1);
  }

}
 类似资料:
  • 该功能可以对您应用开放的端口进行一个调整。 该功能对容器开放的端口进行调整,初始化完之后尽量不动。因为它可能关联问Service、Deployment、Ingress、VirtualService等资源。

  • 本文向大家介绍springboot集成springsecurity 使用OAUTH2做权限管理的教程,包括了springboot集成springsecurity 使用OAUTH2做权限管理的教程的使用技巧和注意事项,需要的朋友参考一下 Spring Security OAuth2 主要配置,注意application.yml最后的配置resource filter顺序配置,不然会能获取token但

  • 正如下面我可以改变服务器端口,有没有一种方法使执行器管理服务端口相同的事情。 我知道更改为via系统参数的类似方法,将management.port=xxx添加到application.properties中。

  • 我有一个通过X509验证的API。但是,我需要从身份验证中绕过我的执行器健康检查endpoint,这样我就可以在没有客户端证书的情况下进行健康检查。 我意识到,这可以通过使用不同的端口来实现,用于执行器endpoint将命中的服务器和管理。 我的application.yml配置如下: 我的问题是,即使使用这种配置,管理endpoint也不是我可以访问健康endpoint的地方。http://lo

  • 本文向大家介绍SpringBoot + SpringSecurity 环境搭建的步骤,包括了SpringBoot + SpringSecurity 环境搭建的步骤的使用技巧和注意事项,需要的朋友参考一下 一、使用SpringBoot+Maven搭建一个多模块项目(可以参考这篇文章 --> 这里) 二、删除父工程的src文件,删除app、browser、core下的.java文件 依赖关系: dem

  • 我最终使用了3个不同的端口。777,3000和4000,我似乎仍然没有一个想要的结果。 如何为Socket.io和nodejs应用程序使用相同的端口?即使用端口3000。