問題:
最近遇到shiro校驗框架成功後不進行自動跳轉到指定頁面;
相關配置如下:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/account/user/current" />
<property name="filterChainDefinitions">
<value>
/login = authc
/test = authc
/logout = logout
/static/** = anon
/** = authc
</value>
</property>
</bean>
我使用的是驗證過濾器是authc;邏輯上只要登錄成功即可跳轉至“/account/user/current”實際上不行;
主要原因是successUrl配置只是做爲一種附加配置,只有session中沒有用戶請求地址時纔會使用successUrl;
系統默認登錄成功後首次跳轉的地址爲,訪問系統時初次使用地址,例:如果用戶首次訪問的是http://****/aa/aa.html;
那麼shiro校驗成功後跳轉的地址即可http://****/aa/aa.html; 否則shiro將跳轉到默認虛擬路徑:“/”;
對於應用中,如果沒有註冊/路徑,則默認使用web.xml中配置的<welcom-list>配置;
shiro源碼邏輯如下:
String successUrl = null;
boolean contextRelative = true;
SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
if (savedRequest != null && savedRequest.getMethod().equalsIgnoreCase(AccessControlFilter.GET_METHOD)) {
successUrl = savedRequest.getRequestUrl();
contextRelative = false;
}
if (successUrl == null) {
successUrl = fallbackUrl;
}
if (successUrl == null) {
throw new IllegalStateException("Success URL not available via saved request or via the " +
"successUrlFallback method parameter. One of these must be non-null for " +
"issueSucce***edirect() to work.");
}
WebUtils.issueRedirect(request, response, successUrl, null, contextRelative);
上述代碼紅色部分表明,對於成功後地址指向,會依據會話SESSION中保留的請求地址;
解決方法:
hiro跳不跳我不再搭理它了,而是在springMVC的配置文件中加上
<mvc:view-controller path="/" view-name="redirect:/welcome"/>
意思是訪問"/"重定向到welcome