Spring Boot中HTTPS配置,服务器返回超时

问题描述 投票:0回答:1

我在 Spring Boot 应用程序中设置 HTTPS 时遇到问题。该应用程序托管在具有 Ubuntu 20 的 AWS EC2 服务器上。当我尝试使用 HTTPS 通过 Postman 访问该应用程序时,服务器响应超时。

Spring 安全配置:

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private final UserDetailsDataImplements clientService;
    private final PasswordEncoder passwordEncoder;

    public SecurityConfiguration(UserDetailsDataImplements usuarioService, PasswordEncoder passwordEncoder) {
        this.clientService = usuarioService;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(clientService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requiresChannel() // Requer configurações de canal (HTTP/HTTPS)
                .anyRequest().requiresSecure() // Requer HTTPS para todas as requisições
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/login").permitAll()
                .antMatchers(HttpMethod.GET, "/update").permitAll()
                .antMatchers(HttpMethod.POST, "/client").permitAll()
                .antMatchers(HttpMethod.GET, "/data/test").permitAll()
                .antMatchers(HttpMethod.POST, "/data/register").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new AuthenticationFilter(authenticationManager()))
                .addFilter(new AuthValidation(authenticationManager()))
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .cors();
    }


    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "TRACE", "CONNECT"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

}

AWS EC2 控制台:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2024-03-31 22:25:01.203  INFO 16246 --- [           main] com.brasens.main.BrasensRest             : Starting BrasensRest v0.0.1-SNAPSHOT using Java 11.0.22 on ip-172-31-21-105 with PID 16246 (/home/ubuntu/mspm-backend/target/msmp-http-0.0.1-SNAPSHOT.jar started by ubuntu in /home/ubuntu/mspm-backend/target)
2024-03-31 22:25:01.209  INFO 16246 --- [           main] com.brasens.main.BrasensRest             : The following profiles are active: prod
2024-03-31 22:25:04.665  INFO 16246 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-03-31 22:25:05.058  INFO 16246 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 371 ms. Found 14 JPA repository interfaces.
2024-03-31 22:25:06.972  INFO 16246 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8443 (https)
2024-03-31 22:25:07.001  INFO 16246 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-03-31 22:25:07.002  INFO 16246 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2024-03-31 22:25:07.209  INFO 16246 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-03-31 22:25:07.215  INFO 16246 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5846 ms
2024-03-31 22:25:08.780  INFO 16246 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2024-03-31 22:25:08.965  INFO 16246 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.4.Final
2024-03-31 22:25:09.386  INFO 16246 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2024-03-31 22:25:09.599  INFO 16246 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-03-31 22:25:10.598  INFO 16246 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-03-31 22:25:10.652  INFO 16246 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgresPlusDialect
2024-03-31 22:25:13.054  INFO 16246 --- [           main] org.hibernate.tuple.PojoInstantiator     : HHH000182: No default (no-argument) constructor for class: com.brasens.main.security.PasswordResetToken (class must be instantiated by Interceptor)
2024-03-31 22:25:13.726  INFO 16246 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2024-03-31 22:25:13.740  INFO 16246 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-03-31 22:25:15.235  WARN 16246 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2024-03-31 22:25:15.973  INFO 16246 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : Autowired annotation should only be used on methods with parameters: public void com.brasens.main.cronjobs.Scheduler.check()
2024-03-31 22:25:16.363  INFO 16246 --- [           main] o.s.s.w.a.c.ChannelProcessingFilter      : Validated configuration attributes
2024-03-31 22:25:16.441  INFO 16246 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with [org.springframework.security.web.access.channel.ChannelProcessingFilter@4a89ef44, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6a950a3b, org.springframework.security.web.context.SecurityContextPersistenceFilter@681c0ae6, org.springframework.security.web.header.HeaderWriterFilter@15639d09, org.springframework.web.filter.CorsFilter@4f7be6c8, org.springframework.security.web.authentication.logout.LogoutFilter@1a2e0d57, com.brasens.main.security.AuthenticationFilter@647b9364, com.brasens.main.security.AuthValidation@b6bccb4, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4d98e41b, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@7459a21e, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@49edcb30, org.springframework.security.web.session.SessionManagementFilter@52bd9a27, org.springframework.security.web.access.ExceptionTranslationFilter@7634f2b, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@1e1237ab]
2024-03-31 22:25:17.839  INFO 16246 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2024-03-31 22:25:18.286  INFO 16246 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8443 (https) with context path ''
2024-03-31 22:25:18.341  INFO 16246 --- [           main] com.brasens.main.BrasensRest             : Started BrasensRest in 18.862 seconds (JVM running for 20.927)
^C2024-03-31 22:28:58.761  INFO 16246 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2024-03-31 22:28:58.764  INFO 16246 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-03-31 22:28:58.791  INFO 16246 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

邮递员的照片:

enter image description here

AWS EC2 安全组照片: enter image description here 出站规则也是这样的

应用程序属性:

http.port: 8080
server.port: 8443

################# SSL CONFIG #################
security.require-ssl=true
server.ssl.key-store:/etc/letsencrypt/live/brasens.com/keystore.p12
server.ssl.key-store-password: root
server.ssl.keyStoreType: PKCS12

server.ssl.keyAlias: tomcat
@RestController
@RequestMapping("/data")
public class DataController {
    @GetMapping("/test")
    public ResponseEntity test() {
        System.out.println("TESTED!");
        return ResponseEntity.ok("TESTING...");
    }
}

尝试通过 HTTPS 访问应用程序时,可能导致超时的原因是什么? 我是否应该在 Spring Boot 或 AWS EC2 中进行任何其他设置以确保 HTTPS 正常工作? 关于如何诊断和解决此超时问题有什么建议吗?

HTTP: enter image description here

java spring security https
1个回答
0
投票

我做了一些修改和测试。 结果在本地服务器上运行正常,说明问题出在AWS对吧?

https http

© www.soinside.com 2019 - 2024. All rights reserved.