域名绑定在nginx上反代tomcat之shiro过滤器上下文处理

5 篇文章 0 订阅
2 篇文章 0 订阅

 域名绑在nginx上,又反代了tomcat

又想以域名直接访问web应用

shiro在处理过程中,用了如下代码

public final void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        StringBuilder targetUrl = new StringBuilder();
        if(this.contextRelative && this.getUrl().startsWith("/")) {
            targetUrl.append(request.getContextPath());
        }

        targetUrl.append(this.getUrl());
        this.appendQueryProperties(targetUrl, model, this.encodingScheme);
        this.sendRedirect(request, response, targetUrl.toString(), this.http10Compatible);
    }

默认登录拦截contextRelatie=true

导致tomcat每次取出了应用名xxx

但需求是通过域名直接访问应用,且域名不能绑tomcat那台机器,只能绑在nginx上。

nginx反代配置则必须如下(IP:PORT请自定义)

location ^~ / {
    proxy_pass http://10.10.10.10:8080/xxx/;
    proxy_set_header Host $host:80;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For  $remote_addr;
    proxy_set_header Via "nginx";
    break;
}

 那么由于shiro上面一段代码,会跳转到

http://www.域名.com/xxx/login.html

实际地址变成了http://10.10.10.10:8080/xxx/xxx/login.html

多了一层xxx

怎么解决这个问题呢,解决方案是重写Filter,以下是我重写的一个Filter,仅供参考


import org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by jdkleo on 2017/2/10.
 */
public class PanyAuthcFilter extends PassThruAuthenticationFilter {
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        if(this.isLoginRequest(request, response)) {
            return true;
        } else {
            this.saveRequest(request);
            String via = ((HttpServletRequest)request).getHeader("Via");
            if (via != null && "nginx".equals(via.toLowerCase())){//如果是nginx,就不需要contextRelative上下文关系
                WebUtils.issueRedirect(request, response, this.getLoginUrl(),null,false);
            }else {
                WebUtils.issueRedirect(request, response, this.getLoginUrl());
            }
            return false;
        }
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值