Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 383 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle requests that includes forward slashes (/)?

#1
I need to handle requests as following:

[To see links please register here]

(does not work)

[To see links please register here]

(works)

[To see links please register here]

(works)

It should accept any sort of character from the value that is located between `www.example.com/show/` and `?`. Please note the value that would be located there would be a single value not name of an action.

For example: `/show/abcd/efg` and `/show/lkikf?name=Jack` in which the first request should redirect user to the page `abcd/efg` (because thats a name) and the second one should redirect user to the page `lkikf` along with value of parameter name.

I have following controller to handle it but the issue is when I have / in the address the controller is unable to handle it.

@RequestMapping(value = "/{mystring:.*}", method = RequestMethod.GET)
public String handleReqShow(
@PathVariable String mystring,
@RequestParam(required = false) String name,
@RequestParam(required = false) String family, Model model) {


I used following regex which did not work.

/^[ A-Za-z0-9_@./#&+-]*$/
Reply

#2
The first one is not working because you are trying to handle an entirely new URL which is not actually mapped your controller.

[To see links please register here]

(does not work)

The correct mapping for the above URL could be like the below code.

@RequestMapping(value = {"/{mystring:.*}" , "/{mystring:.*}/{mystring2:.*}"}, method = RequestMethod.GET)
public String handleReqShow(
@PathVariable String mystring,
@PathVariable String mystring2,
@RequestParam(required = false) String name,
@RequestParam(required = false) String family, Model model) {

I have tried the similar concept when my one controller is used to handle multiple types of request.
Reply

#3
You have to create two methods then one having the `@RequestMapping(value = { "/{string:.+}" })` annotation and the other having `@RequestMapping(value = { "/{string:.+}", "/{string:.+}/{mystring:.+}" })` and then act accordingly in each, because you can't have optional [path variables.][1]

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/show")
public class HelloController {

@RequestMapping(value = { "/{string:.+}" })
public String handleReqShow(@PathVariable String string,
@RequestParam(required = false) String name,
@RequestParam(required = false) String family, Model model) {
System.out.println(string);
model.addAttribute("message", "I am called!");
return "hello";
}

@RequestMapping(value = { "/{string:.+}", "/{string:.+}/{mystring:.+}" })
public String whatever(@PathVariable String string,
@PathVariable String mystring,
@RequestParam(required = false) String name,
@RequestParam(required = false) String family, Model model) {
System.out.println(string);
System.out.println(mystring);
model.addAttribute("message", "I am called!");
return "hello";
}
}


[1]:

[To see links please register here]

Reply

#4
Try escaping forward slash.
Regex: `/^[ A-Za-z0-9_@.\/#&+-]*$/`
Reply

#5
The default Spring MVC path mapper uses the `/` as a delimiter for path variables, no matter what.

The proper way to handle this request would be to write a custom path mapper, that would change this logic for the particular handler method and delegate to default for other handler methods.

However, if you know the max possible count of slashes in your value, you can in fact write a handler that accepts optional path variables, and than in the method itself, assemble the value from path variable parts, here is an example that would work for max one slash, you can easily extend it to three or four

@RequestMapping(value = {"/{part1}", "/{part1}/{part2}"}, method = RequestMethod.GET)
public String handleReqShow(
@PathVariable Map<String, String> pathVariables,
@RequestParam(required = false) String name,
@RequestParam(required = false) String family, Model model) {
String yourValue = "";
if (pathVariables.containsKey("part1")) {
String part = pathVariables.get("part1");
yourValue += " " + part;
}
if (pathVariables.containsKey("part2")) {
String part = pathVariables.get("part2");
yourValue += " /" + part;
}
// do your stuff

}

You can catch all the path variables inside the map, the map `@PathVariable Map<String, String> pathVariables`, but the downside is that the static part of the mapping has to enumarate all the possible variations
Reply

#6
Another way I do is:

@RequestMapping(value = "test_handler/**", method = RequestMethod.GET)

...and your test handler can be "/test_hanlder/a/b/c" and you will get the whole value using following mechanism.

requestedUri = (String)
request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
Reply

#7
You could encode slashes on UI with %2f: ```http://www.example.com/show/abcd%2fefg?name=alex&family=moore```.
Now you should configure Spring to handle slashes. Simple config example:

@RestController
public class TestController {

@GetMapping("{testId:.+}")
public String test(@PathVariable String testId) {
return testId;
}


@GetMapping("{testId:.+}/test/{messageId}")
public String test2(@PathVariable String testId, @PathVariable String messageId) {
return testId + " " + messageId;
}

//Only if using Spring Security
@Configuration
public static class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
DefaultHttpFirewall firewall = new DefaultHttpFirewall();
firewall.setAllowUrlEncodedSlash(true);
return firewall;
}
@Override
public void configure(WebSecurity web) throws Exception {
web.httpFirewall(allowUrlEncodedSlashHttpFirewall());
}
}


@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public static class SpringMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setUrlDecode(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}

}
Reply

#8
You can define rules to avoid that

<!-- language: lang-jsp -->

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

rules.xml add this to your WEB-INF

<!-- language: lang-jsp -->

<urlrewrite>
<rule>
<from>^/(10\..*)$</from> <!-- tweak this rule to meet your needs -->
<to>/Show?temp=$1</to>
</rule>
</urlrewrite>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through