shiro权限控制登陆成功页面跳转问题

5 篇文章 0 订阅
1 篇文章 0 订阅

在开发中使用了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下,没有像我这样多次跳转可能直接页面跳转就成功了,具体测试没做。

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值