Springboot整合https请求(http自动跳转https)

因为项目利用了webRTC技术,查到解决方案是采用https可以把本地demo跑起来,于是就用springboot整合了https,本地demo也是顺利跑起来了。为了避免整合入坑,把细节代码贴出来。

1.生成证书

keytool -genkey -alias tomcat -keyalg RSA -keystore tomcatkeystore.keystore  -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN" -keypass 123456

需要下载keytool插件,命令是在当前目录下生成 tomcatkeystore.keystore证书

2.配置springboot

2.1关于yml文件的配置

server:
  port: 8080
  custom:
    httpPort: 8890
  servlet:
    session:
      timeout: 1800
  ssl:
    key-store: tomcatkeystore.keystore
    key-store-password: 123456
    key-store-type: JKS
    key-alias: tomcat
spring:
  thymeleaf:
    prefix: classpath:/templates/
  servlet:
    multipart:
      max-file-size: 50MB
      max-request-size: 50MB
      location: /data/upload_tmp

2.2关于书写配置类

package com.sap.intellgentcam.demo1.config;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : Jenson.Liu
 * @date : 2019/11/11  2:51 下午
 */
@Configuration
public class HttpsConfig {

//读写配置文件
    @Value("${server.custom.httpPort}")
    private Integer httpPort;

    @Value("${server.port}")
    private Integer httpsPort;

    @Bean
    public TomcatServletWebServerFactory servletContainer() { //springboot2 新变化

        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(initiateHttpConnector());
        return tomcat;
    }

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


结合配置文件和配置类表达的意思:

当你使用8890端口请求的时候使用http协议

当你使用8080端口请求的时候使用https协议

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用以下配置来跳转到指定的页面: 在 Spring Security 配置中,使用`formLogin()`方法开启表单登录,在其中设置`loginPage()`方法来指定登录页面地址,如下所示: ```java @Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 不进行权限验证的 URL .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") // 指定登录页面地址 .permitAll() .and() .logout() .permitAll(); } } ``` 在上述代码中,`/login` 路径为登录页面地址,是一个 GET 请求,访问该路径时返回登录页面。在登录页面中,用户输入用户名和密码,然后提交表单,将会通过 POST 请求发送到`/login`地址。 通过重写 `configure(AuthenticationManagerBuilder auth)` 方法,可以指定用户认证方式默认使用内存中的认证方式。在下面的代码示例中,使用了一对用户名和密码 `user/userpassword`。 ```java @Configuration @EnableWebSecurity public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 不进行权限验证的 URL .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") // 指定登录页面地址 .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user") .password("{noop}userpassword") .roles("USER"); } } ``` 在上述代码中,“{noop}” 是 5.x 版本的特性,用于指定密码以明文形式存储。否则的话,需要使用`PasswordEncoder`接口的实现来加密密码,不过这样做对于一个小型项目并不是必要的。 Spring Security 提供了默认的 login form 表单,不过在以下情况下,可能需要重写默认的登录页面模板。 默认模板位于`/org/springframework/security/web/server/ui/login/LoginPageGeneratingWebFilter.DEFAULT_LOGIN_PAGE_TEMPLATE`。 利用 FreeMarker 模板引擎可以重写该模板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值