Commit 8fbfdea52598ae566beb0d67fa65515e4443ca06
1 parent
735de5a4
Exists in
master
FWK-213: Implementação padrão do TokenManager em @ApplicationScoped (não
recomendado em produção) Task-Url: https://demoiselle.atlassian.net/browse/FWK-213
Showing
4 changed files
with
17 additions
and
37 deletions
Show diff stats
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/security/AppAuthenticator.java
| ... | ... | @@ -2,45 +2,34 @@ package ${package}.security; |
| 2 | 2 | |
| 3 | 3 | import java.security.Principal; |
| 4 | 4 | |
| 5 | -import javax.enterprise.context.RequestScoped; | |
| 6 | - | |
| 7 | -import br.gov.frameworkdemoiselle.security.Authenticator; | |
| 8 | 5 | import br.gov.frameworkdemoiselle.security.Credentials; |
| 9 | 6 | import br.gov.frameworkdemoiselle.security.InvalidCredentialsException; |
| 7 | +import br.gov.frameworkdemoiselle.security.TokenAuthenticator; | |
| 10 | 8 | import br.gov.frameworkdemoiselle.util.Beans; |
| 11 | 9 | |
| 12 | -@RequestScoped | |
| 13 | -public class AppAuthenticator implements Authenticator { | |
| 10 | +public class AppAuthenticator extends TokenAuthenticator { | |
| 14 | 11 | |
| 15 | 12 | private static final long serialVersionUID = 1L; |
| 16 | 13 | |
| 17 | - private Principal user; | |
| 18 | - | |
| 19 | 14 | @Override |
| 20 | - public void authenticate() throws Exception { | |
| 15 | + protected Principal customAuthentication() throws Exception { | |
| 16 | + Principal user = null; | |
| 21 | 17 | final Credentials credentials = Beans.getReference(Credentials.class); |
| 18 | + final String username = credentials.getUsername(); | |
| 22 | 19 | |
| 23 | 20 | if (credentials.getPassword().equals("secret")) { |
| 24 | - this.user = new Principal() { | |
| 21 | + user = new Principal() { | |
| 25 | 22 | |
| 26 | 23 | @Override |
| 27 | 24 | public String getName() { |
| 28 | - return credentials.getUsername(); | |
| 25 | + return username; | |
| 29 | 26 | } |
| 30 | 27 | }; |
| 31 | 28 | |
| 32 | 29 | } else { |
| 33 | 30 | throw new InvalidCredentialsException(); |
| 34 | 31 | } |
| 35 | - } | |
| 36 | 32 | |
| 37 | - @Override | |
| 38 | - public void unauthenticate() throws Exception { | |
| 39 | - this.user = null; | |
| 40 | - } | |
| 41 | - | |
| 42 | - @Override | |
| 43 | - public Principal getUser() { | |
| 44 | - return this.user; | |
| 33 | + return user; | |
| 45 | 34 | } |
| 46 | 35 | } | ... | ... |
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js
| ... | ... | @@ -19,18 +19,8 @@ $(function() { |
| 19 | 19 | }); |
| 20 | 20 | }); |
| 21 | 21 | |
| 22 | -function make_base_auth(user, password) { | |
| 23 | - var tok = user + ':' + password; | |
| 24 | - var hash = btoa(tok); | |
| 25 | - return hash; | |
| 26 | -} | |
| 27 | - | |
| 28 | -function loginOk(data) { | |
| 29 | - var username = $("#username").val().trim(); | |
| 30 | - var password = $("#password").val().trim(); | |
| 31 | - var encoded = btoa(username + ':' + password); | |
| 32 | - | |
| 33 | - App.setToken(encoded); | |
| 22 | +function loginOk(data, status, request) { | |
| 23 | + App.setToken(request.getResponseHeader('Set-Token')); | |
| 34 | 24 | location.href = "home.html"; |
| 35 | 25 | } |
| 36 | 26 | ... | ... |
archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js
| ... | ... | @@ -20,21 +20,22 @@ var App = { |
| 20 | 20 | }, |
| 21 | 21 | |
| 22 | 22 | setToken : function(token) { |
| 23 | + console.log(token); | |
| 23 | 24 | sessionStorage.setItem(this.tokenKey, token); |
| 24 | 25 | }, |
| 25 | 26 | |
| 26 | 27 | setHeader : function(request) { |
| 27 | - request.setRequestHeader("Authorization", "Basic " + App.getToken()); | |
| 28 | + request.setRequestHeader("Authorization", "Token " + App.getToken()); | |
| 28 | 29 | }, |
| 29 | 30 | |
| 30 | 31 | removeToken : function() { |
| 31 | 32 | sessionStorage.removeItem(this.tokenKey); |
| 33 | + $.removeCookie("Token"); | |
| 32 | 34 | }, |
| 33 | - | |
| 35 | + | |
| 34 | 36 | getUrlParameterByName : function(name) { |
| 35 | 37 | name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); |
| 36 | - var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
| 37 | - results = regex.exec(location.search); | |
| 38 | - return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
| 38 | + var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); | |
| 39 | + return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
| 39 | 40 | } |
| 40 | 41 | }; | ... | ... |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultTokenManager.java
| ... | ... | @@ -66,7 +66,7 @@ public class DefaultTokenManager implements TokenManager { |
| 66 | 66 | return store.get(token); |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | - private class TokenStore implements Serializable { | |
| 69 | + public static class TokenStore implements Serializable { | |
| 70 | 70 | |
| 71 | 71 | private static final long serialVersionUID = 1L; |
| 72 | 72 | ... | ... |