怎么解决shiro会话超时302问题

本篇内容介绍了“怎么解决shiro会话超时302问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

10余年的图们网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整图们建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“图们网站设计”,“图们网站推广”以来,每个客户项目都认真落实执行。

  产生异常的情况:nginx配置了https,但是nginx转发请求到web应用走的http,会话超时,shiro会重定向到登录页,这时重定向的是http地址,比如http://xxxxx/login/index ,浏览器会阻止这样的请求(从https页面发起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全局设置中有用
				httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value()); // 改变302状态码
			} else {
				saveRequest(request);
				request.getRequestDispatcher(getLoginUrl()).forward(request, response);
				// 由于是nginx转发的,redirect 302会重定向到http协议,不是浏览器期望的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全局设置

$.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">
	// 使登录页出现在“顶级”窗口,调整浏览器的url地址,上面的filter是forward到登录页的
	if(window.top != window.self || location.pathname != '/login/index') {
	window.top.location = getHost() + '/login/index';
	}
</script>

“怎么解决shiro会话超时302问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

本文名称:怎么解决shiro会话超时302问题
链接地址:https://www.cdcxhl.com/article12/jddegc.html

成都网站建设公司_创新互联,为您提供网站收录网站排名外贸网站建设网站维护移动网站建设微信小程序

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司