BasicProcessingFilterEntryPoint.java 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 authentication via the {@link
  32. * BasicProcessingFilter}.<P>Once a user agent is authenticated using BASIC authentication, logout requires that
  33. * the browser be closed or an unauthorized (401) header be sent. The simplest way of achieving the latter is to call
  34. * the {@link #commence(ServletRequest, ServletResponse, AuthenticationException)} method below. This will indicate to
  35. * the browser its credentials are no longer authorized, causing it to prompt the user to login again.</p>
  36. *
  37. * @author Ben Alex
  38. * @version $Id: BasicProcessingFilterEntryPoint.java 1822 2007-05-17 12:20:16Z vishalpuri $
  39. */
  40. public class BasicProcessingFilterEntryPoint implements AuthenticationEntryPoint, InitializingBean, Ordered, ApplicationContextAware {
  41. //~ Instance fields ================================================================================================
  42. private static final int DEFAULT_ORDER = Integer.MAX_VALUE;
  43. private String realmName;
  44. private int order = DEFAULT_ORDER;
  45. private ApplicationContext applicationContext;
  46. //~ Methods ========================================================================================================
  47. public int getOrder() {
  48. return order;
  49. }
  50. public void setOrder(int order) {
  51. this.order = order;
  52. }
  53. public void afterPropertiesSet() throws Exception {
  54. Assert.hasText(realmName, "realmName must be specified");
  55. if (order == DEFAULT_ORDER) {
  56. OrderedUtils.copyOrderFromOtherClass(BasicProcessingFilter.class, applicationContext, this, true);
  57. }
  58. }
  59. public void commence(ServletRequest request, ServletResponse response, AuthenticationException authException)
  60. throws IOException, ServletException {
  61. HttpServletResponse httpResponse = (HttpServletResponse) response;
  62. httpResponse.addHeader("WWW-Authenticate", "Basic realm=\"" + realmName + "\"");
  63. httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
  64. }
  65. public String getRealmName() {
  66. return realmName;
  67. }
  68. public void setRealmName(String realmName) {
  69. this.realmName = realmName;
  70. }
  71. public void setApplicationContext(ApplicationContext applicationContext) {
  72. this.applicationContext = applicationContext;
  73. }
  74. }