]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/login/AnonymousLoginModule.java
Use GrantedAuthority implementing Principal in order to optimise Jackrabbit login
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / login / AnonymousLoginModule.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 org.argeo.security.login;
17
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Locale;
22
23 import javax.security.auth.callback.Callback;
24 import javax.security.auth.callback.CallbackHandler;
25 import javax.security.auth.callback.UnsupportedCallbackException;
26 import javax.security.auth.login.LoginException;
27
28 import org.argeo.security.SecurityUtils;
29 import org.argeo.util.LocaleCallback;
30 import org.argeo.util.LocaleUtils;
31 import org.springframework.security.authentication.AnonymousAuthenticationToken;
32 import org.springframework.security.core.Authentication;
33 import org.springframework.security.core.authority.SimpleGrantedAuthority;
34
35 /** Login module which caches one subject per thread. */
36 public class AnonymousLoginModule extends AbstractSpringLoginModule {
37 private String anonymousRole = "ROLE_ANONYMOUS";
38 /** Comma separated list of locales */
39 private String availableLocales = null;
40
41 @Override
42 protected Authentication processLogin(CallbackHandler callbackHandler)
43 throws LoginException, UnsupportedCallbackException, IOException,
44 InterruptedException {
45 BundleContextCallback bundleContextCallback = new BundleContextCallback();
46 Locale selectedLocale = null;
47 // multi locale
48 if (availableLocales != null && !availableLocales.trim().equals("")) {
49 LocaleCallback localeCallback = new LocaleCallback(availableLocales);
50 callbackHandler.handle(new Callback[] { localeCallback,
51 bundleContextCallback });
52 selectedLocale = localeCallback.getSelectedLocale();
53 } else {
54 callbackHandler.handle(new Callback[] { bundleContextCallback });
55 }
56
57 List<SimpleGrantedAuthority> authorities = Collections
58 .singletonList(new SimpleGrantedAuthority(anonymousRole));
59 AnonymousAuthenticationToken anonymousToken = new AnonymousAuthenticationToken(
60 SecurityUtils.getStaticKey(), null, authorities);
61
62 Authentication auth = getAuthenticationManager(bundleContextCallback)
63 .authenticate(anonymousToken);
64
65 if (selectedLocale != null)
66 LocaleUtils.threadLocale.set(selectedLocale);
67 return auth;
68 }
69 }