001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.web.filter.authc; 020 021import org.apache.shiro.authc.AuthenticationToken; 022import org.apache.shiro.lang.codec.Base64; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026import javax.servlet.ServletRequest; 027import javax.servlet.ServletResponse; 028import javax.servlet.http.HttpServletRequest; 029 030 031/** 032 * Requires the requesting user to be {@link org.apache.shiro.subject.Subject#isAuthenticated() authenticated} for the 033 * request to continue, and if they're not, requires the user to login via the HTTP Basic protocol-specific challenge. 034 * Upon successful login, they're allowed to continue on to the requested resource/url. 035 * <p/> 036 * This implementation is a 'clean room' Java implementation of Basic HTTP Authentication specification per 037 * <a href="ftp://ftp.isi.edu/in-notes/rfc2617.txt">RFC 2617</a>. 038 * <p/> 039 * Basic authentication functions as follows: 040 * <ol> 041 * <li>A request comes in for a resource that requires authentication.</li> 042 * <li>The server replies with a 401 response status, sets the <code>WWW-Authenticate</code> header, and the contents of a 043 * page informing the user that the incoming resource requires authentication.</li> 044 * <li>Upon receiving this <code>WWW-Authenticate</code> challenge from the server, the client then takes a 045 * username and a password and puts them in the following format: 046 * <p><code>username:password</code></p></li> 047 * <li>This token is then base 64 encoded.</li> 048 * <li>The client then sends another request for the same resource with the following header:<br/> 049 * <p><code>Authorization: Basic <em>Base64_encoded_username_and_password</em></code></p></li> 050 * </ol> 051 * The {@link #onAccessDenied(javax.servlet.ServletRequest, javax.servlet.ServletResponse)} method will 052 * only be called if the subject making the request is not 053 * {@link org.apache.shiro.subject.Subject#isAuthenticated() authenticated} 054 * 055 * @see <a href="https://tools.ietf.org/html/rfc2617">RFC 2617</a> 056 * @see <a href="http://en.wikipedia.org/wiki/Basic_access_authentication">Basic Access Authentication</a> 057 * @since 0.9 058 */ 059public class BasicHttpAuthenticationFilter extends HttpAuthenticationFilter { 060 061 /** 062 * This class's private logger. 063 */ 064 private static final Logger LOGGER = LoggerFactory.getLogger(BasicHttpAuthenticationFilter.class); 065 066 067 public BasicHttpAuthenticationFilter() { 068 setAuthcScheme(HttpServletRequest.BASIC_AUTH); 069 setAuthzScheme(HttpServletRequest.BASIC_AUTH); 070 } 071 072 /** 073 * Creates an AuthenticationToken for use during login attempt with the provided credentials in the http header. 074 * <p/> 075 * This implementation: 076 * <ol><li>acquires the username and password based on the request's 077 * {@link #getAuthzHeader(javax.servlet.ServletRequest) authorization header} via the 078 * {@link #getPrincipalsAndCredentials(String, javax.servlet.ServletRequest) getPrincipalsAndCredentials} method</li> 079 * <li>The return value of that method is converted to an <code>AuthenticationToken</code> via the 080 * {@link #createToken(String, String, javax.servlet.ServletRequest, javax.servlet.ServletResponse) createToken} method</li> 081 * <li>The created <code>AuthenticationToken</code> is returned.</li> 082 * </ol> 083 * 084 * @param request incoming ServletRequest 085 * @param response outgoing ServletResponse (never used) 086 * @return the AuthenticationToken used to execute the login attempt 087 */ 088 @Override 089 protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) { 090 String authorizationHeader = getAuthzHeader(request); 091 if (authorizationHeader == null || authorizationHeader.length() == 0) { 092 // Create an empty authentication token since there is no 093 // Authorization header. 094 return createToken("", "", request, response); 095 } 096 097 LOGGER.debug("Attempting to execute login with auth header"); 098 099 String[] prinCred = getPrincipalsAndCredentials(authorizationHeader, request); 100 if (prinCred == null || prinCred.length < 2) { 101 // Create an authentication token with an empty password, 102 // since one hasn't been provided in the request. 103 String username = prinCred == null || prinCred.length == 0 ? "" : prinCred[0]; 104 return createToken(username, "", request, response); 105 } 106 107 String username = prinCred[0]; 108 String password = prinCred[1]; 109 110 return createToken(username, password, request, response); 111 } 112 113 /** 114 * Returns the username and password pair based on the specified <code>encoded</code> String obtained from 115 * the request's authorization header. 116 * <p/> 117 * Per RFC 2617, the default implementation first Base64 decodes the string and then splits the resulting decoded 118 * string into two based on the ":" character. That is: 119 * <p/> 120 * <code>String decoded = Base64.decodeToString(encoded);<br/> 121 * return decoded.split(":");</code> 122 * 123 * @param scheme the {@link #getAuthcScheme() authcScheme} found in the request 124 * {@link #getAuthzHeader(javax.servlet.ServletRequest) authzHeader}. It is ignored by this implementation, 125 * but available to overriding implementations should they find it useful. 126 * @param encoded the Base64-encoded username:password value found after the scheme in the header 127 * @return the username (index 0)/password (index 1) pair obtained from the encoded header data. 128 */ 129 @Override 130 protected String[] getPrincipalsAndCredentials(String scheme, String encoded) { 131 String decoded = Base64.decodeToString(encoded); 132 return decoded.split(":", 2); 133 } 134}