Browse Source

Merge pull request #6 from Rudi9719/master

Setup Security on /card and /
pull/8/head
jordanmason-stleo 4 years ago committed by GitHub
parent
commit
6b98b4dce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      pom.xml
  2. 22
      src/main/java/MTGClone/WebSecurityConfig.java
  3. 23
      src/main/java/MTGClone/controller/CardController.java
  4. 15
      src/main/webapp/WEB-INF/jsp/card.jsp

6
pom.xml

@ -32,7 +32,11 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- jstl --> <!-- jstl -->
<dependency> <dependency>
<groupId>jstl</groupId> <groupId>jstl</groupId>

22
src/main/java/MTGClone/WebSecurityConfig.java

@ -0,0 +1,22 @@
package MTGClone;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("Called configure(HttpSecurity http);");
http.authorizeRequests().antMatchers("/").permitAll().anyRequest().authenticated().and().formLogin().and()
.csrf().disable().logout();
}
// TODO: Login using SQLDriver.authenticateUser(username, password)
}

23
src/main/java/MTGClone/controller/CardController.java

@ -1,5 +1,6 @@
package MTGClone.controller; package MTGClone.controller;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
@ -14,24 +15,22 @@ import java.util.ArrayList;
@Controller @Controller
public class CardController { public class CardController {
@GetMapping({"/card", "/"}) @GetMapping({"/card", "/"})
public String hello(Model model) { public String showCards(Model model) {
ArrayList<Card> allCards = (new SQLDriver()).getAllCards(); ArrayList<Card> allCards = (new SQLDriver()).getAllCards();
model.addAttribute("allcards", allCards); model.addAttribute("allcards", allCards);
return "card"; return "card";
} }
@PostMapping({"/card", "/"})
public String newCard(@RequestParam("cardname") String cardname, @RequestParam("manacost") int manacost,
@PostMapping({"/card"}) @RequestParam("power") int power, @RequestParam("toughness") int toughness, @RequestParam("description") String description,
public String greet(@RequestParam("cardname") String cardname, @RequestParam("manacost") int manacost, @RequestParam("creaturetype") String creaturetype, ModelMap modelMap) {
@RequestParam("power") int power, @RequestParam("toughness") int toughness, @RequestParam("description") String description, @RequestParam("creaturetype") String creaturetype,
@RequestParam("username") String username, @RequestParam("password") String password, ModelMap modelMap) {
SQLDriver d = new SQLDriver(); SQLDriver d = new SQLDriver();
if (d.authenticateUser(username, password)) { System.out.println(creaturetype);
Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype); System.out.println("Posting to /card detected");
d.insertCard(newCard); Card newCard = new Card(cardname, manacost, power, toughness, description, "", creaturetype);
} d.insertCard(newCard);
//modelMap.addAttribute("allcards", d.getAllCards());
return "card"; return "card";
} }

15
src/main/webapp/WEB-INF/jsp/card.jsp

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html lang="en"> <html lang="en" xmlns:th="http://www.thymeleaf.org">
<head> <head>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Ubuntu&display=swap" rel="stylesheet">
<style> <style>
@ -25,7 +25,7 @@ table, td, th{
.container label { .container label {
position: relative;font-size:20px; position: relative;font-size:20px;
} }
.container input, textarea{ .container input, textarea, select{
background-color:#CFCACA;position:relative;border-radius:5px;font-size:20px; background-color:#CFCACA;position:relative;border-radius:5px;font-size:20px;
} }
</style> </style>
@ -38,7 +38,7 @@ table, td, th{
<div class="container"> <div class="container">
<form action="/card" method="post"> <form action="#" method="post" th:action="@{/card}" >
<br><br><label style="left:210px;font-size:26px;"for="cardname">Card Name:</label> <br><br><label style="left:210px;font-size:26px;"for="cardname">Card Name:</label>
<input style="left:210px;font-size:24px;width:300px;"placeholder="Card Title"type="text" id="cardname" name="cardname"><br><br> <input style="left:210px;font-size:24px;width:300px;"placeholder="Card Title"type="text" id="cardname" name="cardname"><br><br>
<label style="left:150px;"for="manacost">Mana Cost:</label> <label style="left:150px;"for="manacost">Mana Cost:</label>
@ -48,7 +48,7 @@ table, td, th{
<label style="left:350px;"for="toughness">Toughness:</label> <label style="left:350px;"for="toughness">Toughness:</label>
<input style="left:350px;"type="text" id="toughness"name="toughness"> <input style="left:350px;"type="text" id="toughness"name="toughness">
<label style="left:450px;"for="creaturetype">Spell Type:</label> <label style="left:450px;"for="creaturetype">Spell Type:</label>
<select style="background-color:#CFCACA;position:relative;border-radius:4px;left:450px;font-size:20px;width:250px;"id="creaturetype"name="creaturetype"> <select style="left:450px;width:250px;"id="creaturetype"name="creaturetype">
<option value="Evocation">Evocation</option> <option value="Evocation">Evocation</option>
<option value="Ward">Ward</option> <option value="Ward">Ward</option>
<option value="Curse">Curse</option> <option value="Curse">Curse</option>
@ -56,12 +56,7 @@ table, td, th{
<br><br> <br><br>
<label style="vertical-align:middle;left:145px;"for="description">Description:</label> <label style="vertical-align:middle;left:145px;"for="description">Description:</label>
<textarea style="border-radius:5px;vertical-align:middle;left:145px;font-size:18px;width:658px;height:120px;"placeholder="Flavor text, etc."type="text" id="description" name="description"></textarea> <textarea style="border-radius:5px;vertical-align:middle;left:145px;font-size:18px;width:658px;height:120px;"placeholder="Flavor text, etc."type="text" id="description" name="description"></textarea>
<br><input type="hidden" name="_csrf" th:value="${_csrf.token}"/>
<label style="left:245px;font-size:14px;"for="username">Username:</label>
<input style="left:245px;font-size:14px;"type="text" id="username" name="username">
<label style="left:300px;font-size:14px;"for="password">Password:</label>
<input style="left:300px;font-size:14px;"type="password" id="password" name="password"><br>
<br> <input style="height:40px;width:15%;display:block;margin:0 auto;" type="submit" value="Submit"><br><br> <br> <input style="height:40px;width:15%;display:block;margin:0 auto;" type="submit" value="Submit"><br><br>
</form> </form>
</div> </div>

Loading…
Cancel
Save