java ssl通道,Spring MVC框架 - SSL之单一模式与双通道模式的介绍

一、单一模式

如果你希望只有安全的https 通道而没有http 通道的话,那么这非常容易实现:server.port = 8443

server.ssl.key-store = classpath:tomcat.keystore

server.ssl.key-store-password = password

server.ssl.key-password = password2

不要将密码放到仓库中,要使用“${}”符号来导入环境变量。

二、双通道模式

如果你想要在应用中同时使用http 和https 通道的话,那么需要在应用中增加如下的配置:@Configuration

public class SslConfig {

@Bean

public EmbeddedServletContainerFactory servletContainer() throws IOException {

TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

tomcat.addAdditionalTomcatConnectors(createSslConnector());

return tomcat;

}

private Connector createSslConnector() throws IOException {

Connector connector = new Connector(Http11NioProtocol.class.getName());

Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();

connector.setPort(8443);

connector.setSecure(true);

connector.setScheme("https");

protocol.setSSLEnabled(true);

protocol.setKeyAlias("masterspringmvc");

protocol.setKeystorePass("password");

protocol.setKeyPass("password2");

protocol.setKeystoreFile(new ClassPathResource("tomcat.keystore").getFile().getAbsolutePath());

protocol.setSslProtocol("TLS");

return connector;

}

}

这样的话,除了8080 端口以外,还会加载前文生成的keystore 并在8443 创建另外一个通道。

我们可以通过如下的配置,使用Spring Security 自动将连接从http 重定向到https:@Configuration

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

/* rest of the configuration */

http.requiresChannel().anyRequest().requiresSecure().and();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值