Commit 99f90f9d1d7bce0d2c446736aed0800de531fb78
1 parent
c3a8770f
Exists in
master
Implementação do controle de acesso na extensão Servlet
Showing
5 changed files
with
293 additions
and
9 deletions
Show diff stats
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/security/Credentials.java
0 → 100644
| @@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
| 1 | +/* | ||
| 2 | + * Demoiselle Framework | ||
| 3 | + * Copyright (C) 2010 SERPRO | ||
| 4 | + * ---------------------------------------------------------------------------- | ||
| 5 | + * This file is part of Demoiselle Framework. | ||
| 6 | + * | ||
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 9 | + * as published by the Free Software Foundation. | ||
| 10 | + * | ||
| 11 | + * This program is distributed in the hope that it will be useful, | ||
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | + * GNU General Public License for more details. | ||
| 15 | + * | ||
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | + * ---------------------------------------------------------------------------- | ||
| 21 | + * Este arquivo é parte do Framework Demoiselle. | ||
| 22 | + * | ||
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 25 | + * do Software Livre (FSF). | ||
| 26 | + * | ||
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 30 | + * para maiores detalhes. | ||
| 31 | + * | ||
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 36 | + */ | ||
| 37 | +package br.gov.frameworkdemoiselle.security; | ||
| 38 | + | ||
| 39 | +import java.io.Serializable; | ||
| 40 | + | ||
| 41 | +import javax.enterprise.context.RequestScoped; | ||
| 42 | +import javax.inject.Named; | ||
| 43 | + | ||
| 44 | +@Named | ||
| 45 | +@RequestScoped | ||
| 46 | +public class Credentials implements Serializable { | ||
| 47 | + | ||
| 48 | + private static final long serialVersionUID = 1L; | ||
| 49 | + | ||
| 50 | + private String username; | ||
| 51 | + | ||
| 52 | + private String password; | ||
| 53 | + | ||
| 54 | + public void clear() { | ||
| 55 | + this.username = null; | ||
| 56 | + this.password = null; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + public String getUsername() { | ||
| 60 | + return username; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + public void setUsername(String username) { | ||
| 64 | + this.username = username; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public String getPassword() { | ||
| 68 | + return password; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + public void setPassword(String password) { | ||
| 72 | + this.password = password; | ||
| 73 | + } | ||
| 74 | +} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/security/ServletAuthenticator.java
0 → 100644
| @@ -0,0 +1,139 @@ | @@ -0,0 +1,139 @@ | ||
| 1 | +/* | ||
| 2 | + * Demoiselle Framework | ||
| 3 | + * Copyright (C) 2010 SERPRO | ||
| 4 | + * ---------------------------------------------------------------------------- | ||
| 5 | + * This file is part of Demoiselle Framework. | ||
| 6 | + * | ||
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 9 | + * as published by the Free Software Foundation. | ||
| 10 | + * | ||
| 11 | + * This program is distributed in the hope that it will be useful, | ||
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | + * GNU General Public License for more details. | ||
| 15 | + * | ||
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | + * ---------------------------------------------------------------------------- | ||
| 21 | + * Este arquivo é parte do Framework Demoiselle. | ||
| 22 | + * | ||
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 25 | + * do Software Livre (FSF). | ||
| 26 | + * | ||
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 30 | + * para maiores detalhes. | ||
| 31 | + * | ||
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 36 | + */ | ||
| 37 | +package br.gov.frameworkdemoiselle.security; | ||
| 38 | + | ||
| 39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
| 40 | + | ||
| 41 | +import java.security.Principal; | ||
| 42 | + | ||
| 43 | +import javax.servlet.ServletException; | ||
| 44 | +import javax.servlet.http.HttpServletRequest; | ||
| 45 | + | ||
| 46 | +import org.slf4j.Logger; | ||
| 47 | + | ||
| 48 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
| 49 | +import br.gov.frameworkdemoiselle.internal.interceptor.TransactionalInterceptor; | ||
| 50 | +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; | ||
| 51 | +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | ||
| 52 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 53 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 54 | + | ||
| 55 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
| 56 | +public class ServletAuthenticator implements Authenticator { | ||
| 57 | + | ||
| 58 | + private static final long serialVersionUID = 1L; | ||
| 59 | + | ||
| 60 | + private static ResourceBundle bundle; | ||
| 61 | + | ||
| 62 | + private static Logger logger; | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public boolean authenticate() { | ||
| 66 | + boolean result; | ||
| 67 | + | ||
| 68 | + try { | ||
| 69 | + getRequest().login(getCredentials().getUsername(), getCredentials().getPassword()); | ||
| 70 | + result = true; | ||
| 71 | + | ||
| 72 | + } catch (ServletException cause) { | ||
| 73 | + getLogger().debug(getBundle().getString(cause.getLocalizedMessage())); | ||
| 74 | + | ||
| 75 | + result = false; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + return result; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public void unAuthenticate() { | ||
| 83 | + getCredentials().clear(); | ||
| 84 | + getRequest().getSession().invalidate(); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + @Override | ||
| 88 | + public User getUser() { | ||
| 89 | + User user = null; | ||
| 90 | + final Principal userPincipal = getRequest().getUserPrincipal(); | ||
| 91 | + | ||
| 92 | + if (userPincipal != null) { | ||
| 93 | + user = new User() { | ||
| 94 | + | ||
| 95 | + private static final long serialVersionUID = 1L; | ||
| 96 | + | ||
| 97 | + @Override | ||
| 98 | + public String getId() { | ||
| 99 | + return userPincipal.getName(); | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + @Override | ||
| 103 | + public void setAttribute(Object key, Object value) { | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + @Override | ||
| 107 | + public Object getAttribute(Object key) { | ||
| 108 | + return null; | ||
| 109 | + } | ||
| 110 | + }; | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + return user; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + protected Credentials getCredentials() { | ||
| 117 | + return Beans.getReference(Credentials.class); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + private HttpServletRequest getRequest() { | ||
| 121 | + return Beans.getReference(HttpServletRequest.class); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + private static ResourceBundle getBundle() { | ||
| 125 | + if (bundle == null) { | ||
| 126 | + bundle = ResourceBundleProducer.create("demoiselle-servlet-bundle"); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + return bundle; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + private static Logger getLogger() { | ||
| 133 | + if (logger == null) { | ||
| 134 | + logger = LoggerProducer.create(TransactionalInterceptor.class); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + return logger; | ||
| 138 | + } | ||
| 139 | +} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/security/ServletAuthorizer.java
0 → 100644
| @@ -0,0 +1,78 @@ | @@ -0,0 +1,78 @@ | ||
| 1 | +/* | ||
| 2 | + * Demoiselle Framework | ||
| 3 | + * Copyright (C) 2010 SERPRO | ||
| 4 | + * ---------------------------------------------------------------------------- | ||
| 5 | + * This file is part of Demoiselle Framework. | ||
| 6 | + * | ||
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 9 | + * as published by the Free Software Foundation. | ||
| 10 | + * | ||
| 11 | + * This program is distributed in the hope that it will be useful, | ||
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | + * GNU General Public License for more details. | ||
| 15 | + * | ||
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | + * ---------------------------------------------------------------------------- | ||
| 21 | + * Este arquivo é parte do Framework Demoiselle. | ||
| 22 | + * | ||
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 25 | + * do Software Livre (FSF). | ||
| 26 | + * | ||
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 30 | + * para maiores detalhes. | ||
| 31 | + * | ||
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 36 | + */ | ||
| 37 | +package br.gov.frameworkdemoiselle.security; | ||
| 38 | + | ||
| 39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_L1_PRIORITY; | ||
| 40 | + | ||
| 41 | +import javax.servlet.http.HttpServletRequest; | ||
| 42 | + | ||
| 43 | +import br.gov.frameworkdemoiselle.DemoiselleException; | ||
| 44 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
| 45 | +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; | ||
| 46 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
| 47 | +import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
| 48 | + | ||
| 49 | +@Priority(EXTENSIONS_L1_PRIORITY) | ||
| 50 | +public class ServletAuthorizer implements Authorizer { | ||
| 51 | + | ||
| 52 | + private static final long serialVersionUID = 1L; | ||
| 53 | + | ||
| 54 | + private static ResourceBundle bundle; | ||
| 55 | + | ||
| 56 | + @Override | ||
| 57 | + public boolean hasRole(String role) { | ||
| 58 | + return getRequest().isUserInRole(role); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Override | ||
| 62 | + public boolean hasPermission(String resource, String operation) { | ||
| 63 | + throw new DemoiselleException(getBundle().getString("has-permission-not-supported", | ||
| 64 | + RequiredPermission.class.getSimpleName())); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + private HttpServletRequest getRequest() { | ||
| 68 | + return Beans.getReference(HttpServletRequest.class); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + private static ResourceBundle getBundle() { | ||
| 72 | + if (bundle == null) { | ||
| 73 | + bundle = ResourceBundleProducer.create("demoiselle-servlet-bundle"); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + return bundle; | ||
| 77 | + } | ||
| 78 | +} |
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletFilter.java
| @@ -63,14 +63,6 @@ public class ServletFilter implements Filter { | @@ -63,14 +63,6 @@ public class ServletFilter implements Filter { | ||
| 63 | Beans.getReference(HttpServletRequestProducer.class).setDelegate((HttpServletRequest) request); | 63 | Beans.getReference(HttpServletRequestProducer.class).setDelegate((HttpServletRequest) request); |
| 64 | Beans.getReference(HttpServletResponseProducer.class).setDelegate((HttpServletResponse) response); | 64 | Beans.getReference(HttpServletResponseProducer.class).setDelegate((HttpServletResponse) response); |
| 65 | 65 | ||
| 66 | - // LoginContext ctx = null; | ||
| 67 | - // HttpSession sess = (HttpSession) ((HttpServletRequest) request).getSession(false); | ||
| 68 | - // if (sess != null) { | ||
| 69 | - // ctx = (LoginContext) sess.getAttribute("ctx"); | ||
| 70 | - // } | ||
| 71 | - | ||
| 72 | - // System.out.println(ctx); | ||
| 73 | - | ||
| 74 | chain.doFilter(request, response); | 66 | chain.doFilter(request, response); |
| 75 | } | 67 | } |
| 76 | 68 |
impl/extension/servlet/src/main/resources/demoiselle-servlet-bundle.properties
| @@ -33,4 +33,5 @@ | @@ -33,4 +33,5 @@ | ||
| 33 | # ou escreva para a Fundação do Software Livre (FSF) Inc., | 33 | # ou escreva para a Fundação do Software Livre (FSF) Inc., |
| 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 35 | 35 | ||
| 36 | -id-converter-not-found=Voc\u00EA precisa criar um FacesConverter para a classe "{0}". | ||
| 37 | \ No newline at end of file | 36 | \ No newline at end of file |
| 37 | +has-permission-not-supported=N\u00E3o \u00E9 poss\u00EDvel utilizar @{0}, pois esta funcionalidade n\u00E3o \u00E9 suportada pelo JAAS | ||
| 38 | +authentication-failed=Falha na autentica\u00E7\u00E3o. |