How to force Spring MVC controllers use SSL with Spring security?

Viewed 6745

I am having problems forcing SSL for Spring MVC REST resources. I don't need authentication since it is handled via the security token passed in HTTP header, all I need is to disallow using http. Since I am already using Spring security in other parts of my project I would rather continue using it instead of configuring SSL in the web server. When I am trying to connect to a resource I am getting "error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol" which I assume means that the server is trying to respond not via SSL.

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Mongoose Rest Server</display-name>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/application-security.xml   
        </param-value>
    </context-param>    
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>mongoose</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mongoose</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>    
</web-app>

applicaton-security.xml

    <beans
xmlns="http://www.springframework.org/schema/beans" 
    xmlns:security="http://www.springframework.org/schema/security"  
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"      
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-3.2.xsd
                    http://www.springframework.org/schema/util 
                    http://www.springframework.org/schema/util/spring-util-3.2.xsd                    
                    http://www.springframework.org/schema/security   
                    http://www.springframework.org/schema/security/spring-security-3.1.xsd">  

    <security:http auto-config="true" use-expressions="true">
       <security:intercept-url pattern="/**" requires-channel="https" />
    </security:http>        
    <security:authentication-manager>
    </security:authentication-manager>
</beans>

rest controller

@Controller
@RequestMapping("/mongoose")
public class HarmonyRestController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    public HarmonyRestController() {
    }

    @RequestMapping(value = "policies", method = RequestMethod.GET, produces = "application/json")
    public
    @ResponseBody
    String requestPolicies()  {
        return "test...";
    }
}

I also tried to use port redirect in security config like

 <security:http auto-config="true" use-expressions="true">/
   <security:intercept-url pattern="/**" requires-channel="https" security="none"/>
    <security:port-mappings>
        <security:port-mapping http="8080" https="8443" />
    </security:port-mappings> 
  </security:http>    

and bind Jetty to 8443 but it did not help. Every time I am trying to connect to /mongoose/policies resource via https I am getting curl: (35) error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

Perhaps my problem has something to do with isSecure property of the servlet Spring MVC is using is set to false?

0 Answers
Related