做用其實很簡單就是 在HttpServletRequest對象在包裝一次,讓其支持getUserPrincipal,getRemoteUser方法來獲取登陸的用戶信息。session
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); } }