spring重定向

跟第三方.net对接他们要post到我们的登录页面

在WEB-INF/web.xml中配置了welcome-file-list,就会默认加载login.html做为登录页面,并且只支持get请求

 
    login.html
 

web.ignoring().antMatchers("/login.html")

  http.formLogin().loginProcessingUrl("/login")
  http.sessionManagement().and().invalidSessionUrl("/login.html");

现在需要改造login.html的请求,改为post和get同时支持

方案一:自己写/login.html的路由,将原来的html内容输出

@RequestMapping(value = "/login.html", method={RequestMethod.GET,RequestMethod.POST})

@ResponseBody
public String login() {
return "login.html的html内容粘贴过来";
}

方案二:自己写/login.html,重定向到login1.html,也就是说实际的登录页面交给login1.html去处理,出错也跳到login1.html去


    login1.html
 

 
    404
    /login1.html
 

原来的login.html静态页面更名为login1.html

web.ignoring().antMatchers("/login.html").antMatchers("/login1.html")

  http.sessionManagement().and().invalidSessionUrl("/login1.html");

下面用@PostMapping和@GetMapping,是@RequestMapping的另外一种写法

@PostMapping("/login.html")
public String loginPost() {
return "/";//或者直接 return "redirect:login1.html";
}
@GetMapping("/login.html")
public String loginGet() {
return "/";//或者直接 return "redirect:login1.html";
}

方案二最后会重定向到login1.html页面,return "redirect:login1.html";是直接重定向到了login1.html页面,return "/";是加载了welcome-file-list中的login1.html也会跳转到login1.html,如果是错误的404路径,也会跳转到login1.html中

Spring的重定向原理没搞懂,暂时先这样用用了





你可能感兴趣的:(java,spring)