Commit b54b43558563c07d81beb6f00064a9fe2b1823b5
1 parent
32dfc52f
Exists in
master
Removendo o método "Principal getCurrentUser()" e suas referencias
Showing
14 changed files
with
69 additions
and
70 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultAuthenticator.java
... | ... | @@ -37,14 +37,12 @@ |
37 | 37 | package br.gov.frameworkdemoiselle.internal.implementation; |
38 | 38 | |
39 | 39 | import static br.gov.frameworkdemoiselle.annotation.Priority.L1_PRIORITY; |
40 | - | |
41 | -import java.security.Principal; | |
42 | - | |
43 | 40 | import br.gov.frameworkdemoiselle.DemoiselleException; |
44 | 41 | import br.gov.frameworkdemoiselle.annotation.Priority; |
45 | 42 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
46 | 43 | import br.gov.frameworkdemoiselle.security.Authenticator; |
47 | 44 | import br.gov.frameworkdemoiselle.security.SecurityContext; |
45 | +import br.gov.frameworkdemoiselle.security.User; | |
48 | 46 | import br.gov.frameworkdemoiselle.util.Beans; |
49 | 47 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
50 | 48 | import br.gov.frameworkdemoiselle.util.ResourceBundle; |
... | ... | @@ -82,7 +80,7 @@ public class DefaultAuthenticator implements Authenticator { |
82 | 80 | * @see br.gov.frameworkdemoiselle.security.Authenticator#getUser() |
83 | 81 | */ |
84 | 82 | @Override |
85 | - public Principal getUser() { | |
83 | + public User getUser() { | |
86 | 84 | throw getException(); |
87 | 85 | } |
88 | 86 | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java
... | ... | @@ -36,12 +36,8 @@ |
36 | 36 | */ |
37 | 37 | package br.gov.frameworkdemoiselle.internal.implementation; |
38 | 38 | |
39 | -import java.io.Serializable; | |
40 | -import java.security.Principal; | |
41 | - | |
42 | 39 | import javax.inject.Named; |
43 | 40 | |
44 | -import br.gov.frameworkdemoiselle.DemoiselleException; | |
45 | 41 | import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig; |
46 | 42 | import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful; |
47 | 43 | import br.gov.frameworkdemoiselle.security.AfterLogoutSuccessful; |
... | ... | @@ -60,7 +56,6 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; |
60 | 56 | * |
61 | 57 | * @author SERPRO |
62 | 58 | */ |
63 | -@SuppressWarnings("deprecation") | |
64 | 59 | @Named("securityContext") |
65 | 60 | public class SecurityContextImpl implements SecurityContext { |
66 | 61 | |
... | ... | @@ -137,7 +132,7 @@ public class SecurityContextImpl implements SecurityContext { |
137 | 132 | boolean result = true; |
138 | 133 | |
139 | 134 | if (getConfig().isEnabled()) { |
140 | - result = getCurrentUser() != null; | |
135 | + result = getUser() != null; | |
141 | 136 | } |
142 | 137 | |
143 | 138 | return result; |
... | ... | @@ -176,17 +171,11 @@ public class SecurityContextImpl implements SecurityContext { |
176 | 171 | } |
177 | 172 | |
178 | 173 | /** |
179 | - * @deprecated Use {@link #getCurrentUser()} instead. | |
180 | 174 | * @see br.gov.frameworkdemoiselle.security.SecurityContext#getUser() |
181 | 175 | */ |
182 | 176 | @Override |
183 | 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 | 180 | if (!getConfig().isEnabled() && user == null) { |
192 | 181 | user = new EmptyUser(); |
... | ... | @@ -213,13 +202,22 @@ public class SecurityContextImpl implements SecurityContext { |
213 | 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 | 207 | private static final long serialVersionUID = 1L; |
219 | 208 | |
220 | 209 | @Override |
221 | - public String getName() { | |
210 | + public String getId() { | |
222 | 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 | 37 | package br.gov.frameworkdemoiselle.security; |
38 | 38 | |
39 | 39 | import java.io.Serializable; |
40 | -import java.security.Principal; | |
41 | 40 | |
42 | 41 | /** |
43 | 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 | 63 | * |
65 | 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 | 37 | package br.gov.frameworkdemoiselle.security; |
38 | 38 | |
39 | 39 | import java.io.Serializable; |
40 | -import java.security.Principal; | |
41 | 40 | |
42 | 41 | import javax.interceptor.AroundInvoke; |
43 | 42 | import javax.interceptor.Interceptor; |
... | ... | @@ -106,10 +105,10 @@ public class RequiredPermissionInterceptor implements Serializable { |
106 | 105 | */ |
107 | 106 | private String getUsername() { |
108 | 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 | 114 | return username; | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/RequiredRoleInterceptor.java
... | ... | @@ -84,7 +84,7 @@ public class RequiredRoleInterceptor implements Serializable { |
84 | 84 | |
85 | 85 | if (getSecurityContext().isLoggedIn()) { |
86 | 86 | getLogger().info( |
87 | - getBundle().getString("has-role-verification", getSecurityContext().getCurrentUser().getName(), | |
87 | + getBundle().getString("has-role-verification", getSecurityContext().getUser().getId(), | |
88 | 88 | roles)); |
89 | 89 | } |
90 | 90 | |
... | ... | @@ -98,14 +98,14 @@ public class RequiredRoleInterceptor implements Serializable { |
98 | 98 | |
99 | 99 | if (userRoles.isEmpty()) { |
100 | 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 | 102 | roles)); |
103 | 103 | |
104 | 104 | throw new AuthorizationException(getBundle().getString("does-not-have-role-ui", roles)); |
105 | 105 | } |
106 | 106 | |
107 | 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 | 110 | return ic.proceed(); |
111 | 111 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/security/SecurityContext.java
... | ... | @@ -37,7 +37,6 @@ |
37 | 37 | package br.gov.frameworkdemoiselle.security; |
38 | 38 | |
39 | 39 | import java.io.Serializable; |
40 | -import java.security.Principal; | |
41 | 40 | |
42 | 41 | /** |
43 | 42 | * Structure used to handle both authentication and authorizations mechanisms. |
... | ... | @@ -104,15 +103,8 @@ public interface SecurityContext extends Serializable { |
104 | 103 | /** |
105 | 104 | * Return the user logged in the session. |
106 | 105 | * |
107 | - * @deprecated See {@link #getCurrentUser()} | |
108 | 106 | * @return the user logged in a specific session. If there is no active session returns {@code null} |
109 | 107 | */ |
110 | 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 | 37 | package br.gov.frameworkdemoiselle.security; |
38 | 38 | |
39 | 39 | import java.io.Serializable; |
40 | -import java.security.Principal; | |
41 | 40 | |
42 | 41 | /** |
43 | 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 | 47 | * Returns the id of the logged user. | ... | ... |
impl/core/src/test/java/security/athentication/ambiguity/DuplicatedCustomAuthenticator.java
... | ... | @@ -36,10 +36,9 @@ |
36 | 36 | */ |
37 | 37 | package security.athentication.ambiguity; |
38 | 38 | |
39 | -import java.security.Principal; | |
40 | - | |
41 | 39 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
42 | 40 | import br.gov.frameworkdemoiselle.security.Authenticator; |
41 | +import br.gov.frameworkdemoiselle.security.User; | |
43 | 42 | |
44 | 43 | public class DuplicatedCustomAuthenticator implements Authenticator { |
45 | 44 | |
... | ... | @@ -54,7 +53,7 @@ public class DuplicatedCustomAuthenticator implements Authenticator { |
54 | 53 | } |
55 | 54 | |
56 | 55 | @Override |
57 | - public Principal getUser() { | |
56 | + public User getUser() { | |
58 | 57 | return null; |
59 | 58 | } |
60 | 59 | } | ... | ... |
impl/core/src/test/java/security/athentication/credentials/StrictAuthenticator.java
... | ... | @@ -36,31 +36,40 @@ |
36 | 36 | */ |
37 | 37 | package security.athentication.credentials; |
38 | 38 | |
39 | -import java.security.Principal; | |
40 | - | |
41 | 39 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
42 | 40 | import br.gov.frameworkdemoiselle.security.Authenticator; |
41 | +import br.gov.frameworkdemoiselle.security.User; | |
43 | 42 | import br.gov.frameworkdemoiselle.util.Beans; |
44 | 43 | |
45 | 44 | public class StrictAuthenticator implements Authenticator { |
46 | 45 | |
47 | 46 | private static final long serialVersionUID = 1L; |
48 | 47 | |
49 | - private Principal currentUser; | |
48 | + private User currentUser; | |
50 | 49 | |
51 | 50 | @Override |
52 | 51 | public void authenticate() throws AuthenticationException { |
53 | - | |
52 | + | |
54 | 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 | 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 | 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 | 80 | } |
72 | 81 | |
73 | 82 | @Override |
74 | - public Principal getUser() { | |
83 | + public User getUser() { | |
75 | 84 | return this.currentUser; |
76 | 85 | } |
77 | 86 | } | ... | ... |
impl/core/src/test/java/security/athentication/custom/CustomAuthenticator.java
... | ... | @@ -36,24 +36,34 @@ |
36 | 36 | */ |
37 | 37 | package security.athentication.custom; |
38 | 38 | |
39 | -import java.security.Principal; | |
40 | - | |
41 | 39 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
42 | 40 | import br.gov.frameworkdemoiselle.security.Authenticator; |
41 | +import br.gov.frameworkdemoiselle.security.User; | |
43 | 42 | |
44 | 43 | public class CustomAuthenticator implements Authenticator { |
45 | 44 | |
46 | 45 | private static final long serialVersionUID = 1L; |
47 | 46 | |
48 | - private Principal currentUser; | |
47 | + private User currentUser; | |
49 | 48 | |
50 | 49 | @Override |
51 | 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 | 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 | 73 | } |
64 | 74 | |
65 | 75 | @Override |
66 | - public Principal getUser() { | |
76 | + public User getUser() { | |
67 | 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 | 77 | @Test |
78 | 78 | public void unauthenticated() { |
79 | 79 | assertFalse(context.isLoggedIn()); |
80 | - assertNull(context.getCurrentUser()); | |
80 | + assertNull(context.getUser()); | |
81 | 81 | } |
82 | 82 | |
83 | 83 | @Test |
... | ... | @@ -87,7 +87,7 @@ public class CustomAuthenticatorTest { |
87 | 87 | context.login(); |
88 | 88 | assertTrue(context.isLoggedIn()); |
89 | 89 | assertNotNull(observer.getEvent()); |
90 | - assertEquals("demoiselle", context.getCurrentUser().getName()); | |
90 | + assertEquals("demoiselle", context.getUser().getId()); | |
91 | 91 | |
92 | 92 | ContextManager.deactivate(ThreadLocalContext.class, RequestScoped.class); |
93 | 93 | } |
... | ... | @@ -99,7 +99,7 @@ public class CustomAuthenticatorTest { |
99 | 99 | context.login(); |
100 | 100 | context.logout(); |
101 | 101 | assertFalse(context.isLoggedIn()); |
102 | - assertNull(context.getCurrentUser()); | |
102 | + assertNull(context.getUser()); | |
103 | 103 | |
104 | 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 | 82 | @Test |
83 | 83 | public void unauthenticated() { |
84 | 84 | assertTrue(context.isLoggedIn()); |
85 | - assertEquals("demoiselle", context.getCurrentUser().getName()); | |
85 | + assertEquals("demoiselle", context.getUser().getId()); | |
86 | 86 | } |
87 | 87 | |
88 | 88 | @Test |
... | ... | @@ -90,7 +90,7 @@ public class DisabledAuthenticationTest { |
90 | 90 | context.login(); |
91 | 91 | assertTrue(context.isLoggedIn()); |
92 | 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 | 36 | */ |
37 | 37 | package security.athentication.error; |
38 | 38 | |
39 | -import java.security.Principal; | |
40 | - | |
41 | 39 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
42 | 40 | import br.gov.frameworkdemoiselle.security.Authenticator; |
41 | +import br.gov.frameworkdemoiselle.security.User; | |
43 | 42 | |
44 | 43 | public class ErrorAuthenticator implements Authenticator { |
45 | 44 | |
... | ... | @@ -56,9 +55,7 @@ public class ErrorAuthenticator implements Authenticator { |
56 | 55 | } |
57 | 56 | |
58 | 57 | @Override |
59 | - public Principal getUser() { | |
58 | + public User getUser() { | |
60 | 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 | 72 | @Test |
73 | 73 | public void selectedAuthenticatorStrategy() { |
74 | 74 | context.login(); |
75 | - assertEquals("demoiselle", context.getCurrentUser().getName()); | |
75 | + assertEquals("demoiselle", context.getUser().getId()); | |
76 | 76 | } |
77 | 77 | } | ... | ... |