1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| package com.birdzhang.plugin;
import java.io.IOException; import java.util.HashMap; import java.util.Map;
import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import org.jasig.cas.client.authentication.DefaultGatewayResolverImpl; import org.jasig.cas.client.authentication.GatewayResolver; import org.jasig.cas.client.util.AbstractCasFilter; import org.jasig.cas.client.util.CommonUtils; import org.jasig.cas.client.validation.Assertion;
public class AuthenticationFilter extends AbstractCasFilter{
private String casServerLoginUrl;
private boolean renew = false;
private boolean gateway = false;
private String excludePaths; private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl();
protected void initInternal(final FilterConfig filterConfig) throws ServletException { if (!isIgnoreInitConfiguration()) { super.initInternal(filterConfig); setCasServerLoginUrl(getPropertyFromInitParams(filterConfig, "casServerLoginUrl", null)); setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, "renew", "false"))); setGateway(parseBoolean(getPropertyFromInitParams(filterConfig, "gateway", "false")));
final String gatewayStorageClass = getPropertyFromInitParams(filterConfig, "gatewayStorageClass", null);
if (gatewayStorageClass != null) { try { this.gatewayStorage = (GatewayResolver) Class.forName(gatewayStorageClass).newInstance(); } catch (final Exception e) { throw new ServletException(e); } } excludePaths = getPropertyFromInitParams(filterConfig, "excludePaths", null); excludePaths = excludePaths.trim(); } }
public void init() { super.init(); CommonUtils.assertNotNull(this.casServerLoginUrl, "casServerLoginUrl cannot be null."); }
private boolean isExclude(String uri){ boolean isInWhiteList = false; if(excludePaths!=null&& uri!=null){ isInWhiteList = uri.matches(excludePaths); } return isInWhiteList; } public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; final HttpServletResponse response = (HttpServletResponse) servletResponse; final HttpSession session = request.getSession(false); final Assertion assertion = session != null ? (Assertion) session.getAttribute(CONST_CAS_ASSERTION) : null;
Map<String, String[]> paramsMap = request.getParameterMap(); if(null != paramsMap ) { if(paramsMap.containsKey("ticket") && paramsMap.containsKey("operation")) {
Map<String, String[]> extraParams = new HashMap<String, String[]>(); HttpServletRequest wrappedRequest = new PrettyFacesWrappedRequest(request, extraParams); request.getRequestDispatcher(request.getRequestURI()).forward(wrappedRequest, response); return; } } if(isExclude(request.getRequestURI())){ filterChain.doFilter(request, response); return; } if (assertion != null) { filterChain.doFilter(request, response); return; }
final String serviceUrl = constructServiceUrl(request, response); final String ticket = CommonUtils.safeGetParameter(request,getArtifactParameterName()); final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl);
if (CommonUtils.isNotBlank(ticket) || wasGatewayed) { filterChain.doFilter(request, response); return; }
final String modifiedServiceUrl;
if (this.gateway) { modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl); } else { modifiedServiceUrl = serviceUrl; }
final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);
response.sendRedirect(urlToRedirectTo); }
public final void setRenew(final boolean renew) { this.renew = renew; }
public final void setGateway(final boolean gateway) { this.gateway = gateway; }
public final void setCasServerLoginUrl(final String casServerLoginUrl) { this.casServerLoginUrl = casServerLoginUrl; } public final void setGatewayStorage(final GatewayResolver gatewayStorage) { this.gatewayStorage = gatewayStorage; } }
|