怎么解決shiro會話超時302問題

本篇內(nèi)容介紹了“怎么解決shiro會話超時302問題”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

10余年的圖們網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整圖們建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“圖們網(wǎng)站設(shè)計”,“圖們網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

  產(chǎn)生異常的情況:nginx配置了https,但是nginx轉(zhuǎn)發(fā)請求到web應(yīng)用走的http,會話超時,shiro會重定向到登錄頁,這時重定向的是http地址,比如http://xxxxx/login/index ,瀏覽器會阻止這樣的請求(從https頁面發(fā)起http請求是非法的)。

  1. 重寫shiro的FormAuthenticationFilter

public class MyShiroAuthcFilter extends FormAuthenticationFilter {

	public MyShiroAuthcFilter(String loginUrl) {
		super();
		setLoginUrl(loginUrl);
	}

	@Override
	protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
		if (isLoginRequest(request, response)) {
			return super.onAccessDenied(request, response);
		} else {
			if (isAjax((HttpServletRequest) request)) { // 處理ajax請求
				HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
				httpServletResponse.addHeader("REQUIRE_AUTH", "true"); // ajax全局設(shè)置中有用
				httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value()); // 改變302狀態(tài)碼
			} else {
				saveRequest(request);
				request.getRequestDispatcher(getLoginUrl()).forward(request, response);
				// 由于是nginx轉(zhuǎn)發(fā)的,redirect 302會重定向到http協(xié)議,不是瀏覽器期望的https
				// saveRequestAndRedirectToLogin(request, response);
			}
			return false;
		}
	}

	private boolean isAjax(HttpServletRequest request) {
		String requestedWithHeader = request.getHeader("X-Requested-With");
		return "XMLHttpRequest".equals(requestedWithHeader);
	}
}

shiro的filter配置

@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
	String loginUrl = "/login/index";
	ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();

	Map<String, Filter> filters = shiroFilter.getFilters();
	filters.put("anon", new AnonymousFilter());
	filters.put("authc", new MyShiroAuthcFilter(loginUrl));

	Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
	filterChainDefinitionMap.put("/supervisor/**", "authc");
	filterChainDefinitionMap.put("/**", "anon");

	shiroFilter.setSecurityManager(securityManager);
	shiroFilter.setLoginUrl(loginUrl);
	shiroFilter.setUnauthorizedUrl("/login/unauthorized");
	shiroFilter.setFilters(filters);
	shiroFilter.setFilterChainDefinitionMap(filterChainDefinitionMap);

	return shiroFilter;
}
  1. ajax全局設(shè)置

$.ajaxSetup({
    complete: function (xhr, status) {
    	if (xhr.getResponseHeader('REQUIRE_AUTH') == 'true') {
    		alert("未登錄或登錄超時!");
    		window.top.location.href = getHost() + '/login/index';
    		return;
    	}
	}
});
  1. /login/index頁面處理

<script type="text/javascript">
	// 使登錄頁出現(xiàn)在“頂級”窗口,調(diào)整瀏覽器的url地址,上面的filter是forward到登錄頁的
	if(window.top != window.self || location.pathname != '/login/index') {
	window.top.location = getHost() + '/login/index';
	}
</script>

“怎么解決shiro會話超時302問題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

本文名稱:怎么解決shiro會話超時302問題
鏈接地址:http://bm7419.com/article12/jddegc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站排名、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護、移動網(wǎng)站建設(shè)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司