[58] | 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.facebook; |
---|
| 16 | |
---|
| 17 | import javax.servlet.http.Cookie; |
---|
| 18 | import javax.servlet.http.HttpServletRequest; |
---|
| 19 | import javax.servlet.http.HttpServletResponse; |
---|
| 20 | |
---|
| 21 | import org.springframework.beans.factory.InitializingBean; |
---|
| 22 | import org.springframework.security.Authentication; |
---|
| 23 | import org.springframework.security.ui.logout.LogoutHandler; |
---|
| 24 | import org.springframework.util.Assert; |
---|
| 25 | import org.springframework.util.StringUtils; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * Removes cookies at logout. |
---|
| 29 | * |
---|
| 30 | * @author <a href='mailto:beckwithb@studentsonly.com'>Burt Beckwith</a> |
---|
| 31 | */ |
---|
| 32 | public class FacebookLogoutHandler implements LogoutHandler, InitializingBean { |
---|
| 33 | |
---|
| 34 | private String _apiKey; |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * {@inheritDoc} |
---|
| 38 | * @see org.springframework.security.ui.logout.LogoutHandler#logout( |
---|
| 39 | * javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, |
---|
| 40 | * org.springframework.security.Authentication) |
---|
| 41 | */ |
---|
| 42 | public void logout(final HttpServletRequest request, final HttpServletResponse response, |
---|
| 43 | final Authentication authentication) { |
---|
| 44 | |
---|
| 45 | Cookie[] cookies = request.getCookies(); |
---|
| 46 | if (cookies != null) { |
---|
| 47 | String path = StringUtils.hasLength(request.getContextPath()) ? request.getContextPath() : "/"; |
---|
| 48 | for (Cookie cookie : cookies) { |
---|
| 49 | if (cookie.getName().startsWith(_apiKey)) { |
---|
| 50 | cancelCookie(cookie.getName(), path, response); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | private void cancelCookie(final String name, final String path, final HttpServletResponse response) { |
---|
| 57 | Cookie cookie = new Cookie(name, null); |
---|
| 58 | cookie.setMaxAge(0); |
---|
| 59 | cookie.setPath(path); |
---|
| 60 | response.addCookie(cookie); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Dependency injection for the API key. |
---|
| 65 | * @param key the key |
---|
| 66 | */ |
---|
| 67 | public void setApiKey(final String key) { |
---|
| 68 | _apiKey = key; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * {@inheritDoc} |
---|
| 73 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
---|
| 74 | */ |
---|
| 75 | public void afterPropertiesSet() { |
---|
| 76 | Assert.notNull(_apiKey, "API key must be specified"); |
---|
| 77 | } |
---|
| 78 | } |
---|