vendredi 11 septembre 2015

Spring cannot create filter on online server only (works fine on the localhost)

My Spring application deploys and works fine on my localhost but fails on the online server with the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private it.kahoot.robot.rest.filter.SimpleCORSFilter it.kahoot.robot.rest.config.WebSecurityConfiguration.simpleCORSFilter; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'simpleCORSFilter': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanP

The filter class is:

@Component
public class SimpleCORSFilter implements Filter {

    private static Logger logger = LoggerFactory.getLogger(SimpleCORSFilter.class);

    private static final String ORIGIN = "Origin";
    private static final String OPTIONS = "OPTIONS";
    private static final String OK = "OK";

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

        if (httpServletRequest.getHeader(ORIGIN) != null) {
            String origin = httpServletRequest.getHeader(ORIGIN);
            httpServletResponse.setHeader("Access-Control-Allow-Origin", origin);
            httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
            httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
            httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
            httpServletResponse.setHeader("Access-Control-Allow-Headers", "Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,Content-Disposition,Content-Length,"+CommonConstants.EXPORT_FILENAME_HEADER_NAME+","+CommonConstants.AUTH_HEADER_NAME);
            // Allow more than the 6 default headers to be returned, as the content length is required for a download file request to get the file size 
            httpServletResponse.setHeader("Access-Control-Expose-Headers", "Accept-Language,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization,Content-Disposition,Content-Length,"+CommonConstants.EXPORT_FILENAME_HEADER_NAME+","+CommonConstants.AUTH_HEADER_NAME);
        }

        if (httpServletRequest.getMethod().equals(OPTIONS)) {
            try {
                httpServletResponse.getWriter().print(OK);
                httpServletResponse.getWriter().flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }       
    }

    public void init(FilterConfig filterConfig) {
    }

    public void destroy() {
    }

}

Again, it works just fine on my localhost.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire