通过传递无效的URL来成功显示数据(在URL中添加反斜杠)

时间:2019-03-01 08:54:35

标签: java rest spring-boot

即使我在URL中添加额外的反斜杠(),我也能成功获得JSON响应,如下所示:

https://localhost:8080//////\/\\\\\/rest//v1///kems///stats?btv=doerrja

JSON响应:

[
    {
        "ID": "636803914206857000",
        "Rank": "2.401",
        "Artifact_State": "In Arbeit",
        "Count": "10"
    },
    {
        "ID": "636803920812123000",
        "Rank": "1.372",
        "Artifact_State": "Keine Änderung möglich",
        "Count": "1"
    }
]

如何用Java处理这种情况?

2 个答案:

答案 0 :(得分:0)

snapshot of the server

从我看到的

我认为它一次消耗了一个'/'并将其余的当作参数,然后再次递归地将每个'/'视为一条单独的路径,最终达到了您所拥有的实际值试图通过

答案 1 :(得分:0)

最后,下面的解决方案对我有用:

pom.xml:

<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>

SecurityConfig.java

package com.Application;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    /* 
     * Method allow to access any resource without any authentication
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll();
    }
}

UrlFilter.java

package com.component;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.daimler.datalayer.apistreamintegration.exception.BadRequest;

@Component
public class UrlFilter implements Filter {

    private static final Logger LOGGER = LoggerFactory.getLogger(UrlFilter.class);

    @Override
    public void init(FilterConfig filterChain) throws ServletException {

    }


    /* 
     * filter call to check if any double forward slash(//) present in URI before invoking Controller class 
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {

        LOGGER.info("Filtering the url");
        if (!(request instanceof HttpServletRequest)) {
            return;
        }
        String requestURI = ((HttpServletRequest) request).getRequestURI();
        boolean isInvalidUrl = requestURI.contains("//");
        if (isInvalidUrl) {
            throw new BadRequest("The request was rejected because the URL was not normalized.");
        }
        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}

BadRequest.java

package com.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class BadRequest extends RuntimeException {

  private static final long serialVersionUID = 1L;


  public BadRequest(String message) {
    super(message);
  }


  public BadRequest(String message, Throwable cause) {
    super(message, cause);
  }

}