您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AuthenticationFilter.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.IOException;
  18. import java.security.Principal;
  19. import java.util.Enumeration;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. import javax.servlet.Filter;
  23. import javax.servlet.FilterChain;
  24. import javax.servlet.FilterConfig;
  25. import javax.servlet.ServletException;
  26. import javax.servlet.ServletRequest;
  27. import javax.servlet.ServletResponse;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletRequestWrapper;
  30. import javax.servlet.http.HttpServletResponse;
  31. import javax.servlet.http.HttpSession;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import com.gitblit.models.UserModel;
  35. import com.gitblit.utils.DeepCopier;
  36. import com.gitblit.utils.StringUtils;
  37. /**
  38. * The AuthenticationFilter is a servlet filter that preprocesses requests that
  39. * match its url pattern definition in the web.xml file.
  40. *
  41. * http://en.wikipedia.org/wiki/Basic_access_authentication
  42. *
  43. * @author James Moger
  44. *
  45. */
  46. public abstract class AuthenticationFilter implements Filter {
  47. protected static final String CHALLENGE = "Basic realm=\"" + Constants.NAME + "\"";
  48. protected static final String SESSION_SECURED = "com.gitblit.secured";
  49. protected transient Logger logger = LoggerFactory.getLogger(getClass());
  50. /**
  51. * doFilter does the actual work of preprocessing the request to ensure that
  52. * the user may proceed.
  53. *
  54. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
  55. * javax.servlet.ServletResponse, javax.servlet.FilterChain)
  56. */
  57. @Override
  58. public abstract void doFilter(final ServletRequest request, final ServletResponse response,
  59. final FilterChain chain) throws IOException, ServletException;
  60. /**
  61. * Allow the filter to require a client certificate to continue processing.
  62. *
  63. * @return true, if a client certificate is required
  64. */
  65. protected boolean requiresClientCertificate() {
  66. return false;
  67. }
  68. /**
  69. * Returns the full relative url of the request.
  70. *
  71. * @param httpRequest
  72. * @return url
  73. */
  74. protected String getFullUrl(HttpServletRequest httpRequest) {
  75. String servletUrl = httpRequest.getContextPath() + httpRequest.getServletPath();
  76. String url = httpRequest.getRequestURI().substring(servletUrl.length());
  77. String params = httpRequest.getQueryString();
  78. if (url.length() > 0 && url.charAt(0) == '/') {
  79. url = url.substring(1);
  80. }
  81. String fullUrl = url + (StringUtils.isEmpty(params) ? "" : ("?" + params));
  82. return fullUrl;
  83. }
  84. /**
  85. * Returns the user making the request, if the user has authenticated.
  86. *
  87. * @param httpRequest
  88. * @return user
  89. */
  90. protected UserModel getUser(HttpServletRequest httpRequest) {
  91. UserModel user = GitBlit.self().authenticate(httpRequest, requiresClientCertificate());
  92. return user;
  93. }
  94. /**
  95. * Taken from Jetty's LoginAuthenticator.renewSessionOnAuthentication()
  96. */
  97. protected void newSession(HttpServletRequest request, HttpServletResponse response) {
  98. HttpSession oldSession = request.getSession(false);
  99. if (oldSession != null && oldSession.getAttribute(SESSION_SECURED) == null) {
  100. synchronized (this) {
  101. Map<String, Object> attributes = new HashMap<String, Object>();
  102. Enumeration<String> e = oldSession.getAttributeNames();
  103. while (e.hasMoreElements()) {
  104. String name = e.nextElement();
  105. attributes.put(name, oldSession.getAttribute(name));
  106. oldSession.removeAttribute(name);
  107. }
  108. oldSession.invalidate();
  109. HttpSession newSession = request.getSession(true);
  110. newSession.setAttribute(SESSION_SECURED, Boolean.TRUE);
  111. for (Map.Entry<String, Object> entry : attributes.entrySet()) {
  112. newSession.setAttribute(entry.getKey(), entry.getValue());
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  119. */
  120. @Override
  121. public void init(final FilterConfig config) throws ServletException {
  122. }
  123. /**
  124. * @see javax.servlet.Filter#destroy()
  125. */
  126. @Override
  127. public void destroy() {
  128. }
  129. /**
  130. * Wraps a standard HttpServletRequest and overrides user principal methods.
  131. */
  132. public static class AuthenticatedRequest extends HttpServletRequestWrapper {
  133. private UserModel user;
  134. public AuthenticatedRequest(HttpServletRequest req) {
  135. super(req);
  136. user = DeepCopier.copy(UserModel.ANONYMOUS);
  137. }
  138. UserModel getUser() {
  139. return user;
  140. }
  141. void setUser(UserModel user) {
  142. this.user = user;
  143. }
  144. @Override
  145. public String getRemoteUser() {
  146. return user.username;
  147. }
  148. @Override
  149. public boolean isUserInRole(String role) {
  150. if (role.equals(Constants.ADMIN_ROLE)) {
  151. return user.canAdmin();
  152. }
  153. // Gitblit does not currently use actual roles in the traditional
  154. // servlet container sense. That is the reason this is marked
  155. // deprecated, but I may want to revisit this.
  156. return user.canAccessRepository(role);
  157. }
  158. @Override
  159. public Principal getUserPrincipal() {
  160. return user;
  161. }
  162. }
  163. }