Commit 850cc214dd8d7d9bc6952abb19670da7467913ab
1 parent
f559466d
Exists in
master
IN PROGRESS - issue FWK-119: Testes da extensão Servlet
https://demoiselle.atlassian.net/browse/FWK-119
Showing
4 changed files
with
12 additions
and
26 deletions
Show diff stats
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/security/ServletAuthenticator.java
| ... | ... | @@ -65,11 +65,14 @@ public class ServletAuthenticator implements Authenticator { |
| 65 | 65 | @Override |
| 66 | 66 | public void authenticate() throws AuthenticationException { |
| 67 | 67 | try { |
| 68 | - if (getRequest().getUserPrincipal() == null) { | |
| 69 | - getRequest().login(getCredentials().getUsername(), getCredentials().getPassword()); | |
| 70 | - } | |
| 68 | + getRequest().login(getCredentials().getUsername(), getCredentials().getPassword()); | |
| 69 | + | |
| 71 | 70 | } catch (ServletException cause) { |
| 72 | - throw new AuthenticationException(getBundle().getString("authentication-failed"), cause); | |
| 71 | + if (cause.getMessage().contains("invalid")) { | |
| 72 | + throw new InvalidCredentialsException(getBundle().getString("invalid-credentials")); | |
| 73 | + } else { | |
| 74 | + throw new AuthenticationException(getBundle().getString("authentication-failed"), cause); | |
| 75 | + } | |
| 73 | 76 | } |
| 74 | 77 | } |
| 75 | 78 | ... | ... |
impl/extension/servlet/src/main/resources/demoiselle-servlet-bundle.properties
| ... | ... | @@ -34,4 +34,5 @@ |
| 34 | 34 | # 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 35 | 35 | |
| 36 | 36 | has-permission-not-supported=N\u00E3o \u00E9 poss\u00EDvel utilizar @{0}, pois esta funcionalidade n\u00E3o \u00E9 suportada pelo JAAS. |
| 37 | +invalid-credentials=Usu\u00E1rio ou senha inv\u00E1lidos. | |
| 37 | 38 | authentication-failed=Falha no processo de autentica\u00E7\u00E3o. | ... | ... |
impl/extension/servlet/src/test/java/security/authentication/form/HelperServlet.java
| ... | ... | @@ -2,13 +2,9 @@ package security.authentication.form; |
| 2 | 2 | |
| 3 | 3 | import static org.apache.http.HttpStatus.SC_EXPECTATION_FAILED; |
| 4 | 4 | import static org.apache.http.HttpStatus.SC_FORBIDDEN; |
| 5 | -import static org.apache.http.HttpStatus.SC_NOT_FOUND; | |
| 6 | 5 | import static org.apache.http.HttpStatus.SC_OK; |
| 7 | 6 | |
| 8 | 7 | import java.io.IOException; |
| 9 | -import java.security.InvalidParameterException; | |
| 10 | -import java.util.regex.Matcher; | |
| 11 | -import java.util.regex.Pattern; | |
| 12 | 8 | |
| 13 | 9 | import javax.servlet.ServletException; |
| 14 | 10 | import javax.servlet.http.HttpServlet; |
| ... | ... | @@ -26,14 +22,10 @@ public class HelperServlet extends HttpServlet { |
| 26 | 22 | |
| 27 | 23 | @Override |
| 28 | 24 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 29 | - String action = getAction(request); | |
| 30 | - | |
| 31 | - if ("login".equals(action)) { | |
| 25 | + if (request.getRequestURI().endsWith("/login")) { | |
| 32 | 26 | login(request, response); |
| 33 | - } else if ("logout".equals(action)) { | |
| 34 | - logout(request, response); | |
| 35 | 27 | } else { |
| 36 | - response.setStatus(SC_NOT_FOUND); | |
| 28 | + logout(request, response); | |
| 37 | 29 | } |
| 38 | 30 | } |
| 39 | 31 | |
| ... | ... | @@ -74,15 +66,4 @@ public class HelperServlet extends HttpServlet { |
| 74 | 66 | credentials.setUsername(request.getParameter("username")); |
| 75 | 67 | credentials.setPassword(request.getParameter("password")); |
| 76 | 68 | } |
| 77 | - | |
| 78 | - private String getAction(HttpServletRequest request) { | |
| 79 | - Pattern pattern = Pattern.compile("^.+/(.+)$"); | |
| 80 | - Matcher matcher = pattern.matcher(request.getRequestURI()); | |
| 81 | - | |
| 82 | - if (matcher.matches()) { | |
| 83 | - return matcher.group(1).toLowerCase(); | |
| 84 | - } else { | |
| 85 | - throw new InvalidParameterException("Está faltando o parâmetro de ação na URL"); | |
| 86 | - } | |
| 87 | - } | |
| 88 | 69 | } | ... | ... |
impl/extension/servlet/src/test/resources/security/authentication/form/web.xml
| ... | ... | @@ -56,6 +56,7 @@ |
| 56 | 56 | </servlet> |
| 57 | 57 | <servlet-mapping> |
| 58 | 58 | <servlet-name>Helper Servlet</servlet-name> |
| 59 | - <url-pattern>/helper/*</url-pattern> | |
| 59 | + <url-pattern>/helper/login</url-pattern> | |
| 60 | + <url-pattern>/helper/logout</url-pattern> | |
| 60 | 61 | </servlet-mapping> |
| 61 | 62 | </web-app> |
| 62 | 63 | \ No newline at end of file | ... | ... |