Mercurial > dive4elements > river
annotate flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java @ 2956:d7f76f197d89
Refactor GGInA authentication
Move authentication related classes to de.intevation.fly.client.server.auth
package. Abstract the authentication classes to allow other authentications
beside WAS/GGInA.
flys-client/trunk@4936 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Bjoern Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Wed, 11 Jul 2012 13:31:56 +0000 |
parents | 192eddbbd4cf |
children | 16c71457ed43 |
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 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 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
|
27 if (uri == null) { |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 uri = "/FLYS.html"; |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 resp.sendRedirect(uri); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 @Override |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 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
|
35 throws ServletException, IOException { |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 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
|
37 this.redirectFailure(resp); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 @Override |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 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
|
42 throws ServletException, IOException { |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 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
|
48 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
49 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
|
50 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
|
51 this.redirectFailure(resp); |
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 try { |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
54 Authentication aresp = this.auth(username, password, encoding); |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
55 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
|
56 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
|
57 this.redirectFailure(resp); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
58 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 HttpSession session = req.getSession(); |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
60 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
|
61 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
|
62 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
63 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
|
64 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
65 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
|
66 } |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
67 catch(AuthenticationException e) { |
2950
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
68 //TODO User could not be authenticated |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
69 throw new ServletException(e); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
70 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
71 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
72 |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
73 private Authentication auth(String username, String password, String encoding) |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
74 throws AuthenticationException, IOException { |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
75 String auth = this.getInitParameter("authentication"); |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
76 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
|
77 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
78 } |