Mercurial > dive4elements > river
annotate flys-client/src/main/java/de/intevation/flys/client/server/LoginServlet.java @ 3497:88feb3347aa5
Implement a ProxyServlet
Implement a ProxyServlet to be able to restrict the access to the
mapserver too. All queries to the provided map services should go throught
this new ProxyServlet. Currently the ProxyServlet can only handle HTTP GET
requests.
flys-client/trunk@5221 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Bjoern Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Thu, 16 Aug 2012 14:42:36 +0000 |
parents | 71ba3cf3ec5e |
children | 56f17c6c6288 |
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; |
3485
71ba3cf3ec5e
Refactor Authentication to allow to pass the Freatures to the user class
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2984
diff
changeset
|
6 import javax.servlet.ServletContext; |
2950
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.HttpServlet; |
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.HttpServletRequest; |
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.HttpServletResponse; |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 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
|
11 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 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
|
13 |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
14 import de.intevation.flys.client.server.auth.Authentication; |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
15 import de.intevation.flys.client.server.auth.AuthenticationException; |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
16 import de.intevation.flys.client.server.auth.AuthenticationFactory; |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
17 import de.intevation.flys.client.server.auth.User; |
2984
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
18 import de.intevation.flys.client.server.auth.UserClient; |
3485
71ba3cf3ec5e
Refactor Authentication to allow to pass the Freatures to the user class
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2984
diff
changeset
|
19 import de.intevation.flys.client.server.features.Features; |
2950
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 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
|
22 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 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
|
24 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 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
|
26 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
|
27 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 |
2969
16c71457ed43
Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2956
diff
changeset
|
29 private void redirectFailure(HttpServletResponse resp, Exception e) throws IOException { |
2984
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
30 this.redirectFailure(resp, e.getMessage()); |
2969
16c71457ed43
Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2956
diff
changeset
|
31 } |
16c71457ed43
Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2956
diff
changeset
|
32 |
2984
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
33 private void redirectFailure(HttpServletResponse resp, String message) throws IOException { |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
34 resp.sendRedirect("/login.jsp?error=" + message); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
35 } |
2950
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 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
|
37 if (uri == null) { |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 uri = "/FLYS.html"; |
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 resp.sendRedirect(uri); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 } |
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 @Override |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 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
|
45 throws ServletException, IOException { |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
46 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
|
47 this.redirectFailure(resp); |
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 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
50 @Override |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
51 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
|
52 throws ServletException, IOException { |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
57 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
|
58 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 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
|
60 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
|
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 try { |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
64 Authentication aresp = this.auth(username, password, encoding); |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
65 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
|
66 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
|
67 this.redirectFailure(resp); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
68 } |
2984
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
69 User user = aresp.getUser(); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
70 |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
71 String url = getServletContext().getInitParameter("server-url"); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
72 UserClient client = new UserClient(url); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
73 if (!client.userExists(user)) { |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
74 if (!client.createUser(user)) { |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
75 this.redirectFailure(resp, "Could not create new user"); |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
76 } |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
77 } |
06d999e95615
Add UserClient class to handle REST communication for user related interfaces.
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2972
diff
changeset
|
78 |
2950
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
79 HttpSession session = req.getSession(); |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
80 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
|
81 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
82 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
|
83 |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
84 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
|
85 } |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
86 catch(AuthenticationException e) { |
2970
b89dd09b486c
Also log an authentication failure
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2969
diff
changeset
|
87 logger.error(e); |
2969
16c71457ed43
Display error details to the user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2956
diff
changeset
|
88 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
|
89 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
90 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
91 |
2956
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
92 private Authentication auth(String username, String password, String encoding) |
d7f76f197d89
Refactor GGInA authentication
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2950
diff
changeset
|
93 throws AuthenticationException, IOException { |
3485
71ba3cf3ec5e
Refactor Authentication to allow to pass the Freatures to the user class
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2984
diff
changeset
|
94 ServletContext sc = this.getServletContext(); |
71ba3cf3ec5e
Refactor Authentication to allow to pass the Freatures to the user class
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2984
diff
changeset
|
95 Features features = (Features)sc.getAttribute(Features.CONTEXT_ATTRIBUTE); |
71ba3cf3ec5e
Refactor Authentication to allow to pass the Freatures to the user class
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2984
diff
changeset
|
96 String auth = sc.getInitParameter("authentication"); |
71ba3cf3ec5e
Refactor Authentication to allow to pass the Freatures to the user class
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
2984
diff
changeset
|
97 return AuthenticationFactory.getInstance(auth).auth(username, password, encoding, features); |
2950
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
98 } |
192eddbbd4cf
Implement a login page to be able to authenticate a user
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
99 } |