shIro跳转方法https服务变为http请求

本文探讨了在使用Shiro框架时,HTTPS环境下会话过期后重定向至登录页面出现MixedContent错误的问题及解决方案。指出Shiro默认设置redirectHttp10Compatible为true,导致HTTPS转化为HTTP,通过修改此属性为false解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用shiro框架时,当session会话过期后返回登陆页面报错,
错误为:Mixed Content: The page at ‘https://XXX’ was loaded over HTTPS, but requested an insecure
这是因为
原因是因为需要https协议而shiro帮我们转化为了http协议.
网上的一种方法是在视图层将redirectHttp10Compatible设置为false

<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
	<property name="contentType" value="text/html" />
	<property name="prefix" value="/" />
	<property name="suffix" value=".jsp" />
	<property name="redirectHttp10Compatible" value="false" />
</bean>

但是使用在shiro里没有用

因为shiro将 redirectHttp10Compatible 设置为默认为true,所以不管你是否在视图中设置了redirectHttp10Compatible属性,他都一定为true。

看下源码就知道了,在shiro的有WebUtils工具类中有这样的方法

    public static void issueRedirect(ServletRequest request, ServletResponse response, String url, Map queryParams, boolean contextRelative, boolean http10Compatible) throws IOException {
        RedirectView view = new RedirectView(url, contextRelative, http10Compatible);
        view.renderMergedOutputModel(queryParams, toHttp(request), toHttp(response));
    }

    public static void issueRedirect(ServletRequest request, ServletResponse response, String url) throws IOException {
        issueRedirect(request, response, url, (Map)null, true, true);
    }

可以看到shiro在调用issueRedirect方法时会调用issueRedirect方法,
issueRedirect(request, response, url, (Map)null, true, true);
http10Compatible已经默认设置为true.
所以解决起来也简单,我们直接调用issueRedirect方法,将issueRedirect设置为false

        //如果被踢出了,直接退出,重定向到踢出后的地址
        if (session.getAttribute("kickout") != null) {
            //会话被踢出了
            try {
                subject.logout();
            } catch (Exception e) { //ignore
            }
            saveRequest(request);
            WebUtils.issueRedirect(request, response, kickoutUrl,null,true,false);
            return false;
        }

在此记录一下,希望能帮助到需要的朋友.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值