spring重定向

15 篇文章 1 订阅

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

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

  <welcome-file-list>
    <welcome-file>login.html</welcome-file>
  </welcome-file-list>

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去

<welcome-file-list>
    <welcome-file>login1.html</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>404</error-code>
    <location>/login1.html</location>
  </error-page>

原来的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的重定向原理没搞懂,暂时先这样用用了





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值