-
Notifications
You must be signed in to change notification settings - Fork 2
Create server2.java #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package com.example.server; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.http.Cookie; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.sql.*; | ||
|
|
||
| public class Server extends HttpServlet { | ||
|
|
||
| static final String DB_URL = "jdbc:mysql://localhost/users"; | ||
| static Connection conn; | ||
|
|
||
| @Override | ||
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
| Cookie c = new Cookie("uid", req.getSession().getId()); | ||
| // For older browsers? | ||
| c.setSecure(false); | ||
| resp.addCookie(c); | ||
| resp.setHeader("Access-Control-Allow-Origin", "*"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| Boolean b = Boolean.parseBoolean(req.getParameter("winCondition")); | ||
| int ticketNumber = Integer.parseInt(req.getParameter("ticket")); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try { | ||
| Statement s = conn.createStatement(); | ||
| s.execute("SELECT userName, isWin FROM users WHERE uid = " + ticketNumber + ";"); | ||
|
Comment on lines
+24
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ResultSet r = s.getResultSet(); | ||
|
Comment on lines
+26
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if (r.getBoolean("isWin") && b) { | ||
| resp.getWriter().write("You win, " + r.getString("userName")); | ||
| } else { | ||
| resp.getWriter().write("You lose, " + r.getString("userName")); | ||
|
Comment on lines
+28
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } catch (SQLException throwables) { | ||
| throwables.printStackTrace(); | ||
| } | ||
| resp.setStatus(200); | ||
|
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| @Override | ||
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
| super.doPost(req, resp); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| super.destroy(); | ||
|
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| @Override | ||
| public void init() throws ServletException { | ||
| super.init(); | ||
|
|
||
| try { | ||
| conn = DriverManager.getConnection(DB_URL, "user", ""); | ||
| } catch (SQLException throwables) { | ||
| throwables.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ccarries the session ID and explicitly disables theSecureflag. Any HTTP downgrade or misrouted plaintext request can leak credentials and allow takeover.Set
c.setSecure(true)andc.setHttpOnly(true), and addSameSiteviaSet-Cookieattributes to reduce cross-site abuse