简体   繁体   English

外部tomcat中spring的SSL配置

[英]SSL Configuration for spring in External tomcat

I am new for spring.我是春天的新人。 I want to Configure ssl certificate for Spring application.我想为 Spring 应用程序配置 ssl 证书。 Currently i am using external tomcat9 and configured its 8443 port(which is working fine):目前我正在使用外部 tomcat9 并配置了它的 8443 端口(工作正常):

server.xml of tomcat: 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>

In application:在应用中:

web.xml网页.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>

And setup basic authentication using spring security like: MyBasicAuthEntryPoint:并使用 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();
    }
 }

And BasicAuthService:和 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() {

        }
    }

But when I try to access one of api, I am getting 404. For Testing purpose added index.jsp with some sample contents.但是当我尝试访问其中一个 api 时,我得到了 404。为了测试目的,添加了 index.jsp 和一些示例内容。 It's working fine after deploying.部署后运行正常。

So what changes need to be done in configuration to enable SSL in spring.那么在配置中需要做哪些改动才能在spring中启用SSL。 I cant mention keystore path in spring configuration since I have already configured it in tomcat.我不能在 spring 配置中提到密钥库路径,因为我已经在 tomcat 中配置了它。 Also I am using Jersey for api request.此外,我正在使用 Jersey 进行 api 请求。

I am using Spring Boot 2 with an external tomcat 9 running on Ubuntu 18.04我正在将 Spring Boot 2 与在 Ubuntu 18.04 上运行的外部 tomcat 9 一起使用

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

Store the p12 file on your apache server.将 p12 文件存储在您的 apache 服务器上。 I stored it in /opt/tomcat我将它存储在 /opt/tomcat

Edit your apache server.xml file to have something like this.编辑您的 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"/>

Restart Tomcat重启Tomcat

Done!完毕!

It is very quick once you know what the steps are!一旦你知道步骤是什么,它是非常快的! Just don't forget anything.只是不要忘记任何事情。

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

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