스프링 MVC의 컨트롤러 작업에서 외부 URL로 리디렉션
다음 코드가 사용자를 프로젝트 내의 URL로 리디렉션하고 있음을 알았다.
@RequestMapping(method = RequestMethod.POST)
public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = "yahoo.com";
return "redirect:" + redirectUrl;
}
반면에 다음은 의도한 대로 적절하게 리디렉션되지만 http:// 또는 https:///가 필요하다.
@RequestMapping(method = RequestMethod.POST)
public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = "http://www.yahoo.com";
return "redirect:" + redirectUrl;
}
리디렉션이 유효한 프로토콜이 있는지 여부에 관계없이 지정된 URL로 항상 리디렉션하고 보기로 리디렉션하지 않도록 하십시오.내가 어떻게 그럴 수 있을까?
고마워요.
두 가지 방법으로 할 수 있다.
첫 번째:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", projectUrl);
httpServletResponse.setStatus(302);
}
두 번째:
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}
당신은 그것을 사용할 수 있다.RedirectView
JavaDoc에서 복사됨:
절대, 컨텍스트 상대 또는 현재 요청 상대 URL로 리디렉션되는 보기
예:
@RequestMapping("/to-be-redirected")
public RedirectView localRedirect() {
RedirectView redirectView = new RedirectView();
redirectView.setUrl("http://www.yahoo.com");
return redirectView;
}
또한 a를 사용할 수 있다.ResponseEntity
, 예:
@RequestMapping("/to-be-redirected")
public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException {
URI yahoo = new URI("http://www.yahoo.com");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(yahoo);
return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER);
}
그리고 물론, 반품.redirect:http://www.yahoo.com
남들이 말한 바와 같이
LurlBasedViewResolver 및 RedirectView의 실제 구현을 살펴보면 리디렉션 대상이 /로 시작하는 경우 리디렉션은 항상 contextResolver가 된다.따라서 //yahoo.com/path/to/resource을 전송하는 것도 프로토콜 관련 리디렉션을 얻는 데 도움이 되지 않을 것이다.
그래서 여러분이 노력하고 있는 것을 성취하기 위해 다음과 같은 것을 할 수 있다.
@RequestMapping(method = RequestMethod.POST)
public String processForm(HttpServletRequest request, LoginForm loginForm,
BindingResult result, ModelMap model)
{
String redirectUrl = request.getScheme() + "://www.yahoo.com";
return "redirect:" + redirectUrl;
}
당신은 이것을 매우 간결한 방법으로 할 수 있다.ResponseEntity
다음과 같은 경우:
@GetMapping
ResponseEntity<Void> redirect() {
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create("http://www.yahoo.com"))
.build();
}
또 다른 방법은 단지 그것을 사용하는 것이다.sendRedirect
방법:
@RequestMapping(
value = "/",
method = RequestMethod.GET)
public void redirectToTwitter(HttpServletResponse httpServletResponse) throws IOException {
httpServletResponse.sendRedirect("https://twitter.com");
}
내게는 잘 먹힌다.
@RequestMapping (value = "/{id}", method = RequestMethod.GET)
public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException {
URI uri = new URI("http://www.google.com");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setLocation(uri);
return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER);
}
외부 URL의 경우 리디렉션 URL로 "http://www.yahoo.com"을 사용해야 한다.
이는 리디렉션: 스프링 참조 문서의 접두사에 설명되어 있다.
리디렉션:/myapp/일부/일부/일부
이름이 다음과 같은 동안 현재 서블릿 컨텍스트를 기준으로 리디렉션됨
절대 URL로 리디렉션됨
contextRelated 매개 변수를 제공할 수 있는 RedirectView를 시도하셨습니까?
이것은 나에게 효과가 있으며, "비행 전 요청에 대한 응답은 접근 제어 검사를 통과하지 못한다..." 문제를 해결했다.
제어기
RedirectView doRedirect(HttpServletRequest request){
String orgUrl = request.getRequestURL()
String redirectUrl = orgUrl.replaceAll(".*/test/","http://xxxx.com/test/")
RedirectView redirectView = new RedirectView()
redirectView.setUrl(redirectUrl)
redirectView.setStatusCode(HttpStatus.TEMPORARY_REDIRECT)
return redirectView
}
그리고 증권화를 가능하게 한다.
@EnableWebSecurity
class SecurityConfigurer extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
}
}
요컨대"redirect://yahoo.com"
에게 빌려줄 것이다yahoo.com
.
로서"redirect:yahoo.com"
너에게 빌려줄 것이다your-context/yahoo.com
예:localhost:8080/yahoo.com
'programing' 카테고리의 다른 글
java.lang은 무엇을 하는가.스레드.인터럽트()가? (0) | 2022.05.19 |
---|---|
정렬 가능에서 비활성화 옵션을 사용하는 방법부에 프로젝트에서 JS? (0) | 2022.05.19 |
Vuex / Vue를 사용하여 비동기 데이터 검색을 처리하는 방법 (0) | 2022.05.19 |
Vuejs에서 DOM 요소 사이의 거리를 계산하는 가장 좋은 방법은 무엇인가? (0) | 2022.05.19 |
Java에서 인터페이스 이름 지정 (0) | 2022.05.19 |