選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AuthenticationFilter.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.servlet;
  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.Constants;
  35. import com.gitblit.manager.IAuthenticationManager;
  36. import com.gitblit.models.UserModel;
  37. import com.gitblit.utils.DeepCopier;
  38. import com.gitblit.utils.StringUtils;
  39. /**
  40. * The AuthenticationFilter is a servlet filter that preprocesses requests that
  41. * match its url pattern definition in the web.xml file.
  42. *
  43. * http://en.wikipedia.org/wiki/Basic_access_authentication
  44. *
  45. * @author James Moger
  46. *
  47. */
  48. public abstract class AuthenticationFilter implements Filter {
  49. protected static final String CHALLENGE = "Basic realm=\"" + Constants.NAME + "\"";
  50. protected static final String SESSION_SECURED = "com.gitblit.secured";
  51. protected transient Logger logger = LoggerFactory.getLogger(getClass());
  52. protected IAuthenticationManager authenticationManager;
  53. protected AuthenticationFilter(IAuthenticationManager authenticationManager) {
  54. this.authenticationManager = authenticationManager;
  55. }
  56. @Override
  57. public void init(FilterConfig filterConfig) throws ServletException {
  58. }
  59. @Override
  60. public void destroy() {
  61. }
  62. /**
  63. * doFilter does the actual work of preprocessing the request to ensure that
  64. * the user may proceed.
  65. *
  66. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
  67. * javax.servlet.ServletResponse, javax.servlet.FilterChain)
  68. */
  69. @Override
  70. public abstract void doFilter(final ServletRequest request, final ServletResponse response,
  71. final FilterChain chain) throws IOException, ServletException;
  72. /**
  73. * Allow the filter to require a client certificate to continue processing.
  74. *
  75. * @return true, if a client certificate is required
  76. */
  77. protected boolean requiresClientCertificate() {
  78. return false;
  79. }
  80. /**
  81. * Returns the full relative url of the request.
  82. *
  83. * @param httpRequest
  84. * @return url
  85. */
  86. protected String getFullUrl(HttpServletRequest httpRequest) {
  87. String servletUrl = httpRequest.getContextPath() + httpRequest.getServletPath();
  88. String url = httpRequest.getRequestURI().substring(servletUrl.length());
  89. String params = httpRequest.getQueryString();
  90. if (url.length() > 0 && url.charAt(0) == '/') {
  91. url = url.substring(1);
  92. }
  93. String fullUrl = url + (StringUtils.isEmpty(params) ? "" : ("?" + params));
  94. return fullUrl;
  95. }
  96. /**
  97. * Returns the user making the request, if the user has authenticated.
  98. *
  99. * @param httpRequest
  100. * @return user
  101. */
  102. protected UserModel getUser(HttpServletRequest httpRequest) {
  103. UserModel user = authenticationManager.authenticate(httpRequest, requiresClientCertificate());
  104. return user;
  105. }
  106. /**
  107. * Taken from Jetty's LoginAuthenticator.renewSessionOnAuthentication()
  108. */
  109. protected void newSession(HttpServletRequest request, HttpServletResponse response) {
  110. HttpSession oldSession = request.getSession(false);
  111. if (oldSession != null && oldSession.getAttribute(SESSION_SECURED) == null) {
  112. synchronized (this) {
  113. Map<String, Object> attributes = new HashMap<String, Object>();
  114. Enumeration<String> e = oldSession.getAttributeNames();
  115. while (e.hasMoreElements()) {
  116. String name = e.nextElement();
  117. attributes.put(name, oldSession.getAttribute(name));
  118. oldSession.removeAttribute(name);
  119. }
  120. oldSession.invalidate();
  121. HttpSession newSession = request.getSession(true);
  122. newSession.setAttribute(SESSION_SECURED, Boolean.TRUE);
  123. for (Map.Entry<String, Object> entry : attributes.entrySet()) {
  124. newSession.setAttribute(entry.getKey(), entry.getValue());
  125. }
  126. }
  127. }
  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.hasRepositoryPermission(role);
  157. }
  158. @Override
  159. public Principal getUserPrincipal() {
  160. return user;
  161. }
  162. }
  163. }