annotate flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java @ 2970:b89dd09b486c

Also log an authentication failure flys-client/trunk@4966 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 13 Jul 2012 06:57:00 +0000
parents 16c71457ed43
children 6266dff93ed2
rev   line source
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.client.server;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
2
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
3 import java.io.IOException;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
4
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
5 import javax.servlet.ServletException;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
6 import javax.servlet.http.HttpServlet;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
7 import javax.servlet.http.HttpServletRequest;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
8 import javax.servlet.http.HttpServletResponse;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
9 import javax.servlet.http.HttpSession;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
10
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
11 import org.apache.log4j.Logger;
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
12
2956
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
13 import de.intevation.flys.client.server.auth.Authentication;
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
14 import de.intevation.flys.client.server.auth.AuthenticationException;
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
15 import de.intevation.flys.client.server.auth.AuthenticationFactory;
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
16 import de.intevation.flys.client.server.auth.User;
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
17
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
18 public class LoginServlet extends HttpServlet {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
19
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
20 private static Logger logger = Logger.getLogger(LoginServlet.class);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
21
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
22 private void redirectFailure(HttpServletResponse resp) throws IOException {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
23 resp.sendRedirect("/login.jsp");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
24 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
25
2969
16c71457ed43 Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2956
diff changeset
26 private void redirectFailure(HttpServletResponse resp, Exception e) throws IOException {
16c71457ed43 Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2956
diff changeset
27 resp.sendRedirect("/login.jsp?error=" + e.getMessage());
16c71457ed43 Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2956
diff changeset
28 }
16c71457ed43 Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2956
diff changeset
29
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
30 private void redirectSuccess(HttpServletResponse resp, String uri) throws IOException {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
31 if (uri == null) {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
32 uri = "/FLYS.html";
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
33 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
34 resp.sendRedirect(uri);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
35 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
36
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
37 @Override
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
38 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
39 throws ServletException, IOException {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
40 logger.debug("Processing get request");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
41 this.redirectFailure(resp);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
42 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
43
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
44 @Override
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
45 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
46 throws ServletException, IOException {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
47 String encoding = req.getCharacterEncoding();
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
48 String username = req.getParameter("username");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
49 String password = req.getParameter("password");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
50
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
51 logger.debug("Processing post request");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
52
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
53 if (username == null || password == null) {
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
54 logger.debug("No username or password provided");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
55 this.redirectFailure(resp);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
56 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
57 try {
2956
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
58 Authentication aresp = this.auth(username, password, encoding);
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
59 if (aresp == null || !aresp.isSuccess()) {
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
60 logger.debug("Athentication not successful");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
61 this.redirectFailure(resp);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
62 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
63 HttpSession session = req.getSession();
2956
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
64 User user = aresp.getUser();
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
65 session.setAttribute("user", user);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
66
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
67 String uri = (String)session.getAttribute("requesturi");
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
68
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
69 this.redirectSuccess(resp, uri);
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
70 }
2956
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
71 catch(AuthenticationException e) {
2970
b89dd09b486c Also log an authentication failure
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2969
diff changeset
72 logger.error(e);
2969
16c71457ed43 Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2956
diff changeset
73 this.redirectFailure(resp, e);
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
74 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
75 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
76
2956
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
77 private Authentication auth(String username, String password, String encoding)
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
78 throws AuthenticationException, IOException {
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
79 String auth = this.getInitParameter("authentication");
d7f76f197d89 Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents: 2950
diff changeset
80 return AuthenticationFactory.getInstance(auth).auth(username, password, encoding);
2950
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
81 }
192eddbbd4cf Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff changeset
82 }

http://dive4elements.wald.intevation.org