繁体   English   中英

外部tomcat中spring的SSL配置

[英]SSL Configuration for spring in External tomcat

我是春天的新人。 我想为 Spring 应用程序配置 ssl 证书。 目前我正在使用外部 tomcat9 并配置了它的 8443 端口(工作正常):

tomcat的server.xml:

<Connector port="8443"
       protocol="org.apache.coyote.http11.Http11NioProtocol"
       maxThreads="150"
       SSLEnabled="true"
       scheme="https"
       secure="true" >
<SSLHostConfig>
    <Certificate certificateKeystoreFile="G:\apache-tomcat-9.0.39\conf\myexistingkey.jks"
                 certificateKeystorePassword="password"
                 certificateKeyAlias="tomcat"
                 type="RSA" />
</SSLHostConfig>

在应用中:

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <absolute-ordering />

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>securedapp</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>
</web-app>

并使用 Spring Security 设置基本身份验证,例如:MyBasicAuthEntryPoint:

public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

    @Override
    public void commence(final HttpServletRequest request,
                         final HttpServletResponse response,
                         final AuthenticationException authException) throws IOException {
        //Authentication failed, send error response.
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName() + "");

        PrintWriter writer = response.getWriter();
        writer.println("HTTP Status 401 : " + authException.getMessage());
    }

    @Override
    public void afterPropertiesSet()  {
        setRealmName("MY_TEST_REALM");
        super.afterPropertiesSet();
    }
 }

和 BasicAuthService:

@Configuration
@EnableWebSecurity
public class BasicAuthenticationService extends WebSecurityConfigurerAdapter {

    private static String REALM = "MY_TEST_REALM";

    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("admin")).roles("USER");
          System.out.println("configureGlobalSecurity");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        System.out.println("configure");
        MyBasicAuthenticationEntryPoint authenticationEntryPoint;

        authenticationEntryPoint = new MyBasicAuthenticationEntryPoint();
        http.csrf().disable()
                .authorizeRequests().anyRequest().authenticated().
                and().
                httpBasic()
                .authenticationEntryPoint(authenticationEntryPoint);

        http.addFilterAfter(new CustomFilter(),
                BasicAuthenticationFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        System.out.println("passwordEncoder");
        return new BCryptPasswordEncoder();
    }


    class CustomFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            filterChain.doFilter(servletRequest, servletResponse);
        }

        @Override
        public void destroy() {

        }
    }

但是当我尝试访问其中一个 api 时,我得到了 404。为了测试目的,添加了 index.jsp 和一些示例内容。 部署后运行正常。

那么在配置中需要做哪些改动才能在spring中启用SSL。 我不能在 spring 配置中提到密钥库路径,因为我已经在 tomcat 中配置了它。 此外,我正在使用 Jersey 进行 api 请求。

我正在将 Spring Boot 2 与在 Ubuntu 18.04 上运行的外部 tomcat 9 一起使用

获取 SSL 证书,我使用带有密码的 keytool 创建了一个自签名证书。 https://www.sslshopper.com/article-how-to-create-a-self-signed-certificate-using-java-keytool.html

将 p12 文件存储在您的 apache 服务器上。 我将它存储在 /opt/tomcat

编辑您的 apache server.xml 文件以获得类似的内容。

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" 
      maxThreads="150" scheme="https" secure="true"
      keystoreFile="/opt/tomcat/keystore.p12" keystorePass="password" 
      clientAuth="false" sslProtocol="TLS" sslEnabledProtocols="TLSv1.2,TLSv1.1"/>

重启Tomcat

完毕!

一旦你知道步骤是什么,它是非常快的! 只是不要忘记任何事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM