Merge pull request #7338 from eugenp/remove-unused-module
remove unused module
This commit is contained in:
commit
6cae387a89
7
pom.xml
7
pom.xml
|
@ -544,7 +544,6 @@
|
|||
<!-- <module>raml</module> --> <!-- Not a maven project -->
|
||||
<module>ratpack</module>
|
||||
<module>reactor-core</module>
|
||||
<module>rest-with-spark-java</module>
|
||||
<module>resteasy</module>
|
||||
<module>restx</module>
|
||||
<!-- <module>rmi</module> --> <!-- Not a maven project -->
|
||||
|
@ -755,8 +754,6 @@
|
|||
|
||||
<module>spring-thymeleaf</module>
|
||||
|
||||
<module>spring-userservice</module>
|
||||
|
||||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
|
||||
|
@ -925,7 +922,6 @@
|
|||
<module>spring-state-machine</module>
|
||||
<module>spring-swagger-codegen/spring-swagger-codegen-app</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-userservice</module>
|
||||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
<module>spring-zuul/spring-zuul-foos-resource</module>
|
||||
|
@ -1225,7 +1221,6 @@
|
|||
<!-- <module>raml</module> --> <!-- Not a maven project -->
|
||||
<module>ratpack</module>
|
||||
<module>reactor-core</module>
|
||||
<module>rest-with-spark-java</module>
|
||||
<module>resteasy</module>
|
||||
<module>restx</module>
|
||||
<!-- <module>rmi</module> --> <!-- Not a maven project -->
|
||||
|
@ -1421,8 +1416,6 @@
|
|||
|
||||
<module>spring-thymeleaf</module>
|
||||
|
||||
<module>spring-userservice</module>
|
||||
|
||||
<module>spring-vault</module>
|
||||
<module>spring-vertx</module>
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
## Relevant articles:
|
|
@ -1,39 +0,0 @@
|
|||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>rest-with-spark-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>rest-with-spark-java</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.sparkjava</groupId>
|
||||
<artifactId>spark-core</artifactId>
|
||||
<version>${spark-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spark-core.version>2.5.4</spark-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import static spark.Spark.after;
|
||||
import static spark.Spark.before;
|
||||
import static spark.Spark.delete;
|
||||
import static spark.Spark.get;
|
||||
import static spark.Spark.post;
|
||||
import static spark.Spark.port;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.baeldung.service.LibraryService;
|
||||
|
||||
public class Router {
|
||||
|
||||
public static void main( String[] args ){
|
||||
|
||||
port(8080);
|
||||
|
||||
before((request, response) -> {
|
||||
|
||||
//do some filtering stuff
|
||||
|
||||
});
|
||||
|
||||
after((request, response) -> {
|
||||
response.type("application/json");
|
||||
});
|
||||
|
||||
get("ListOfBooks", (request, response) -> {
|
||||
return LibraryService.view();
|
||||
});
|
||||
|
||||
get("SearchBook/:title", (request, response) -> {
|
||||
return LibraryService.view(request.params("title"));
|
||||
});
|
||||
|
||||
post("AddBook/:title/:author/:publisher", (request, response) -> {
|
||||
Book book = new Book();
|
||||
book.setTitle(request.params("title"));
|
||||
book.setAuthor(request.params("author"));
|
||||
book.setPublisher(request.params("publisher"));
|
||||
return LibraryService.add(book);
|
||||
});
|
||||
|
||||
delete("DeleteBook/:title", (request, response) -> {
|
||||
return LibraryService.delete(request.params("title"));
|
||||
});
|
||||
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
package com.baeldung.domain;
|
||||
|
||||
public class Book {
|
||||
|
||||
private String title;
|
||||
private String author;
|
||||
private String publisher;
|
||||
|
||||
public Book() {}
|
||||
|
||||
public Book(String title, String author, String publisher) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
public String getPublisher() {
|
||||
return publisher;
|
||||
}
|
||||
public void setPublisher(String publisher) {
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
protected boolean canEqual(Object other) {
|
||||
return other instanceof Book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) return true;
|
||||
if (!(o instanceof Book)) return false;
|
||||
Book other = (Book) o;
|
||||
if (!other.canEqual((Object)this)) return false;
|
||||
if (this.getTitle() == null ? other.getTitle() != null : !this.getTitle().equals(other.getTitle())) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.ObjectWriter;
|
||||
|
||||
public class LibraryService {
|
||||
|
||||
private static ObjectWriter mapper = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
||||
private static Map<String, Book> library = new HashMap<String,Book>();
|
||||
|
||||
public static String view() throws JsonProcessingException {
|
||||
List<Book> books = new ArrayList<Book>();
|
||||
library.forEach((key, value) -> {
|
||||
books.add(value);
|
||||
});
|
||||
return mapper.writeValueAsString(books);
|
||||
}
|
||||
|
||||
public static String view(String title) throws JsonProcessingException {
|
||||
return mapper.writeValueAsString(library.get(title));
|
||||
}
|
||||
|
||||
public static String add(Book book) throws JsonProcessingException {
|
||||
library.put(book.getTitle(), book);
|
||||
return mapper.writeValueAsString(book);
|
||||
}
|
||||
|
||||
public static String delete(String title) throws JsonProcessingException {
|
||||
Book deletedBook = library.remove(title);
|
||||
return mapper.writeValueAsString(deletedBook);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,148 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.domain.Book;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
public class AppLiveTest extends TestCase {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public AppLiveTest( String testName ) {
|
||||
super( testName );
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite( AppLiveTest.class );
|
||||
}
|
||||
|
||||
public void testApp() throws IOException, ClassNotFoundException {
|
||||
|
||||
URL url;
|
||||
HttpURLConnection conn;
|
||||
BufferedReader br;
|
||||
String output;
|
||||
StringBuffer resp;
|
||||
Book book;
|
||||
Book temp;
|
||||
|
||||
url = new URL("http://localhost:8080/AddBook/Odessy/YannMartel/GreenLeaves");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
conn.getContent();
|
||||
br = new BufferedReader(new InputStreamReader(
|
||||
(conn.getInputStream())));
|
||||
resp = new StringBuffer();
|
||||
|
||||
while ((output = br.readLine()) != null) {
|
||||
resp.append(output);
|
||||
}
|
||||
|
||||
book = mapper.readValue(resp.toString(), Book.class);
|
||||
temp = new Book("Odessy","YannMartel","GreenLeaves");
|
||||
|
||||
assertEquals(book, temp);
|
||||
|
||||
url = new URL("http://localhost:8080/AddBook/Twilight/StephenieMeyer/LittleBrown");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("POST");
|
||||
br = new BufferedReader(new InputStreamReader(
|
||||
(conn.getInputStream())));
|
||||
resp = new StringBuffer();
|
||||
|
||||
while ((output = br.readLine()) != null) {
|
||||
resp.append(output);
|
||||
}
|
||||
|
||||
book = mapper.readValue(resp.toString(), Book.class);
|
||||
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
|
||||
|
||||
assertEquals(book, temp);
|
||||
|
||||
url = new URL("http://localhost:8080/ListOfBooks");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
br = new BufferedReader(new InputStreamReader(
|
||||
(conn.getInputStream())));
|
||||
resp = new StringBuffer();
|
||||
|
||||
while ((output = br.readLine()) != null) {
|
||||
resp.append(output);
|
||||
}
|
||||
|
||||
List<Book> books = new ArrayList<Book>();
|
||||
|
||||
books.add(new Book("Odessy","YannMartel","GreenLeaves"));
|
||||
books.add(new Book("Twilight","StephenieMeyer","LittleBrown"));
|
||||
|
||||
List<Book> listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){});
|
||||
|
||||
assertEquals(books, listOfBooks);
|
||||
|
||||
url = new URL("http://localhost:8080/SearchBook/Twilight");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
br = new BufferedReader(new InputStreamReader(
|
||||
(conn.getInputStream())));
|
||||
resp = new StringBuffer();
|
||||
|
||||
while ((output = br.readLine()) != null) {
|
||||
resp.append(output);
|
||||
}
|
||||
|
||||
book = mapper.readValue(resp.toString(), Book.class);
|
||||
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
|
||||
|
||||
assertEquals(book, temp);
|
||||
|
||||
url = new URL("http://localhost:8080/DeleteBook/Twilight");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("DELETE");
|
||||
br = new BufferedReader(new InputStreamReader(
|
||||
(conn.getInputStream())));
|
||||
resp = new StringBuffer();
|
||||
|
||||
while ((output = br.readLine()) != null) {
|
||||
resp.append(output);
|
||||
}
|
||||
|
||||
book = mapper.readValue(resp.toString(), Book.class);
|
||||
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
|
||||
|
||||
assertEquals(book, temp);
|
||||
|
||||
url = new URL("http://localhost:8080/ListOfBooks");
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
br = new BufferedReader(new InputStreamReader(
|
||||
(conn.getInputStream())));
|
||||
resp = new StringBuffer();
|
||||
|
||||
while ((output = br.readLine()) != null) {
|
||||
resp.append(output);
|
||||
}
|
||||
|
||||
books = new ArrayList<Book>();
|
||||
|
||||
books.add(new Book("Odessy","YannMartel","GreenLeaves"));
|
||||
listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){});
|
||||
|
||||
assertEquals(books, listOfBooks);
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
derby.log
|
|
@ -1,15 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beansProjectDescription>
|
||||
<version>1</version>
|
||||
<pluginVersion><![CDATA[3.7.3.201602250914-RELEASE]]></pluginVersion>
|
||||
<configSuffixes>
|
||||
<configSuffix><![CDATA[xml]]></configSuffix>
|
||||
</configSuffixes>
|
||||
<enableImports><![CDATA[false]]></enableImports>
|
||||
<configs>
|
||||
</configs>
|
||||
<autoconfigs>
|
||||
</autoconfigs>
|
||||
<configSets>
|
||||
</configSets>
|
||||
</beansProjectDescription>
|
|
@ -1 +0,0 @@
|
|||
spring-userservice is using a in memory derby db. Right click -> run on server to run the project
|
|
@ -1,240 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>spring-userservice</groupId>
|
||||
<artifactId>spring-userservice</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<name>spring-userservice</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-4</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-4</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-ehcache</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xml-apis</groupId>
|
||||
<artifactId>xml-apis</artifactId>
|
||||
<version>${xml-apis.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
<version>${javassist.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql-connector-java.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>${spring-data-jpa.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- validation -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>${javax.el-api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- utils -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-core</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derby</artifactId>
|
||||
<version>${derby.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derbyclient</artifactId>
|
||||
<version>${derby.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derbynet</artifactId>
|
||||
<version>${derby.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.derby</groupId>
|
||||
<artifactId>derbytools</artifactId>
|
||||
<version>${derby.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>taglibs</groupId>
|
||||
<artifactId>standard</artifactId>
|
||||
<version>${taglibs-standard.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-taglibs</artifactId>
|
||||
<version>${org.springframework.security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
||||
<artifactId>jstl-api</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-test</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-userservice</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.security.version>4.2.6.RELEASE</org.springframework.security.version>
|
||||
<spring-boot.version>1.5.14.RELEASE</spring-boot.version>
|
||||
<javassist.version>3.21.0-GA</javassist.version>
|
||||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
|
||||
<spring-data-jpa.version>1.10.5.RELEASE</spring-data-jpa.version>
|
||||
<derby.version>10.13.1.1</derby.version>
|
||||
|
||||
<!-- various -->
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<javax.el-api.version>2.2.5</javax.el-api.version>
|
||||
<taglibs-standard.version>1.1.2</taglibs-standard.version>
|
||||
|
||||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<xml-apis.version>1.4.01</xml-apis.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,42 +0,0 @@
|
|||
package org.baeldung.custom.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
|
||||
@EnableWebMvc
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "org.baeldung.security" })
|
||||
public class MvcConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
public MvcConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
super.addViewControllers(registry);
|
||||
|
||||
registry.addViewController("/");
|
||||
registry.addViewController("/index");
|
||||
registry.addViewController("/login");
|
||||
registry.addViewController("/register");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
|
||||
bean.setViewClass(JstlView.class);
|
||||
bean.setPrefix("/WEB-INF/view/");
|
||||
bean.setSuffix(".jsp");
|
||||
|
||||
return bean;
|
||||
}
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
package org.baeldung.custom.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.baeldung.user.dao.MyUserDAO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@PropertySource({ "classpath:persistence-derby.properties" })
|
||||
public class PersistenceDerbyJPAConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceDerbyJPAConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
// beans
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean myEmf() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
|
||||
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
em.setJpaVendorAdapter(vendorAdapter);
|
||||
em.setJpaProperties(additionalProperties());
|
||||
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(emf);
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MyUserDAO myUserDAO() {
|
||||
final MyUserDAO myUserDAO = new MyUserDAO();
|
||||
return myUserDAO;
|
||||
}
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
package org.baeldung.custom.config;
|
||||
|
||||
import org.baeldung.security.MyUserDetailsService;
|
||||
import org.baeldung.user.service.MyUserService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
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;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@Profile("!https")
|
||||
public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
public SecSecurityConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
||||
// @formatter:off
|
||||
auth.authenticationProvider(authenticationProvider());
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
// @formatter:off
|
||||
http
|
||||
.csrf().disable()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/*").permitAll()
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/login")
|
||||
.loginProcessingUrl("/login")
|
||||
.defaultSuccessUrl("/",true)
|
||||
.failureUrl("/login?error=true")
|
||||
.and()
|
||||
.logout()
|
||||
.logoutUrl("/logout")
|
||||
.deleteCookies("JSESSIONID")
|
||||
.logoutSuccessUrl("/");
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DaoAuthenticationProvider authenticationProvider() {
|
||||
final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
||||
authProvider.setUserDetailsService(myUserDetailsService());
|
||||
authProvider.setPasswordEncoder(encoder());
|
||||
return authProvider;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder encoder() {
|
||||
return new BCryptPasswordEncoder(11);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MyUserDetailsService myUserDetailsService() {
|
||||
return new MyUserDetailsService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MyUserService myUserService() {
|
||||
return new MyUserService();
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "spring_custom_user_service")
|
||||
public class MyUser {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Column(unique = true, nullable = false)
|
||||
private String username;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
public MyUser() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(final String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(final String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package org.baeldung.security;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.user.dao.MyUserDAO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service("userDetailsService")
|
||||
public class MyUserDetailsService implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
MyUserDAO myUserDAO;
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
|
||||
final MyUser user = myUserDAO.findByUsername(username);
|
||||
|
||||
if (user == null) {
|
||||
throw new UsernameNotFoundException("No user found with username: " + username);
|
||||
}
|
||||
return new User(user.getUsername(), user.getPassword(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package org.baeldung.security;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.baeldung.user.service.MyUserService;
|
||||
import org.baeldung.web.MyUserDto;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
MyUserService myUserService;
|
||||
|
||||
@RequestMapping(value = "/register", method = RequestMethod.POST)
|
||||
public String registerUserAccount(final MyUserDto accountDto, final Model model) {
|
||||
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
model.addAttribute("name", auth.getName());
|
||||
try {
|
||||
myUserService.registerNewUserAccount(accountDto);
|
||||
model.addAttribute("message", "Registration successful");
|
||||
return "index";
|
||||
} catch (final Exception exc) {
|
||||
model.addAttribute("message", "Registration failed");
|
||||
|
||||
return "index";
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||
public String getHomepage(final Model model) {
|
||||
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
model.addAttribute("name", auth.getName());
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/register", method = RequestMethod.GET)
|
||||
public String getRegister() {
|
||||
return "register";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
||||
public String getLogin() {
|
||||
return "login";
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package org.baeldung.user.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class MyUserDAO {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
public MyUser findByUsername(final String username) {
|
||||
final Query query = entityManager.createQuery("from MyUser where username=:username", MyUser.class);
|
||||
query.setParameter("username", username);
|
||||
final List<MyUser> result = query.getResultList();
|
||||
if (result != null && result.size() > 0) {
|
||||
return result.get(0);
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
public MyUser save(final MyUser user) {
|
||||
entityManager.persist(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
public void removeUserByUsername(String username) {
|
||||
final Query query = entityManager.createQuery("delete from MyUser where username=:username");
|
||||
query.setParameter("username", username);
|
||||
query.executeUpdate();
|
||||
}
|
||||
|
||||
public EntityManager getEntityManager() {
|
||||
return entityManager;
|
||||
}
|
||||
|
||||
public void setEntityManager(final EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package org.baeldung.user.service;
|
||||
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.user.dao.MyUserDAO;
|
||||
import org.baeldung.web.MyUserDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class MyUserService {
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@Autowired
|
||||
MyUserDAO myUserDAO;
|
||||
|
||||
public MyUser registerNewUserAccount(final MyUserDto accountDto) throws Exception {
|
||||
if (usernameExists(accountDto.getUsername())) {
|
||||
throw new Exception("There is an account with that username: " + accountDto.getUsername());
|
||||
}
|
||||
final MyUser user = new MyUser();
|
||||
|
||||
user.setUsername(accountDto.getUsername());
|
||||
user.setPassword(passwordEncoder.encode(accountDto.getPassword()));
|
||||
return myUserDAO.save(user);
|
||||
}
|
||||
|
||||
public MyUser getUserByUsername(final String username) {
|
||||
final MyUser user = myUserDAO.findByUsername(username);
|
||||
return user;
|
||||
}
|
||||
|
||||
public void removeUserByUsername(String username) {
|
||||
myUserDAO.removeUserByUsername(username);
|
||||
}
|
||||
|
||||
private boolean usernameExists(final String username) {
|
||||
final MyUser user = myUserDAO.findByUsername(username);
|
||||
if (user != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
package org.baeldung.web;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class MyUserDto {
|
||||
@NotNull
|
||||
@Size(min = 1)
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(final String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(final String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,12 +0,0 @@
|
|||
# jdbc.X
|
||||
jdbc.driverClassName=org.apache.derby.jdbc.EmbeddedDriver
|
||||
jdbc.url=jdbc:derby:memory:spring_custom_user_service;create=true
|
||||
jdbc.user=tutorialuser
|
||||
jdbc.pass=tutorialpass
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.DerbyDialect
|
||||
hibernate.show_sql=false
|
||||
hibernate.hbm2ddl.auto=update
|
||||
hibernate.cache.use_second_level_cache=false
|
||||
hibernate.cache.use_query_cache=false
|
|
@ -1,3 +0,0 @@
|
|||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
|
@ -1,88 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
|
||||
|
||||
<context:annotation-config></context:annotation-config>
|
||||
|
||||
<security:http auto-config="true">
|
||||
<security:intercept-url pattern="/" access="permitAll" />
|
||||
<security:intercept-url pattern="/login*" access="permitAll" />
|
||||
<security:intercept-url pattern="/logout*" access="permitAll" />
|
||||
<security:intercept-url pattern="/register*" access="permitAll" />
|
||||
|
||||
<security:csrf disabled="true"/>
|
||||
|
||||
<security:form-login login-page='/login' login-processing-url="/login" default-target-url="/" authentication-failure-url="/login?error=true"
|
||||
always-use-default-target="true"/>
|
||||
|
||||
<security:logout logout-url="/logout" delete-cookies="JSESSIONID" logout-success-url="/"/>
|
||||
</security:http>
|
||||
|
||||
<security:authentication-manager>
|
||||
<security:authentication-provider user-service-ref="myUserDetailsService" >
|
||||
<security:password-encoder ref="passwordEncoder"></security:password-encoder>
|
||||
</security:authentication-provider>
|
||||
</security:authentication-manager>
|
||||
|
||||
<bean id="myUserDetailsService" class="org.baeldung.security.MyUserDetailsService" />
|
||||
|
||||
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
|
||||
<constructor-arg value="11"/>
|
||||
</bean>
|
||||
|
||||
<context:component-scan base-package="org.baeldung.security"></context:component-scan>
|
||||
<context:component-scan base-package="org.baeldung.user"></context:component-scan>
|
||||
|
||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix">
|
||||
<value>/WEB-INF/views/</value>
|
||||
</property>
|
||||
<property name="suffix">
|
||||
<value>.jsp</value>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<context:property-placeholder location="classpath:persistence-derby.properties"/>
|
||||
|
||||
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
||||
<property name="dataSource" ref="dataSource"/>
|
||||
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
|
||||
<property name="jpaVendorAdapter">
|
||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
|
||||
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> <property name="generateDdl" value="${jpa.generateDdl}" /> <property name="databasePlatform"
|
||||
value="${persistence.dialect}" /> </bean> -->
|
||||
</property>
|
||||
<property name="jpaProperties">
|
||||
<props>
|
||||
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
|
||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
||||
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
|
||||
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.user}"/>
|
||||
<property name="password" value="${jdbc.pass}"/>
|
||||
</bean>
|
||||
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
<property name="entityManagerFactory" ref="myEmf"/>
|
||||
</bean>
|
||||
<tx:annotation-driven/>
|
||||
|
||||
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
</beans>
|
|
@ -1,35 +0,0 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="c"
|
||||
uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Welcome!</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<c:url value="/register" var="registerUrl" />
|
||||
<c:url value="/login" var="loginUrl"/>
|
||||
<c:url value="/logout" var="logoutUrl"/>
|
||||
|
||||
<a href="${registerUrl }">Register</a>
|
||||
<br /><br/>
|
||||
<sec:authorize access="!isAuthenticated()">
|
||||
<a href="${loginUrl }">Login</a>
|
||||
</sec:authorize>
|
||||
<br><br>
|
||||
${message }
|
||||
<br><br>
|
||||
<sec:authorize access="isAuthenticated()">
|
||||
Hello, ${name }!
|
||||
<br>
|
||||
<br>
|
||||
<a href="${logoutUrl }">Logout</a>
|
||||
</sec:authorize>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,29 +0,0 @@
|
|||
<%@ taglib prefix="c"
|
||||
uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
|
||||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<h1>Login</h1>
|
||||
|
||||
<form name='f' action="login" method='POST'>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>User:</td>
|
||||
<td><input type='text' name='username' value=''></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Password:</td>
|
||||
<td><input type='password' name='password' /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input name="submit" type="submit" value="submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
</form>
|
||||
<c:if test="${param.error }">Username or password invalid! </c:if>
|
||||
</body>
|
||||
</html>
|
|
@ -1,23 +0,0 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="c"
|
||||
uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Welcome!</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<c:url value="/register" var="registerUrl" />
|
||||
|
||||
Register here:<br><br>
|
||||
<form action="${registerUrl }" method="POST">
|
||||
Username: <input type="text" name="username"/><br>
|
||||
Password: <input type="password" name="password"/><br>
|
||||
<input type="submit" value="Register"/>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,51 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
<display-name>Spring MVC Application</display-name>
|
||||
|
||||
|
||||
<!--
|
||||
<context-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>
|
||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||
</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>org.baeldung.custom.config</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
-->
|
||||
<!-- Spring child -->
|
||||
<servlet>
|
||||
<servlet-name>mvc-dispatcher</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>mvc-dispatcher</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<!-- Spring Security -->
|
||||
<filter>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>springSecurityFilterChain</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
|
@ -1,18 +0,0 @@
|
|||
package org.baeldung;
|
||||
|
||||
import org.baeldung.custom.config.MvcConfig;
|
||||
import org.baeldung.custom.config.PersistenceDerbyJPAConfig;
|
||||
import org.baeldung.custom.config.SecSecurityConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class })
|
||||
public class SpringContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
package org.baeldung.userservice;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.baeldung.custom.config.MvcConfig;
|
||||
import org.baeldung.custom.config.PersistenceDerbyJPAConfig;
|
||||
import org.baeldung.custom.config.SecSecurityConfig;
|
||||
import org.baeldung.user.service.MyUserService;
|
||||
import org.baeldung.web.MyUserDto;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class })
|
||||
@WebAppConfiguration
|
||||
public class CustomUserDetailsServiceIntegrationTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger("CustomUserDetailsServiceTest");
|
||||
|
||||
public static final String USERNAME = "user";
|
||||
public static final String PASSWORD = "pass";
|
||||
public static final String USERNAME2 = "user2";
|
||||
|
||||
@Autowired
|
||||
MyUserService myUserService;
|
||||
|
||||
@Autowired
|
||||
AuthenticationProvider authenticationProvider;
|
||||
|
||||
@Test
|
||||
public void givenExistingUser_whenAuthenticate_thenRetrieveFromDb() {
|
||||
try {
|
||||
MyUserDto userDTO = new MyUserDto();
|
||||
userDTO.setUsername(USERNAME);
|
||||
userDTO.setPassword(PASSWORD);
|
||||
|
||||
myUserService.registerNewUserAccount(userDTO);
|
||||
|
||||
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(USERNAME, PASSWORD);
|
||||
Authentication authentication = authenticationProvider.authenticate(auth);
|
||||
|
||||
assertEquals(authentication.getName(), USERNAME);
|
||||
|
||||
} catch (Exception exc) {
|
||||
LOG.log(Level.SEVERE, "Error creating account");
|
||||
} finally {
|
||||
myUserService.removeUserByUsername(USERNAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = BadCredentialsException.class)
|
||||
public void givenIncorrectUser_whenAuthenticate_thenBadCredentialsException() {
|
||||
try {
|
||||
MyUserDto userDTO = new MyUserDto();
|
||||
userDTO.setUsername(USERNAME);
|
||||
userDTO.setPassword(PASSWORD);
|
||||
|
||||
try {
|
||||
myUserService.registerNewUserAccount(userDTO);
|
||||
} catch (Exception exc) {
|
||||
LOG.log(Level.SEVERE, "Error creating account");
|
||||
}
|
||||
|
||||
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(USERNAME2, PASSWORD);
|
||||
Authentication authentication = authenticationProvider.authenticate(auth);
|
||||
} finally {
|
||||
myUserService.removeUserByUsername(USERNAME);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue