当前位置: 首页>>代码示例>>Java>>正文


Java ByteChunk.startsWithIgnoreCase方法代码示例

本文整理汇总了Java中org.apache.tomcat.util.buf.ByteChunk.startsWithIgnoreCase方法的典型用法代码示例。如果您正苦于以下问题:Java ByteChunk.startsWithIgnoreCase方法的具体用法?Java ByteChunk.startsWithIgnoreCase怎么用?Java ByteChunk.startsWithIgnoreCase使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.tomcat.util.buf.ByteChunk的用法示例。


在下文中一共展示了ByteChunk.startsWithIgnoreCase方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: authenticate

import org.apache.tomcat.util.buf.ByteChunk; //导入方法依赖的package包/类
/**
 * Authenticate the user making this request, based on the specified login
 * configuration. Return <code>true</code> if any specified constraint has
 * been satisfied, or <code>false</code> if we have created a response
 * challenge already.
 *
 * @param request
 *            Request we are processing
 * @param response
 *            Response we are creating
 * @param config
 *            Login configuration describing how authentication should be
 *            performed
 *
 * @exception IOException
 *                if an input/output error occurs
 */
@Override
public boolean authenticate(Request request, HttpServletResponse response, LoginConfig config) throws IOException {

	if (checkForCachedAuthentication(request, response, true)) {
		return true;
	}

	// Validate any credentials already included with this request
	String username = null;
	String password = null;

	MessageBytes authorization = request.getCoyoteRequest().getMimeHeaders().getValue("authorization");

	if (authorization != null) {
		authorization.toBytes();
		ByteChunk authorizationBC = authorization.getByteChunk();
		if (authorizationBC.startsWithIgnoreCase("basic ", 0)) {
			authorizationBC.setOffset(authorizationBC.getOffset() + 6);

			byte[] decoded = Base64.decodeBase64(authorizationBC.getBuffer(), authorizationBC.getOffset(),
					authorizationBC.getLength());

			// Get username and password
			int colon = -1;
			for (int i = 0; i < decoded.length; i++) {
				if (decoded[i] == ':') {
					colon = i;
					break;
				}
			}

			if (colon < 0) {
				username = new String(decoded, B2CConverter.ISO_8859_1);
			} else {
				username = new String(decoded, 0, colon, B2CConverter.ISO_8859_1);
				password = new String(decoded, colon + 1, decoded.length - colon - 1, B2CConverter.ISO_8859_1);
			}

			authorizationBC.setOffset(authorizationBC.getOffset() - 6);
		}

		Principal principal = context.getRealm().authenticate(username, password);
		if (principal != null) {
			register(request, response, principal, HttpServletRequest.BASIC_AUTH, username, password);
			return (true);
		}
	}

	StringBuilder value = new StringBuilder(16);
	value.append("Basic realm=\"");
	if (config.getRealmName() == null) {
		value.append(REALM_NAME);
	} else {
		value.append(config.getRealmName());
	}
	value.append('\"');
	response.setHeader(AUTH_HEADER_NAME, value.toString());
	response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
	return (false);

}
 
开发者ID:how2j,项目名称:lazycat,代码行数:79,代码来源:BasicAuthenticator.java


注:本文中的org.apache.tomcat.util.buf.ByteChunk.startsWithIgnoreCase方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。