博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cas HttpServletRequestWrapperFilter
阅读量:6828 次
发布时间:2019-06-26

本文共 3708 字,大约阅读时间需要 12 分钟。

HttpServletRequestWrapperFilter

作用其实很简单就是 在HttpServletRequest对象在包装一次,让其支持getUserPrincipal,getRemoteUser方法来获取登录的用户信息。

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {       //从session或者request中取得AttributePrincipal,其实Assertion的一个principal属性        AttributePrincipal principal = this.retrievePrincipalFromSessionOrRequest(servletRequest);       //对request进行包装,并处理后面的过滤器,使其后面的过滤器或者servlert能够在reqeust能够在request.getRemoteUser()或者request.getUserPrincipal        filterChain.doFilter(new HttpServletRequestWrapperFilter.CasHttpServletRequestWrapper((HttpServletRequest)servletRequest, principal), servletResponse);    }    protected AttributePrincipal retrievePrincipalFromSessionOrRequest(ServletRequest servletRequest) {        HttpServletRequest request = (HttpServletRequest)servletRequest;        HttpSession session = request.getSession(false);        Assertion assertion = (Assertion)((Assertion)(session == null?request.getAttribute("_const_cas_assertion_"):session.getAttribute("_const_cas_assertion_")));        return assertion == null?null:assertion.getPrincipal();    }
实现起来也比较简单,这个里面使用一个内部类CasHttpServletRequestWrapper,其继承HttpServletRequestWrapper,    通过给定Assertion对象中取得AttributePrincipal对象来组装CasHttpServletRequestWrapper。
final class CasHttpServletRequestWrapper extends HttpServletRequestWrapper {        private final AttributePrincipal principal;        CasHttpServletRequestWrapper(HttpServletRequest request, AttributePrincipal principal) {            super(request);            this.principal = principal;        }        public Principal getUserPrincipal() {            return this.principal;        }        public String getRemoteUser() {            return this.principal != null?this.principal.getName():null;        }        public boolean isUserInRole(String role) {            if(CommonUtils.isBlank(role)) {                HttpServletRequestWrapperFilter.this.logger.debug("No valid role provided.  Returning false.");                return false;            } else if(this.principal == null) {                HttpServletRequestWrapperFilter.this.logger.debug("No Principal in Request.  Returning false.");                return false;            } else if(CommonUtils.isBlank(HttpServletRequestWrapperFilter.this.roleAttribute)) {                HttpServletRequestWrapperFilter.this.logger.debug("No Role Attribute Configured. Returning false.");                return false;            } else {                Object value = this.principal.getAttributes().get(HttpServletRequestWrapperFilter.this.roleAttribute);                if(value instanceof Collection) {                    Iterator isMember = ((Collection)value).iterator();                    while(isMember.hasNext()) {                        Object o = isMember.next();                        if(this.rolesEqual(role, o)) {                            HttpServletRequestWrapperFilter.this.logger.debug("User [{}] is in role [{}]: true", this.getRemoteUser(), role);                            return true;                        }                    }                }                boolean isMember1 = this.rolesEqual(role, value);                HttpServletRequestWrapperFilter.this.logger.debug("User [{}] is in role [{}]: {}", new Object[]{this.getRemoteUser(), role, Boolean.valueOf(isMember1)});                return isMember1;            }        }        private boolean rolesEqual(String given, Object candidate) {            return HttpServletRequestWrapperFilter.this.ignoreCase?given.equalsIgnoreCase(candidate.toString()):given.equals(candidate);        }    }

转载于:https://www.cnblogs.com/ssgao/p/8817316.html

你可能感兴趣的文章
题目4:棋盘寻宝扩展
查看>>
对一些面试题的回答
查看>>
c# enum用法
查看>>
Struts2 中action之间的跳转(分享)
查看>>
HDU4707:Pet(DFS)
查看>>
html标签页图标
查看>>
C# list 新用法
查看>>
Android 获取控件相对于屏幕位置
查看>>
DNGuard Enterprise v2.80 released
查看>>
WPP
查看>>
C# GetSchema Get List of Table 获取数据库中所有的表名以及表中的纪录条数的方法
查看>>
PySide教程:“.NET研究”第一个PySide应用
查看>>
winrar自解压释放路径详解
查看>>
图像开运算+闭运算+腐蚀+膨胀
查看>>
poj-1324 Holedox Moving **** [转]
查看>>
深入foreach工作方式
查看>>
UIView 进行各种动画展示及其用法解释
查看>>
公布2012年5月赛CSDN算法达人赛试题及参考答案
查看>>
Mysql ON子句和USING子句
查看>>
linux杂谈
查看>>