moto web application
リビジョン | 987341e41304019cf42a43f7f385ec12d73e1d26 (tree) |
---|---|
日時 | 2014-02-07 16:08:40 |
作者 | astoria-d <astoria-d@mail...> |
コミッター | astoria-d |
openid login complete
@@ -47,9 +47,8 @@ public class LoginFilter implements Filter { | ||
47 | 47 | String contextPath = ((HttpServletRequest)request).getContextPath(); |
48 | 48 | ((HttpServletResponse)response).sendRedirect(contextPath + "/login.jsf"); |
49 | 49 | } |
50 | - | |
50 | + | |
51 | 51 | chain.doFilter(request, response); |
52 | - | |
53 | 52 | } |
54 | 53 | |
55 | 54 | public void init(FilterConfig config) throws ServletException { |
@@ -16,10 +16,13 @@ import org.openid4java.discovery.Identifier; | ||
16 | 16 | import org.openid4java.consumer.VerificationResult; |
17 | 17 | import org.openid4java.message.ParameterList; |
18 | 18 | |
19 | - | |
20 | 19 | public class OpenIdReturn extends HttpServlet { |
21 | 20 | private static Logger log = Logger.getLogger(OpenIdReturn.class.getName()); |
22 | 21 | |
22 | + public OpenIdReturn() { | |
23 | + super(); | |
24 | + } | |
25 | + | |
23 | 26 | static Identifier returnCheck(DiscoveryInformation discoveryInformation, Map pageParameters, String returnToUrl) { |
24 | 27 | ParameterList response = new ParameterList(pageParameters); |
25 | 28 | try { |
@@ -39,19 +42,36 @@ public class OpenIdReturn extends HttpServlet { | ||
39 | 42 | HttpSession session = request.getSession(); |
40 | 43 | DiscoveryInformation di = (DiscoveryInformation) session.getAttribute("discoveryInformation"); |
41 | 44 | Identifier id = returnCheck(di, request.getParameterMap(), OpenIdBean.getReturnUrl()); |
45 | + session.removeAttribute("discoveryInformation"); | |
46 | + | |
42 | 47 | PrintWriter out = response.getWriter(); |
48 | + UserBean ubean = (UserBean)session.getAttribute("userBean"); | |
43 | 49 | |
44 | 50 | out.println("<HTML><HEAD><TITLE>open id login page</TITLE></HEAD>"); |
45 | 51 | out.println("<BODY>"); |
52 | + | |
46 | 53 | if (id == null) { |
47 | 54 | out.println("<H3>open id verification failure...</H3>"); |
48 | 55 | } |
49 | 56 | else { |
50 | - out.println("Open ID successfull : <H3>" + id.getIdentifier() + "</H3>"); | |
57 | + String openid = id.getIdentifier(); | |
58 | + String nextUrl = ubean.doOpenIdLogin(openid, session); | |
59 | + if (nextUrl == null) { | |
60 | + out.println("Open ID : <H3>" + openid + "</H3> not found..."); | |
61 | + } | |
62 | + else { | |
63 | + out.println("open id login ok. log in user : <H3>" + ubean.getUname()+ "</H3>."); | |
64 | + String contextPath = request.getContextPath(); | |
65 | + /*log.info("contextPath: " + contextPath); | |
66 | + log.info("last char: " + contextPath.charAt(contextPath.length() - 1)); | |
67 | + if (contextPath.charAt(contextPath.length() - 1) != '/') | |
68 | + contextPath += "/"; | |
69 | + */ | |
70 | + response.sendRedirect(contextPath + nextUrl); | |
71 | + } | |
51 | 72 | } |
52 | 73 | out.println("</BODY>"); |
53 | - out.println("</HTML>"); | |
74 | + out.println("</HTML>"); | |
54 | 75 | } |
55 | 76 | |
56 | - | |
57 | 77 | } |
@@ -92,6 +92,34 @@ public class UserBean implements Serializable { | ||
92 | 92 | } |
93 | 93 | } |
94 | 94 | |
95 | + private void initLogin(Connection conn, String user_name) { | |
96 | + login = true; | |
97 | + this.uname = user_name; | |
98 | + initUserFlights(conn); | |
99 | + | |
100 | + //add to the app login list. | |
101 | + loginMemberBean.login(uid, true); | |
102 | + //notify log in message | |
103 | + for (SelectItem si : flights) { | |
104 | + String chatRoom = si.getValue().toString(); | |
105 | + | |
106 | + PushMsg pm = PushMsg.newLoginPushMsg(); | |
107 | + PushMsg.LoginUser lu = pm.getLoginUser(); | |
108 | + lu.setUid(uid); | |
109 | + lu.setLogin(true); | |
110 | + | |
111 | + String br_url = "/comet/" + chatRoom; | |
112 | + Broadcaster b = BroadcasterFactory.getDefault().lookup(br_url, true); | |
113 | + ObjectMapper mapper = new ObjectMapper(); | |
114 | + try { | |
115 | + String json = mapper.writeValueAsString(pm); | |
116 | + b.broadcast(json); | |
117 | + } catch (IOException e) { | |
118 | + log.severe("json encode error."); | |
119 | + } | |
120 | + } | |
121 | + } | |
122 | + | |
95 | 123 | public String doLogin() { |
96 | 124 | |
97 | 125 | try { |
@@ -111,36 +139,11 @@ public class UserBean implements Serializable { | ||
111 | 139 | //log.info("name = " + rs.getString("user_name")); |
112 | 140 | |
113 | 141 | if (pwd.equals(db_pwd)) { |
114 | - login = true; | |
115 | - this.uname = rs.getString("user_name"); | |
142 | + initLogin(conn, rs.getString("user_name")); | |
143 | + | |
116 | 144 | statement.close(); |
117 | - | |
118 | - initUserFlights(conn); | |
119 | - | |
120 | 145 | conn.close(); |
121 | 146 | |
122 | - //add to the app login list. | |
123 | - loginMemberBean.login(uid, true); | |
124 | - //notify log in message | |
125 | - for (SelectItem si : flights) { | |
126 | - String chatRoom = si.getValue().toString(); | |
127 | - | |
128 | - PushMsg pm = PushMsg.newLoginPushMsg(); | |
129 | - PushMsg.LoginUser lu = pm.getLoginUser(); | |
130 | - lu.setUid(uid); | |
131 | - lu.setLogin(true); | |
132 | - | |
133 | - String br_url = "/comet/" + chatRoom; | |
134 | - Broadcaster b = BroadcasterFactory.getDefault().lookup(br_url, true); | |
135 | - ObjectMapper mapper = new ObjectMapper(); | |
136 | - try { | |
137 | - String json = mapper.writeValueAsString(pm); | |
138 | - b.broadcast(json); | |
139 | - } catch (IOException e) { | |
140 | - log.severe("json encode error."); | |
141 | - } | |
142 | - } | |
143 | - | |
144 | 147 | ///after login, redirect to the user specified url. |
145 | 148 | HttpSession session = Resources.getSession(); |
146 | 149 | String request_url = (String) session.getAttribute("request_url"); |
@@ -166,6 +169,43 @@ public class UserBean implements Serializable { | ||
166 | 169 | return ""; |
167 | 170 | } |
168 | 171 | |
172 | + | |
173 | + public String doOpenIdLogin (String openid, HttpSession session) { | |
174 | + | |
175 | + try { | |
176 | + Connection conn = Resources.getConnection(); | |
177 | + | |
178 | + String sqlString = "select user_id, user_name from tb_users where use_open_id=\'TRUE\' and user_open_id=\'" + openid +"\'"; | |
179 | + | |
180 | + Statement statement = conn.createStatement(); | |
181 | + statement.setQueryTimeout(30); // set timeout to 30 sec. | |
182 | + | |
183 | + ResultSet rs = statement.executeQuery(sqlString); | |
184 | + if(rs.next()) | |
185 | + { | |
186 | + this.uid = rs.getString("user_id"); | |
187 | + initLogin(conn, rs.getString("user_name")); | |
188 | + statement.close(); | |
189 | + conn.close(); | |
190 | + | |
191 | + ///after login, redirect to the user specified url. | |
192 | + String request_url = (String) session.getAttribute("request_url"); | |
193 | + if (request_url == null) | |
194 | + request_url = "/secured/chat.jsf"; | |
195 | + return request_url + "?faces-redirect=true"; | |
196 | + } | |
197 | + else { | |
198 | + log.info("openid " + openid + " not found..."); | |
199 | + } | |
200 | + statement.close(); | |
201 | + conn.close(); | |
202 | + } | |
203 | + catch (SQLException se) { | |
204 | + log.severe("sql err!!!"); | |
205 | + } | |
206 | + return null; | |
207 | + } | |
208 | + | |
169 | 209 | public String doLogout() { |
170 | 210 | //notify log in message |
171 | 211 | for (SelectItem si : flights) { |
@@ -18,6 +18,15 @@ password : <h:inputSecret value="#{userBean.pwd}"/> | ||
18 | 18 | |
19 | 19 | </h:form> |
20 | 20 | |
21 | +<br /> | |
22 | +<br /> | |
23 | +<br /> | |
24 | +<hr /> | |
25 | +<br /> | |
26 | +<br /> | |
27 | +<br /> | |
28 | +Open ID log in: | |
29 | +<br /> | |
21 | 30 | <h:form> |
22 | 31 | <h:commandLink action="#{openIdBean.yahooLogin}"> |
23 | 32 | <img src="http://i.yimg.jp/images/login/btn/btnXSYid.gif" width="241" height="28" alt="Yahoo! JAPAN IDでログイン" border="0" /> |