From 0a8b63041025674e8689fc6df0b7cd13056bcda7 Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Tue, 11 Dec 2012 18:58:36 -0300 Subject: [PATCH] Colocando as mensagens no resource bundle --- impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthenticator.java | 25 +++++++++++++++++++++++-- impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthorizer.java | 16 +++++++++++++++- impl/extension/jaas/src/main/resources/demoiselle-jaas-bundle.properties | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 impl/extension/jaas/src/main/resources/demoiselle-jaas-bundle.properties diff --git a/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthenticator.java b/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthenticator.java index f5e59fc..34752bc 100644 --- a/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthenticator.java +++ b/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthenticator.java @@ -54,6 +54,9 @@ import javax.security.auth.login.LoginException; import br.gov.frameworkdemoiselle.annotation.Priority; import br.gov.frameworkdemoiselle.internal.configuration.JAASConfig; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; +import br.gov.frameworkdemoiselle.util.ResourceBundle; +import br.gov.frameworkdemoiselle.util.Strings; @SessionScoped @Priority(EXTENSIONS_L1_PRIORITY) @@ -61,6 +64,8 @@ public class JAASAuthenticator implements Authenticator { private static final long serialVersionUID = 1L; + private ResourceBundle bundle; + private User user; private final Subject subject; @@ -136,7 +141,13 @@ public class JAASAuthenticator implements Authenticator { } public LoginContext createLoginContext() throws LoginException { - return new LoginContext(config.getLoginModuleName(), this.subject, createCallbackHandler()); + String name = config.getLoginModuleName(); + + if (Strings.isEmpty(name)) { + throw new SecurityException(getBundle().getString("required-login-module-name")); + } + + return new LoginContext(name, this.subject, createCallbackHandler()); } private CallbackHandler createCallbackHandler() { @@ -151,10 +162,20 @@ public class JAASAuthenticator implements Authenticator { ((PasswordCallback) callbacks[i]).setPassword(credentials.getPassword().toCharArray()); } else { - System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXX Unsupported callback " + callbacks[i]); + // TODO Utilizar o logger... + + System.out.println(getBundle().getString("unsupported-callback", callbacks[i])); } } } }; } + + private ResourceBundle getBundle() { + if (this.bundle == null) { + this.bundle = ResourceBundleProducer.create("demoiselle-jaas-bundle"); + } + + return this.bundle; + } } diff --git a/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthorizer.java b/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthorizer.java index 2239557..a0fc942 100644 --- a/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthorizer.java +++ b/impl/extension/jaas/src/main/java/br/gov/frameworkdemoiselle/security/JAASAuthorizer.java @@ -44,14 +44,19 @@ import java.util.Enumeration; import javax.security.auth.Subject; +import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.annotation.Priority; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; import br.gov.frameworkdemoiselle.util.Beans; +import br.gov.frameworkdemoiselle.util.ResourceBundle; @Priority(EXTENSIONS_L1_PRIORITY) public class JAASAuthorizer implements Authorizer { private static final long serialVersionUID = 1L; + private ResourceBundle bundle; + @Override public boolean hasRole(String role) { boolean result = false; @@ -83,6 +88,15 @@ public class JAASAuthorizer implements Authorizer { @Override public boolean hasPermission(String resource, String operation) { - return true; + throw new DemoiselleException(getBundle().getString("has-permission-not-supported", + RequiredPermission.class.getSimpleName())); + } + + private ResourceBundle getBundle() { + if (this.bundle == null) { + this.bundle = ResourceBundleProducer.create("demoiselle-jaas-bundle"); + } + + return this.bundle; } } diff --git a/impl/extension/jaas/src/main/resources/demoiselle-jaas-bundle.properties b/impl/extension/jaas/src/main/resources/demoiselle-jaas-bundle.properties new file mode 100644 index 0000000..c1952b1 --- /dev/null +++ b/impl/extension/jaas/src/main/resources/demoiselle-jaas-bundle.properties @@ -0,0 +1,38 @@ +# Demoiselle Framework +# Copyright (C) 2010 SERPRO +# ---------------------------------------------------------------------------- +# This file is part of Demoiselle Framework. +# +# Demoiselle Framework is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public License version 3 +# as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License version 3 +# along with this program; if not, see +# or write to the Free Software Foundation, Inc., 51 Franklin Street, +# Fifth Floor, Boston, MA 02110-1301, USA. +# ---------------------------------------------------------------------------- +# Este arquivo é parte do Framework Demoiselle. +# +# O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou +# modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação +# do Software Livre (FSF). +# +# Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA +# GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou +# APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português +# para maiores detalhes. +# +# Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título +# "LICENCA.txt", junto com esse programa. Se não, acesse +# ou escreva para a Fundação do Software Livre (FSF) Inc., +# 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + +has-permission-not-supported=N\u00E3o \u00E9 poss\u00EDvel utilizar @{0}, pois esta funcionalidade n\u00E3o \u00E9 suportada pelo JAAS +unsupported-callback=Callback n\u00E3o suportado\: {0} +required-login-module-name=\u00C9 preciso definir a propriedade frameworkdemoiselle.security.login.module.name no arquivo demoiselle.properties -- libgit2 0.21.2