Commit b54b43558563c07d81beb6f00064a9fe2b1823b5

Authored by Cleverson Sacramento
1 parent 32dfc52f
Exists in master

Removendo o método "Principal getCurrentUser()" e suas referencias

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultAuthenticator.java
@@ -37,14 +37,12 @@ @@ -37,14 +37,12 @@
37 package br.gov.frameworkdemoiselle.internal.implementation; 37 package br.gov.frameworkdemoiselle.internal.implementation;
38 38
39 import static br.gov.frameworkdemoiselle.annotation.Priority.L1_PRIORITY; 39 import static br.gov.frameworkdemoiselle.annotation.Priority.L1_PRIORITY;
40 -  
41 -import java.security.Principal;  
42 -  
43 import br.gov.frameworkdemoiselle.DemoiselleException; 40 import br.gov.frameworkdemoiselle.DemoiselleException;
44 import br.gov.frameworkdemoiselle.annotation.Priority; 41 import br.gov.frameworkdemoiselle.annotation.Priority;
45 import br.gov.frameworkdemoiselle.security.AuthenticationException; 42 import br.gov.frameworkdemoiselle.security.AuthenticationException;
46 import br.gov.frameworkdemoiselle.security.Authenticator; 43 import br.gov.frameworkdemoiselle.security.Authenticator;
47 import br.gov.frameworkdemoiselle.security.SecurityContext; 44 import br.gov.frameworkdemoiselle.security.SecurityContext;
  45 +import br.gov.frameworkdemoiselle.security.User;
48 import br.gov.frameworkdemoiselle.util.Beans; 46 import br.gov.frameworkdemoiselle.util.Beans;
49 import br.gov.frameworkdemoiselle.util.NameQualifier; 47 import br.gov.frameworkdemoiselle.util.NameQualifier;
50 import br.gov.frameworkdemoiselle.util.ResourceBundle; 48 import br.gov.frameworkdemoiselle.util.ResourceBundle;
@@ -82,7 +80,7 @@ public class DefaultAuthenticator implements Authenticator { @@ -82,7 +80,7 @@ public class DefaultAuthenticator implements Authenticator {
82 * @see br.gov.frameworkdemoiselle.security.Authenticator#getUser() 80 * @see br.gov.frameworkdemoiselle.security.Authenticator#getUser()
83 */ 81 */
84 @Override 82 @Override
85 - public Principal getUser() { 83 + public User getUser() {
86 throw getException(); 84 throw getException();
87 } 85 }
88 86
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java
@@ -36,12 +36,8 @@ @@ -36,12 +36,8 @@
36 */ 36 */
37 package br.gov.frameworkdemoiselle.internal.implementation; 37 package br.gov.frameworkdemoiselle.internal.implementation;
38 38
39 -import java.io.Serializable;  
40 -import java.security.Principal;  
41 -  
42 import javax.inject.Named; 39 import javax.inject.Named;
43 40
44 -import br.gov.frameworkdemoiselle.DemoiselleException;  
45 import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig; 41 import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig;
46 import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful; 42 import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful;
47 import br.gov.frameworkdemoiselle.security.AfterLogoutSuccessful; 43 import br.gov.frameworkdemoiselle.security.AfterLogoutSuccessful;
@@ -60,7 +56,6 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; @@ -60,7 +56,6 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle;
60 * 56 *
61 * @author SERPRO 57 * @author SERPRO
62 */ 58 */
63 -@SuppressWarnings("deprecation")  
64 @Named("securityContext") 59 @Named("securityContext")
65 public class SecurityContextImpl implements SecurityContext { 60 public class SecurityContextImpl implements SecurityContext {
66 61
@@ -137,7 +132,7 @@ public class SecurityContextImpl implements SecurityContext { @@ -137,7 +132,7 @@ public class SecurityContextImpl implements SecurityContext {
137 boolean result = true; 132 boolean result = true;
138 133
139 if (getConfig().isEnabled()) { 134 if (getConfig().isEnabled()) {
140 - result = getCurrentUser() != null; 135 + result = getUser() != null;
141 } 136 }
142 137
143 return result; 138 return result;
@@ -176,17 +171,11 @@ public class SecurityContextImpl implements SecurityContext { @@ -176,17 +171,11 @@ public class SecurityContextImpl implements SecurityContext {
176 } 171 }
177 172
178 /** 173 /**
179 - * @deprecated Use {@link #getCurrentUser()} instead.  
180 * @see br.gov.frameworkdemoiselle.security.SecurityContext#getUser() 174 * @see br.gov.frameworkdemoiselle.security.SecurityContext#getUser()
181 */ 175 */
182 @Override 176 @Override
183 public User getUser() { 177 public User getUser() {
184 - throw new DemoiselleException("Utilize o método getCurrentUser() ao invés do getUser()");  
185 - }  
186 -  
187 - @Override  
188 - public Principal getCurrentUser() {  
189 - Principal user = getAuthenticator().getUser(); 178 + User user = getAuthenticator().getUser();
190 179
191 if (!getConfig().isEnabled() && user == null) { 180 if (!getConfig().isEnabled() && user == null) {
192 user = new EmptyUser(); 181 user = new EmptyUser();
@@ -213,13 +202,22 @@ public class SecurityContextImpl implements SecurityContext { @@ -213,13 +202,22 @@ public class SecurityContextImpl implements SecurityContext {
213 return bundle; 202 return bundle;
214 } 203 }
215 204
216 - private static class EmptyUser implements Principal, Serializable { 205 + private static class EmptyUser implements User{
217 206
218 private static final long serialVersionUID = 1L; 207 private static final long serialVersionUID = 1L;
219 208
220 @Override 209 @Override
221 - public String getName() { 210 + public String getId() {
222 return "demoiselle"; 211 return "demoiselle";
223 } 212 }
  213 +
  214 + @Override
  215 + public Object getAttribute(Object key) {
  216 + return null;
  217 + }
  218 +
  219 + @Override
  220 + public void setAttribute(Object key, Object value) {
  221 + }
224 } 222 }
225 } 223 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/Authenticator.java
@@ -37,7 +37,6 @@ @@ -37,7 +37,6 @@
37 package br.gov.frameworkdemoiselle.security; 37 package br.gov.frameworkdemoiselle.security;
38 38
39 import java.io.Serializable; 39 import java.io.Serializable;
40 -import java.security.Principal;  
41 40
42 /** 41 /**
43 * Defines the methods that should be implemented by anyone who wants an authentication mechanism. 42 * Defines the methods that should be implemented by anyone who wants an authentication mechanism.
@@ -64,5 +63,5 @@ public interface Authenticator extends Serializable { @@ -64,5 +63,5 @@ public interface Authenticator extends Serializable {
64 * 63 *
65 * @return the user currently authenticated 64 * @return the user currently authenticated
66 */ 65 */
67 - Principal getUser(); 66 + User getUser();
68 } 67 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/RequiredPermissionInterceptor.java
@@ -37,7 +37,6 @@ @@ -37,7 +37,6 @@
37 package br.gov.frameworkdemoiselle.security; 37 package br.gov.frameworkdemoiselle.security;
38 38
39 import java.io.Serializable; 39 import java.io.Serializable;
40 -import java.security.Principal;  
41 40
42 import javax.interceptor.AroundInvoke; 41 import javax.interceptor.AroundInvoke;
43 import javax.interceptor.Interceptor; 42 import javax.interceptor.Interceptor;
@@ -106,10 +105,10 @@ public class RequiredPermissionInterceptor implements Serializable { @@ -106,10 +105,10 @@ public class RequiredPermissionInterceptor implements Serializable {
106 */ 105 */
107 private String getUsername() { 106 private String getUsername() {
108 String username = ""; 107 String username = "";
109 - Principal user = getSecurityContext().getCurrentUser(); 108 + User user = getSecurityContext().getUser();
110 109
111 - if (user != null && user.getName() != null) {  
112 - username = user.getName(); 110 + if (user != null && user.getId() != null) {
  111 + username = user.getId();
113 } 112 }
114 113
115 return username; 114 return username;
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/RequiredRoleInterceptor.java
@@ -84,7 +84,7 @@ public class RequiredRoleInterceptor implements Serializable { @@ -84,7 +84,7 @@ public class RequiredRoleInterceptor implements Serializable {
84 84
85 if (getSecurityContext().isLoggedIn()) { 85 if (getSecurityContext().isLoggedIn()) {
86 getLogger().info( 86 getLogger().info(
87 - getBundle().getString("has-role-verification", getSecurityContext().getCurrentUser().getName(), 87 + getBundle().getString("has-role-verification", getSecurityContext().getUser().getId(),
88 roles)); 88 roles));
89 } 89 }
90 90
@@ -98,14 +98,14 @@ public class RequiredRoleInterceptor implements Serializable { @@ -98,14 +98,14 @@ public class RequiredRoleInterceptor implements Serializable {
98 98
99 if (userRoles.isEmpty()) { 99 if (userRoles.isEmpty()) {
100 getLogger() 100 getLogger()
101 - .error(getBundle().getString("does-not-have-role", getSecurityContext().getCurrentUser().getName(), 101 + .error(getBundle().getString("does-not-have-role", getSecurityContext().getUser().getId(),
102 roles)); 102 roles));
103 103
104 throw new AuthorizationException(getBundle().getString("does-not-have-role-ui", roles)); 104 throw new AuthorizationException(getBundle().getString("does-not-have-role-ui", roles));
105 } 105 }
106 106
107 getLogger().debug( 107 getLogger().debug(
108 - getBundle().getString("user-has-role", getSecurityContext().getCurrentUser().getName(), userRoles)); 108 + getBundle().getString("user-has-role", getSecurityContext().getUser().getId(), userRoles));
109 109
110 return ic.proceed(); 110 return ic.proceed();
111 } 111 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/SecurityContext.java
@@ -37,7 +37,6 @@ @@ -37,7 +37,6 @@
37 package br.gov.frameworkdemoiselle.security; 37 package br.gov.frameworkdemoiselle.security;
38 38
39 import java.io.Serializable; 39 import java.io.Serializable;
40 -import java.security.Principal;  
41 40
42 /** 41 /**
43 * Structure used to handle both authentication and authorizations mechanisms. 42 * Structure used to handle both authentication and authorizations mechanisms.
@@ -104,15 +103,8 @@ public interface SecurityContext extends Serializable { @@ -104,15 +103,8 @@ public interface SecurityContext extends Serializable {
104 /** 103 /**
105 * Return the user logged in the session. 104 * Return the user logged in the session.
106 * 105 *
107 - * @deprecated See {@link #getCurrentUser()}  
108 * @return the user logged in a specific session. If there is no active session returns {@code null} 106 * @return the user logged in a specific session. If there is no active session returns {@code null}
109 */ 107 */
110 User getUser(); 108 User getUser();
111 109
112 - /**  
113 - * Return the user logged in the authenticated session.  
114 - *  
115 - * @return the user logged in a specific session. If there is no active authenticated session returns {@code null}  
116 - */  
117 - Principal getCurrentUser();  
118 } 110 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/User.java
@@ -37,13 +37,11 @@ @@ -37,13 +37,11 @@
37 package br.gov.frameworkdemoiselle.security; 37 package br.gov.frameworkdemoiselle.security;
38 38
39 import java.io.Serializable; 39 import java.io.Serializable;
40 -import java.security.Principal;  
41 40
42 /** 41 /**
43 * @author SERPRO 42 * @author SERPRO
44 - * @deprecated  
45 */ 43 */
46 -public interface User extends Principal, Serializable { 44 +public interface User extends Serializable {
47 45
48 /** 46 /**
49 * Returns the id of the logged user. 47 * Returns the id of the logged user.
impl/core/src/test/java/security/athentication/ambiguity/DuplicatedCustomAuthenticator.java
@@ -36,10 +36,9 @@ @@ -36,10 +36,9 @@
36 */ 36 */
37 package security.athentication.ambiguity; 37 package security.athentication.ambiguity;
38 38
39 -import java.security.Principal;  
40 -  
41 import br.gov.frameworkdemoiselle.security.AuthenticationException; 39 import br.gov.frameworkdemoiselle.security.AuthenticationException;
42 import br.gov.frameworkdemoiselle.security.Authenticator; 40 import br.gov.frameworkdemoiselle.security.Authenticator;
  41 +import br.gov.frameworkdemoiselle.security.User;
43 42
44 public class DuplicatedCustomAuthenticator implements Authenticator { 43 public class DuplicatedCustomAuthenticator implements Authenticator {
45 44
@@ -54,7 +53,7 @@ public class DuplicatedCustomAuthenticator implements Authenticator { @@ -54,7 +53,7 @@ public class DuplicatedCustomAuthenticator implements Authenticator {
54 } 53 }
55 54
56 @Override 55 @Override
57 - public Principal getUser() { 56 + public User getUser() {
58 return null; 57 return null;
59 } 58 }
60 } 59 }
impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java
@@ -36,31 +36,40 @@ @@ -36,31 +36,40 @@
36 */ 36 */
37 package security.athentication.credentials; 37 package security.athentication.credentials;
38 38
39 -import java.security.Principal;  
40 -  
41 import br.gov.frameworkdemoiselle.security.AuthenticationException; 39 import br.gov.frameworkdemoiselle.security.AuthenticationException;
42 import br.gov.frameworkdemoiselle.security.Authenticator; 40 import br.gov.frameworkdemoiselle.security.Authenticator;
  41 +import br.gov.frameworkdemoiselle.security.User;
43 import br.gov.frameworkdemoiselle.util.Beans; 42 import br.gov.frameworkdemoiselle.util.Beans;
44 43
45 public class StrictAuthenticator implements Authenticator { 44 public class StrictAuthenticator implements Authenticator {
46 45
47 private static final long serialVersionUID = 1L; 46 private static final long serialVersionUID = 1L;
48 47
49 - private Principal currentUser; 48 + private User currentUser;
50 49
51 @Override 50 @Override
52 public void authenticate() throws AuthenticationException { 51 public void authenticate() throws AuthenticationException {
53 - 52 +
54 Credentials c = Beans.getReference(Credentials.class); 53 Credentials c = Beans.getReference(Credentials.class);
55 - if ("demoiselle".equals(c.getLogin())){  
56 - this.currentUser = new Principal() {  
57 -  
58 - public String getName() { 54 + if ("demoiselle".equals(c.getLogin())) {
  55 + this.currentUser = new User() {
  56 +
  57 + private static final long serialVersionUID = 1L;
  58 +
  59 + public String getId() {
59 return "demoiselle"; 60 return "demoiselle";
60 } 61 }
  62 +
  63 + @Override
  64 + public Object getAttribute(Object key) {
  65 + return null;
  66 + }
  67 +
  68 + @Override
  69 + public void setAttribute(Object key, Object value) {
  70 + }
61 }; 71 };
62 - }  
63 - else{ 72 + } else {
64 throw new AuthenticationException("As credenciais fornecidas não são válidas"); 73 throw new AuthenticationException("As credenciais fornecidas não são válidas");
65 } 74 }
66 } 75 }
@@ -71,7 +80,7 @@ public class StrictAuthenticator implements Authenticator { @@ -71,7 +80,7 @@ public class StrictAuthenticator implements Authenticator {
71 } 80 }
72 81
73 @Override 82 @Override
74 - public Principal getUser() { 83 + public User getUser() {
75 return this.currentUser; 84 return this.currentUser;
76 } 85 }
77 } 86 }
impl/core/src/test/java/security/athentication/custom/CustomAuthenticator.java
@@ -36,24 +36,34 @@ @@ -36,24 +36,34 @@
36 */ 36 */
37 package security.athentication.custom; 37 package security.athentication.custom;
38 38
39 -import java.security.Principal;  
40 -  
41 import br.gov.frameworkdemoiselle.security.AuthenticationException; 39 import br.gov.frameworkdemoiselle.security.AuthenticationException;
42 import br.gov.frameworkdemoiselle.security.Authenticator; 40 import br.gov.frameworkdemoiselle.security.Authenticator;
  41 +import br.gov.frameworkdemoiselle.security.User;
43 42
44 public class CustomAuthenticator implements Authenticator { 43 public class CustomAuthenticator implements Authenticator {
45 44
46 private static final long serialVersionUID = 1L; 45 private static final long serialVersionUID = 1L;
47 46
48 - private Principal currentUser; 47 + private User currentUser;
49 48
50 @Override 49 @Override
51 public void authenticate() throws AuthenticationException { 50 public void authenticate() throws AuthenticationException {
52 - this.currentUser = new Principal() { 51 + this.currentUser = new User() {
  52 +
  53 + private static final long serialVersionUID = 1L;
53 54
54 - public String getName() { 55 + public String getId() {
55 return "demoiselle"; 56 return "demoiselle";
56 } 57 }
  58 +
  59 + @Override
  60 + public Object getAttribute(Object key) {
  61 + return null;
  62 + }
  63 +
  64 + @Override
  65 + public void setAttribute(Object key, Object value) {
  66 + }
57 }; 67 };
58 } 68 }
59 69
@@ -63,7 +73,7 @@ public class CustomAuthenticator implements Authenticator { @@ -63,7 +73,7 @@ public class CustomAuthenticator implements Authenticator {
63 } 73 }
64 74
65 @Override 75 @Override
66 - public Principal getUser() { 76 + public User getUser() {
67 return this.currentUser; 77 return this.currentUser;
68 } 78 }
69 } 79 }
impl/core/src/test/java/security/athentication/custom/CustomAuthenticatorTest.java
@@ -77,7 +77,7 @@ public class CustomAuthenticatorTest { @@ -77,7 +77,7 @@ public class CustomAuthenticatorTest {
77 @Test 77 @Test
78 public void unauthenticated() { 78 public void unauthenticated() {
79 assertFalse(context.isLoggedIn()); 79 assertFalse(context.isLoggedIn());
80 - assertNull(context.getCurrentUser()); 80 + assertNull(context.getUser());
81 } 81 }
82 82
83 @Test 83 @Test
@@ -87,7 +87,7 @@ public class CustomAuthenticatorTest { @@ -87,7 +87,7 @@ public class CustomAuthenticatorTest {
87 context.login(); 87 context.login();
88 assertTrue(context.isLoggedIn()); 88 assertTrue(context.isLoggedIn());
89 assertNotNull(observer.getEvent()); 89 assertNotNull(observer.getEvent());
90 - assertEquals("demoiselle", context.getCurrentUser().getName()); 90 + assertEquals("demoiselle", context.getUser().getId());
91 91
92 ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class); 92 ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class);
93 } 93 }
@@ -99,7 +99,7 @@ public class CustomAuthenticatorTest { @@ -99,7 +99,7 @@ public class CustomAuthenticatorTest {
99 context.login(); 99 context.login();
100 context.logout(); 100 context.logout();
101 assertFalse(context.isLoggedIn()); 101 assertFalse(context.isLoggedIn());
102 - assertNull(context.getCurrentUser()); 102 + assertNull(context.getUser());
103 103
104 ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class); 104 ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class);
105 } 105 }
impl/core/src/test/java/security/athentication/disabled/DisabledAuthenticationTest.java
@@ -82,7 +82,7 @@ public class DisabledAuthenticationTest { @@ -82,7 +82,7 @@ public class DisabledAuthenticationTest {
82 @Test 82 @Test
83 public void unauthenticated() { 83 public void unauthenticated() {
84 assertTrue(context.isLoggedIn()); 84 assertTrue(context.isLoggedIn());
85 - assertEquals("demoiselle", context.getCurrentUser().getName()); 85 + assertEquals("demoiselle", context.getUser().getId());
86 } 86 }
87 87
88 @Test 88 @Test
@@ -90,7 +90,7 @@ public class DisabledAuthenticationTest { @@ -90,7 +90,7 @@ public class DisabledAuthenticationTest {
90 context.login(); 90 context.login();
91 assertTrue(context.isLoggedIn()); 91 assertTrue(context.isLoggedIn());
92 assertNull(event); 92 assertNull(event);
93 - assertEquals("demoiselle", context.getCurrentUser().getName()); 93 + assertEquals("demoiselle", context.getUser().getId());
94 } 94 }
95 95
96 // 96 //
impl/core/src/test/java/security/athentication/error/ErrorAuthenticator.java
@@ -36,10 +36,9 @@ @@ -36,10 +36,9 @@
36 */ 36 */
37 package security.athentication.error; 37 package security.athentication.error;
38 38
39 -import java.security.Principal;  
40 -  
41 import br.gov.frameworkdemoiselle.security.AuthenticationException; 39 import br.gov.frameworkdemoiselle.security.AuthenticationException;
42 import br.gov.frameworkdemoiselle.security.Authenticator; 40 import br.gov.frameworkdemoiselle.security.Authenticator;
  41 +import br.gov.frameworkdemoiselle.security.User;
43 42
44 public class ErrorAuthenticator implements Authenticator { 43 public class ErrorAuthenticator implements Authenticator {
45 44
@@ -56,9 +55,7 @@ public class ErrorAuthenticator implements Authenticator { @@ -56,9 +55,7 @@ public class ErrorAuthenticator implements Authenticator {
56 } 55 }
57 56
58 @Override 57 @Override
59 - public Principal getUser() { 58 + public User getUser() {
60 return null; 59 return null;
61 } 60 }
62 -  
63 -  
64 } 61 }
impl/core/src/test/java/security/athentication/selection/SelectedAuthenticatorTest.java
@@ -72,6 +72,6 @@ public class SelectedAuthenticatorTest { @@ -72,6 +72,6 @@ public class SelectedAuthenticatorTest {
72 @Test 72 @Test
73 public void selectedAuthenticatorStrategy() { 73 public void selectedAuthenticatorStrategy() {
74 context.login(); 74 context.login();
75 - assertEquals("demoiselle", context.getCurrentUser().getName()); 75 + assertEquals("demoiselle", context.getUser().getId());
76 } 76 }
77 } 77 }