Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java Spring启用https的问题_Java_Spring_Security_Https_Server - Fatal编程技术网

使用Java Spring启用https的问题

使用Java Spring启用https的问题,java,spring,security,https,server,Java,Spring,Security,Https,Server,下面是关于为项目启用https安全性的简短教程。 但是,当我运行该项目时,出现以下错误: 配置为在端口8443上侦听的Tomcat连接器连接失败 开始端口可能已在使用中,或者连接器可能已损坏 配置错误 我已经生成了教程中提到的密钥库。 我以前见过有人提出这个问题,但没有明确的答案。 端口8443配置不正确,或者在命令行中键入netstate-ao时无法看到它 任何帮助都将不胜感激 ============ 我的档案 1) 应用程序属性 spring.data.rest.base-path=/a

下面是关于为项目启用https安全性的简短教程。

但是,当我运行该项目时,出现以下错误:

配置为在端口8443上侦听的Tomcat连接器连接失败 开始端口可能已在使用中,或者连接器可能已损坏 配置错误

我已经生成了教程中提到的密钥库。 我以前见过有人提出这个问题,但没有明确的答案。 端口8443配置不正确,或者在命令行中键入
netstate-ao
时无法看到它

任何帮助都将不胜感激

============

我的档案 1) 应用程序属性

spring.data.rest.base-path=/api
server.port = 8443
server.ssl.key-store-type=PKCS12
server.ssl.key-store=src/main/resources:keystore.p12
server.ssl.key-store-password=Welcome1
server.ssl.key-alias=tomcat
server.servlet.contextPath=/
2) SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.requestMatchers()
        .antMatchers("/login", "/oauth/authorize")
        .and()
        .authorizeRequests()
        .anyRequest()
        //.permitAll()
        .authenticated()
        .and()
        .formLogin()
        .permitAll();
        //.httpBasic();

    http.csrf().disable();
}

/*@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}*/

/*
// Create an encoder with strength 16
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(16);
    String result = encoder.encode("myPassword");
    assertTrue(encoder.matches("myPassword", result));
        */

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    tomcat.addAdditionalTomcatConnectors(getHttpConnector());
    return tomcat;
}

private Connector getHttpConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setScheme("http");
    connector.setPort(8080);
    connector.setSecure(false);
    connector.setRedirectPort(8443);
    return connector;
}
}

3) HelloResource

@RestController
@RequestMapping("/rest/hello")
public class HelloResource {

  @GetMapping
  public String hello(){
    return "Hello World";
  }
}

在配置中将端口更改为443

server.port = 443

可能正在检查密钥存储文件路径

server.ssl.key-store=src/main/resources:keystore.p12
to
server.ssl.key-store=src/main/resources/keystore.p12

尝试过但情况相同:(如果您使用的是SpringBoot2.1.1或更高版本,并且安装了tomcat native,则可能会遇到此问题