我使用 Spring MVC 4 和 Tomcat 7,并且我已经创建了一个自签名证书。 Spring MVC 只是 REST 服务的服务器后端。但是我如何使用 java config 激活和配置 https 呢?有人能给我举个例子吗?我希望只有我的登录休息服务使用 https 运行。
最佳答案
在 Tomcat 上,您必须配置与此类似的 SSL 连接器:(更多详细信息:http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html)
<Connector
protocol="HTTP/1.1"
port="8443" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="${user.home}/.keystore" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS"/>
然后您可以继续使用 JavaConfig 或 XML 以正常方式设置 Spring Security。如果使用 JavaConfig,您将需要表单使用 https,其余表单则需要 http,因此配置将与此类似:
http
.authorizeUrls()
.antMatchers("/secure/**").hasRole("USER")
.antMatchers("/login").permitAll()
.and()
.requiresChannel()
.antMatchers("/login").requiresSecure()
.anyRequest().requiresInsecure()
关于java - Spring MVC 4 + Tomcat 7,如何用JavaConfig实现HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25394976/