2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.internal
.auth
;
18 import java
.io
.IOException
;
19 import java
.util
.Collections
;
20 import java
.util
.List
;
21 import java
.util
.Locale
;
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
;
28 import org
.argeo
.cms
.KernelHeader
;
29 import org
.argeo
.cms
.internal
.kernel
.Activator
;
30 import org
.argeo
.util
.LocaleCallback
;
31 import org
.argeo
.util
.LocaleUtils
;
32 import org
.springframework
.security
.authentication
.AnonymousAuthenticationToken
;
33 import org
.springframework
.security
.core
.Authentication
;
35 /** Login module which caches one subject per thread. */
36 public class AnonymousLoginModule
extends AbstractLoginModule
{
37 /** Comma separated list of locales */
38 private String availableLocales
= null;
41 protected Authentication
processLogin(CallbackHandler callbackHandler
)
42 throws LoginException
, UnsupportedCallbackException
, IOException
,
43 InterruptedException
{
44 Locale selectedLocale
= null;
46 if (availableLocales
!= null && !availableLocales
.trim().equals("")) {
47 LocaleCallback localeCallback
= new LocaleCallback(availableLocales
);
48 callbackHandler
.handle(new Callback
[] { localeCallback
});
49 selectedLocale
= localeCallback
.getSelectedLocale();
51 callbackHandler
.handle(new Callback
[] {});
54 List
<GrantedAuthorityPrincipal
> authorities
= Collections
55 .singletonList(new GrantedAuthorityPrincipal(
56 KernelHeader
.ROLE_ANONYMOUS
));
57 AnonymousAuthenticationToken anonymousToken
= new AnonymousAuthenticationToken(
58 Activator
.getSystemKey(), null, authorities
);
60 Authentication auth
= getAuthenticationManager().authenticate(
63 if (selectedLocale
!= null)
64 LocaleUtils
.threadLocale
.set(selectedLocale
);