我有一个返回 JSP 页面的 Spring Boot Controller 方法。我有 Spring Security 实现来授权每个请求。这是我的 Controller ,单击图标后会返回主页。
@RequestMapping("/home")
public String getDataForHome(HttpServletRequest request, Model model)
{
return "/dashboard";
}
现在返回首页的图标是-
<img src="<%=contextPath%>/assets/img/logo.png" onclick="handleClick();">
handleClick函数的实现,其中ajax调用授权 Controller 并在ajax成功时重定向到主页 -
function handleClick(){
$.ajax({
url: "<%=contextPath%>/home",
headers: {
'Authorization':'${sessionScope.token}'
},
type: 'GET',
success: function(data){
window.location='<%= contextPath %>/home';
}
error:function(e){
console.log(e);
}
});
}
现在单击图像会显示该请求需要完整的身份验证。
最佳答案
如果您配置了view resolver使用返回“仪表板”
(不带/)
如果您尝试将请求转发到请求中的另一个 Controller 句柄,请使用return "forward:/dashboard"
。
如果您想重定向到新请求,请使用返回“redirect:/dashboard”
关于java - 如何重定向和授权在ajax成功中返回jsp页面的同一url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59100523/