我是靠谱客的博主 尊敬汽车,最近开发中收集的这篇文章主要介绍shiro权限控制登陆成功页面跳转问题,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

在开发中使用了shiro进行权限控制,遇到一个页面跳转问题:当用户账号密码都正确的时候并没有跳转到登陆成功页面。

在shiroFilter过滤器中配置了登陆成功路径没有反应。注意:我使用的是表单验证。

<!-- 基于Form表单的身份验证过滤器 -->
<bean id="formAuthenticationFilter" class="com.youyuan.shiro.filter.WithNavibarFormAuthenticationFilter">
<property name="usernameParam" value="username"/>
<property name="passwordParam" value="password"/>
<property name="rememberMeParam" value="rememberMe"/>
<property name="loginUrl" value="/user/login.html"/>
<property name="successUrl" value="/nons/index.html"/>
</bean>
<!-- Shiro的Web过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/user/login.html"/>
<!--<property name="unauthorizedUrl" value="/unauthorized.jsp"/>-->
<property name="filters">
<util:map>
<entry key="authc" value-ref="formAuthenticationFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/=authc
/nons/noAuth.html=anon
/static/**=anon<!-- 静态资源文件访问 -->
/index.html=authc
/user/logout.html=authc
/nons/index.html=authc
/admin/**/**.html=authc
/admin/index=authc
/**=user
</value>
</property>
</bean>

可能项目部署不一样,我登陆请求成功后(这时只是账号密码正确的请求,页面并不直接跳转到成功页面)是先找到后台的方法判断,通过请求时返回登陆成功的页面。

@Controller
@RequestMapping("/nons")
public class IndexController {
@Resource
private UserResourceService userResourceService;
@RequestMapping(value="/index")
public ModelAndView index(){
ModelAndView mov=new ModelAndView("/admin/index");
return mov;
}
}

 

通过代码查看和源码分析在onLoginSuccess找到了问题。

@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
ServletResponse response) throws Exception {
HttpServletRequest httpReq=(HttpServletRequest)request;
String userName=(String) SecurityUtils.getSubject().getPrincipal();
System.out.println("onLoginSuccess==="+userName);
WebUtils.issueRedirect(request,response,getSuccessUrl());
//return super.onLoginSuccess(token, subject, request, response);
return false;
}

这个方法执行之后会将请求过来的地址保存到successUrl当中这样就覆盖了我们配置中需要跳转的地址。   

在源码中首先通过SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);

从session中获取到第一次请求时的地址,然后通过successUrl = savedRequest.getRequestUrl();将FormAuthenticationFilter配置的successUrl值覆盖掉,所以shiro默认跳转到了首次请求的url了。

所以我们直接重写使用重定向跳转。 WebUtils.issueRedirect(request,response,getSuccessUrl());

也许你的页面都在webapp下,没有像我这样多次跳转可能直接页面跳转就成功了,具体测试没做。

 

 

 

 

 

 

 

 

最后

以上就是尊敬汽车为你收集整理的shiro权限控制登陆成功页面跳转问题的全部内容,希望文章能够帮你解决shiro权限控制登陆成功页面跳转问题所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部