BasicProcessingFilterEntryPoint.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package org.acegisecurity.ui.basicauth;
  16. import java.io.IOException;
  17. import java.util.Map;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.ServletRequest;
  20. import javax.servlet.ServletResponse;
  21. import javax.servlet.http.HttpServletResponse;
  22. import org.acegisecurity.AuthenticationException;
  23. import org.acegisecurity.ui.AuthenticationEntryPoint;
  24. import org.acegisecurity.util.OrderedUtils;
  25. import org.springframework.beans.factory.InitializingBean;
  26. import org.springframework.context.ApplicationContext;
  27. import org.springframework.context.ApplicationContextAware;
  28. import org.springframework.core.Ordered;
  29. import org.springframework.util.Assert;
  30. /**
  31. * Used by the <code>SecurityEnforcementFilter</code> to commence
  32. * authentication via the {@link BasicProcessingFilter}.
  33. * <P>
  34. * Once a user agent is authenticated using BASIC authentication, logout
  35. * requires that the browser be closed or an unauthorized (401) header be sent.
  36. * The simplest way of achieving the latter is to call the
  37. * {@link #commence(ServletRequest, ServletResponse, AuthenticationException)}
  38. * method below. This will indicate to the browser its credentials are no longer
  39. * authorized, causing it to prompt the user to login again.
  40. * </p>
  41. *
  42. * @author Ben Alex
  43. * @version $Id: BasicProcessingFilterEntryPoint.java 1822 2007-05-17 12:20:16Z
  44. * vishalpuri $
  45. */
  46. public class BasicProcessingFilterEntryPoint implements AuthenticationEntryPoint, InitializingBean, Ordered,
  47. ApplicationContextAware {
  48. // ~ Static fields/initializers
  49. // =====================================================================================
  50. private static final int DEFAULT_ORDER = Integer.MAX_VALUE;
  51. // ~ Instance fields
  52. // ================================================================================================
  53. private String realmName;
  54. private int order = DEFAULT_ORDER;
  55. private ApplicationContext applicationContext;
  56. // ~ Methods
  57. // ========================================================================================================
  58. public int getOrder() {
  59. return order;
  60. }
  61. public void setOrder(int order) {
  62. this.order = order;
  63. }
  64. public void afterPropertiesSet() throws Exception {
  65. Assert.hasText(realmName, "realmName must be specified");
  66. if (order == DEFAULT_ORDER) {
  67. OrderedUtils.copyOrderFromOtherClass(BasicProcessingFilter.class, applicationContext, this, true);
  68. }
  69. }
  70. public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)
  71. throws IOException, ServletException {
  72. HttpServletResponse httpResponse = (HttpServletResponse) response;
  73. httpResponse.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\"");
  74. httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
  75. }
  76. public String getRealmName() {
  77. return realmName;
  78. }
  79. public void setRealmName(String realmName) {
  80. this.realmName = realmName;
  81. }
  82. public void setApplicationContext(ApplicationContext applicationContext) {
  83. this.applicationContext = applicationContext;
  84. }
  85. }