| 1 | /* Copyright 2006-2009 the original author or authors. |
|---|
| 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.codehaus.groovy.grails.plugins.springsecurity |
|---|
| 16 | |
|---|
| 17 | import org.springframework.security.AuthenticationException |
|---|
| 18 | import org.springframework.security.BadCredentialsException |
|---|
| 19 | import org.springframework.security.ui.ntlm.NtlmBaseException |
|---|
| 20 | import org.springframework.security.ui.ntlm.NtlmBeginHandshakeException |
|---|
| 21 | import org.springframework.security.ui.ntlm.NtlmProcessingFilter |
|---|
| 22 | import org.springframework.security.ui.ntlm.NtlmProcessingFilterEntryPoint |
|---|
| 23 | |
|---|
| 24 | import javax.servlet.ServletException |
|---|
| 25 | import javax.servlet.ServletRequest |
|---|
| 26 | import javax.servlet.ServletResponse |
|---|
| 27 | import javax.servlet.http.HttpServletRequest |
|---|
| 28 | import javax.servlet.http.HttpServletResponse |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * @author Martin Vlcek |
|---|
| 32 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
|---|
| 33 | */ |
|---|
| 34 | class GrailsNtlmProcessingFilterEntryPoint extends NtlmProcessingFilterEntryPoint { |
|---|
| 35 | |
|---|
| 36 | private final String STATE_ATTR = NtlmProcessingFilter.@STATE_ATTR |
|---|
| 37 | private final Integer BEGIN = NtlmProcessingFilter.@BEGIN |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * {@inheritDoc} |
|---|
| 41 | * @see org.springframework.security.ui.ntlm.NtlmProcessingFilterEntryPoint#commence( |
|---|
| 42 | * javax.servlet.ServletRequest, javax.servlet.ServletResponse, |
|---|
| 43 | * org.springframework.security.AuthenticationException) |
|---|
| 44 | */ |
|---|
| 45 | @Override |
|---|
| 46 | void commence(ServletRequest req, ServletResponse res, AuthenticationException authException) throws IOException, ServletException { |
|---|
| 47 | |
|---|
| 48 | // start authentication, if necessary and forceIdentification in NtlmProcessingFilter is false |
|---|
| 49 | if (!(authException instanceof NtlmBaseException |
|---|
| 50 | || authException instanceof BadCredentialsException)) { |
|---|
| 51 | |
|---|
| 52 | req.session.setAttribute STATE_ATTR, BEGIN |
|---|
| 53 | |
|---|
| 54 | HttpServletResponse response = (HttpServletResponse)res |
|---|
| 55 | |
|---|
| 56 | response.setHeader 'WWW-Authenticate', new NtlmBeginHandshakeException().message |
|---|
| 57 | response.setHeader 'Connection', 'Keep-Alive' |
|---|
| 58 | response.status = HttpServletResponse.SC_UNAUTHORIZED |
|---|
| 59 | response.contentLength = 0 |
|---|
| 60 | response.flushBuffer() |
|---|
| 61 | } |
|---|
| 62 | else { |
|---|
| 63 | super.commence(req, res, authException) |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | } |
|---|