Springboot集成Shiro重定向login.jsp
问题
在简单配置 shiro 相关配置后,浏览器请求/index进入到login.html页面,打开F12后发现console中报错,多次重定向login.js页面。但login.jsp文件不存在,而且springboot默认前台模板是thymleaf,不支持jsp。
问题查找
想着可能是自己在配置时出现了问题,多次查对外加重建项目,将问题点锁定到shiro配置中。通过百度找到loginUrl,这个问题字段。
解析
- loginUrl 字段:用于认证用户时,重定向的路径。认证失败,session过期后走的路径。
- AccessControlFilter 类中,可以看到重定向方法
/**
* Convenience method for subclasses that merely acquires the {@link #getLoginUrl() getLoginUrl} and redirects
* the request to that url.
* <p/>
* <b>N.B.</b> If you want to issue a redirect with the intention of allowing the user to then return to their
* originally requested URL, don't use this method directly. Instead you should call
* {@link #saveRequestAndRedirectToLogin(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
* saveRequestAndRedirectToLogin(request,response)}, which will save the current request state so that it can
* be reconstructed and re-used after a successful login.
*
* @param request the incoming <code>ServletRequest</code>
* @param response the outgoing <code>ServletResponse</code>
* @throws IOException if an error occurs.
*/
protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
String loginUrl = getLoginUrl();
WebUtils.issueRedirect(request, response, loginUrl);
}
-
根据文档提示,如果session 等情况需要重新登录,登录成功后,会继续之前的操作。

-
所以,我们怎么改?
shiro.loginUril=/index
第一点: 实际loginUril 的请求路径是ip:port/projectname/loginUrl。但有时登录页面文件存在在无法访问的文件夹内。简单改为/login.html 不可取。
第二点: loginUrl请求逻辑,是当前请求是否认证成功。即便设置了chainDefinition.addPathDefinition("/index", "anon");,请求可以进入到 controller,但数据返回时,同样会认证用户,失败后也会走loginUrl。
第三点: 配置了/index,相当于配置了系统的登录入口。
另辟蹊径:
可以重写 redirectToLogin()来达到,不重定向的效果。百度查查吧,有文章,就不加连接了。

浙公网安备 33010602011771号