├── bin ├── messages.properties ├── info │ └── ewai │ │ └── sbmt │ │ ├── domain │ │ ├── Book.class │ │ ├── User.class │ │ ├── Authorities.class │ │ ├── AuthoritiesId.class │ │ ├── BookRepository.class │ │ ├── UserRepository.class │ │ └── BookRepositoryTests.class │ │ ├── service │ │ ├── BookService.class │ │ ├── UserService.class │ │ └── BookServiceTests.class │ │ ├── web │ │ ├── BookController.class │ │ ├── BookValidator.class │ │ ├── form │ │ │ └── BookForm.class │ │ ├── SimpleController.class │ │ ├── BookControllerTests.class │ │ └── SimpleControllerTests.class │ │ ├── config │ │ ├── SecurityConfig.class │ │ ├── SecurityConfigTests.class │ │ └── SecurityConfig$AuthenticationConfiguration.class │ │ └── SpringBootTemplateApplication.class ├── application.properties ├── messages_ja.properties ├── static │ ├── css │ │ └── common.css │ └── img │ │ └── mark-github.svg └── templates │ ├── book-complete.html │ ├── index.html │ ├── common.html │ ├── login.html │ ├── book.html │ └── book-edit.html ├── src ├── main │ ├── resources │ │ ├── messages.properties │ │ ├── application.properties │ │ ├── messages_ja.properties │ │ ├── static │ │ │ ├── css │ │ │ │ └── common.css │ │ │ └── img │ │ │ │ └── mark-github.svg │ │ └── templates │ │ │ ├── book-complete.html │ │ │ ├── index.html │ │ │ ├── common.html │ │ │ ├── login.html │ │ │ ├── book.html │ │ │ └── book-edit.html │ └── java │ │ └── info │ │ └── ewai │ │ └── sbmt │ │ ├── domain │ │ ├── UserRepository.java │ │ ├── BookRepository.java │ │ ├── AuthoritiesId.java │ │ ├── Authorities.java │ │ ├── Book.java │ │ └── User.java │ │ ├── SpringBootTemplateApplication.java │ │ ├── web │ │ ├── SimpleController.java │ │ ├── BookValidator.java │ │ ├── form │ │ │ └── BookForm.java │ │ └── BookController.java │ │ ├── service │ │ ├── UserService.java │ │ └── BookService.java │ │ └── config │ │ └── SecurityConfig.java └── test │ └── java │ └── info │ └── ewai │ └── sbmt │ ├── domain │ └── BookRepositoryTests.java │ ├── service │ └── BookServiceTests.java │ ├── web │ ├── BookControllerTests.java │ └── SimpleControllerTests.java │ └── config │ └── SecurityConfigTests.java ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md ├── gradlew.bat └── gradlew /bin/messages.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/Book.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/Book.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/User.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/Authorities.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/Authorities.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/service/BookService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/service/BookService.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/service/UserService.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/service/UserService.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/web/BookController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/web/BookController.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/web/BookValidator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/web/BookValidator.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/web/form/BookForm.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/web/form/BookForm.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/config/SecurityConfig.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/config/SecurityConfig.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/AuthoritiesId.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/AuthoritiesId.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/BookRepository.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/BookRepository.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/UserRepository.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/UserRepository.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/web/SimpleController.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/web/SimpleController.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/service/BookServiceTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/service/BookServiceTests.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/web/BookControllerTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/web/BookControllerTests.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/config/SecurityConfigTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/config/SecurityConfigTests.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/domain/BookRepositoryTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/domain/BookRepositoryTests.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/web/SimpleControllerTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/web/SimpleControllerTests.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/SpringBootTemplateApplication.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/SpringBootTemplateApplication.class -------------------------------------------------------------------------------- /bin/info/ewai/sbmt/config/SecurityConfig$AuthenticationConfiguration.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewai/spring-boot-mvc-template/HEAD/bin/info/ewai/sbmt/config/SecurityConfig$AuthenticationConfiguration.class -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-bin.zip 6 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/domain/UserRepository.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | public interface UserRepository extends JpaRepository { 6 | public User findByUsername(String username); 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/domain/BookRepository.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | 7 | public interface BookRepository extends JpaRepository { 8 | 9 | public List findByBookNameLikeAndTagLike(String bookName, String tag); 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | nbproject/private/ 21 | build/ 22 | nbbuild/ 23 | dist/ 24 | nbdist/ 25 | .nb-gradle/ -------------------------------------------------------------------------------- /bin/application.properties: -------------------------------------------------------------------------------- 1 | # db 2 | spring.datasource.url=jdbc:mysql://localhost/sbtdb 3 | spring.datasource.username=sbt 4 | spring.datasource.password=sbt 5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 | 7 | # thymleaf 8 | spring.thymeleaf.cache=false 9 | 10 | # log 11 | logging.level.org.springframework.web=info 12 | logging.level.org.hibernate=info 13 | 14 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # db 2 | spring.datasource.url=jdbc:mysql://localhost/sbtdb 3 | spring.datasource.username=sbt 4 | spring.datasource.password=sbt 5 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 6 | 7 | # thymleaf 8 | spring.thymeleaf.cache=false 9 | 10 | # log 11 | logging.level.org.springframework.web=info 12 | logging.level.org.hibernate=info 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/SpringBootTemplateApplication.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootTemplateApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootTemplateApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-template 2 | 3 | ## MVC 4 | * controller 5 | * form 6 | * validation 7 | * service 8 | * repository 9 | 10 | ## Architecture 11 | * thymeleaf 12 | * mysql 13 | * java 14 | * jpa 15 | 16 | ## how to 17 | 18 | db construction 19 | 20 | https://github.com/ewai/docker-spring-boot-template-mysql 21 | 22 | Qiita 23 | 24 | http://qiita.com/ewai/items/f375d562744502d79790 25 | 26 | Demo 27 | 28 | https://ewai.info/sbt/ 29 | 30 | -------------------------------------------------------------------------------- /bin/messages_ja.properties: -------------------------------------------------------------------------------- 1 | field.required=入力してください 2 | input.error=入力エラーが発生しました 3 | exception.error=例外エラーが発生しました 4 | not.found={0}対象のデータが見つかりませんでした 5 | 6 | # 認証 7 | AbstractUserDetailsAuthenticationProvider.locked=ロックされています 8 | AbstractUserDetailsAuthenticationProvider.disabled=利用できません 9 | AbstractUserDetailsAuthenticationProvider.expired=IDの有効期限がきれています 10 | AbstractUserDetailsAuthenticationProvider.credentialsExpired=パスワードの有効期限がきれています 11 | AbstractUserDetailsAuthenticationProvider.badCredentials=ID またはパスワードが正しくありません 12 | -------------------------------------------------------------------------------- /src/main/resources/messages_ja.properties: -------------------------------------------------------------------------------- 1 | field.required=入力してください 2 | input.error=入力エラーが発生しました 3 | exception.error=例外エラーが発生しました 4 | not.found={0}対象のデータが見つかりませんでした 5 | 6 | # 認証 7 | AbstractUserDetailsAuthenticationProvider.locked=ロックされています 8 | AbstractUserDetailsAuthenticationProvider.disabled=利用できません 9 | AbstractUserDetailsAuthenticationProvider.expired=IDの有効期限がきれています 10 | AbstractUserDetailsAuthenticationProvider.credentialsExpired=パスワードの有効期限がきれています 11 | AbstractUserDetailsAuthenticationProvider.badCredentials=ID またはパスワードが正しくありません 12 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/web/SimpleController.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.web; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RequestMethod; 6 | 7 | @Controller 8 | public class SimpleController { 9 | 10 | @RequestMapping(value = "/", method = RequestMethod.GET) 11 | public String index() { 12 | return "index"; 13 | } 14 | 15 | @RequestMapping(value = "/login", method = RequestMethod.GET) 16 | public String login() { 17 | return "login"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /bin/static/css/common.css: -------------------------------------------------------------------------------- 1 | 2 | a { 3 | text-decoration:none; 4 | } 5 | 6 | a:link { 7 | text-decoration:none; 8 | } 9 | 10 | a:visited { 11 | text-decoration:none; 12 | } 13 | a:hover { 14 | text-decoration:none; 15 | } 16 | 17 | .error-message { 18 | color:red; 19 | font-weight:bold; 20 | } 21 | 22 | #header { 23 | background-color:powderblue; 24 | margin-bottom:30px; 25 | } 26 | 27 | #footer { 28 | background-color:powderblue; 29 | margin-top:30px; 30 | padding:20px 5px; 31 | } 32 | 33 | #footer li { 34 | display:table-cell; 35 | padding-left:30px; 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/resources/static/css/common.css: -------------------------------------------------------------------------------- 1 | 2 | a { 3 | text-decoration:none; 4 | } 5 | 6 | a:link { 7 | text-decoration:none; 8 | } 9 | 10 | a:visited { 11 | text-decoration:none; 12 | } 13 | a:hover { 14 | text-decoration:none; 15 | } 16 | 17 | .error-message { 18 | color:red; 19 | font-weight:bold; 20 | } 21 | 22 | #header { 23 | background-color:powderblue; 24 | margin-bottom:30px; 25 | } 26 | 27 | #footer { 28 | background-color:powderblue; 29 | margin-top:30px; 30 | padding:20px 5px; 31 | } 32 | 33 | #footer li { 34 | display:table-cell; 35 | padding-left:30px; 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/domain/AuthoritiesId.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Embeddable; 6 | 7 | @Embeddable 8 | public class AuthoritiesId implements Serializable { 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | private int user_id; 13 | private String authority; 14 | 15 | public AuthoritiesId() { 16 | } 17 | 18 | public AuthoritiesId(int user_id, String authority) { 19 | super(); 20 | this.user_id = user_id; 21 | this.authority = authority; 22 | } 23 | 24 | public int getUser_id() { 25 | return user_id; 26 | } 27 | 28 | public void setUser_id(int user_id) { 29 | this.user_id = user_id; 30 | } 31 | 32 | public String getAuthority() { 33 | return authority; 34 | } 35 | 36 | public void setAuthority(String authority) { 37 | this.authority = authority; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/domain/Authorities.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.FetchType; 5 | import javax.persistence.Id; 6 | import javax.persistence.JoinColumn; 7 | import javax.persistence.ManyToOne; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "authorities") 12 | public class Authorities { 13 | 14 | @Id 15 | private AuthoritiesId id; 16 | 17 | @ManyToOne(fetch = FetchType.LAZY) 18 | @JoinColumn(name = "user_id", insertable = false, updatable = false) 19 | private User user; 20 | 21 | public Authorities() { 22 | } 23 | 24 | public Authorities(AuthoritiesId id) { 25 | this.id = id; 26 | } 27 | 28 | public AuthoritiesId getId() { 29 | return id; 30 | } 31 | 32 | public void setId(AuthoritiesId id) { 33 | this.id = id; 34 | } 35 | 36 | public User getUser() { 37 | return user; 38 | } 39 | 40 | public void setUser(User user) { 41 | this.user = user; 42 | } 43 | } -------------------------------------------------------------------------------- /bin/templates/book-complete.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | Book 8 | 9 | 10 | 11 | 12 |
13 |
14 | 15 |
16 | 17 |
18 |
19 |
20 | 21 |
22 |
23 | 戻る 24 |
25 |
26 |
27 |
28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /bin/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Top page 7 | 8 | 9 | 10 | 11 |
12 |

Hello Book !!

13 |
14 | 19 |
20 |
21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/resources/templates/book-complete.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | Book 8 | 9 | 10 | 11 | 12 |
13 |
14 | 15 |
16 | 17 |
18 |
19 |
20 | 21 |
22 |
23 | 戻る 24 |
25 |
26 |
27 |
28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/resources/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Top page 7 | 8 | 9 | 10 | 11 |
12 |

Hello Book !!

13 |
14 | 19 |
20 |
21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/service/UserService.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.security.core.userdetails.UserDetails; 5 | import org.springframework.security.core.userdetails.UserDetailsService; 6 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 7 | import org.springframework.stereotype.Component; 8 | import org.springframework.util.StringUtils; 9 | 10 | import info.ewai.sbmt.domain.User; 11 | import info.ewai.sbmt.domain.UserRepository; 12 | 13 | @Component 14 | public class UserService implements UserDetailsService { 15 | 16 | @Autowired 17 | private UserRepository userRepository; 18 | 19 | @Override 20 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 21 | if (StringUtils.isEmpty(username)) { 22 | throw new UsernameNotFoundException("Username is empty"); 23 | } 24 | User user = userRepository.findByUsername(username); 25 | if (user == null) { 26 | throw new UsernameNotFoundException("User not found for name: " + username); 27 | } 28 | return user; 29 | } 30 | } -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/web/BookValidator.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.web; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.validation.Errors; 6 | import org.springframework.validation.ValidationUtils; 7 | import org.springframework.validation.Validator; 8 | 9 | import info.ewai.sbmt.service.BookService; 10 | import info.ewai.sbmt.web.form.BookForm; 11 | 12 | @Component 13 | public class BookValidator implements Validator { 14 | 15 | @Autowired 16 | BookService bookService; 17 | 18 | @Override 19 | public boolean supports(Class clazz) { 20 | return BookForm.class.isAssignableFrom(clazz); 21 | } 22 | 23 | @Override 24 | public void validate(Object target, Errors errors) { 25 | 26 | // required check 27 | ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bookName", "field.required"); 28 | 29 | // TODO form check 30 | // BookForm form = BookForm.class.cast(target); 31 | // errors.rejectValue("field", "errorCode"); 32 | 33 | // global message 34 | if (errors.hasErrors()) { 35 | errors.reject("input.error"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /bin/static/img/mark-github.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mark-github 5 | Created with Sketch. 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/resources/static/img/mark-github.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mark-github 5 | Created with Sketch. 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/service/BookService.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | import org.springframework.util.StringUtils; 9 | 10 | import info.ewai.sbmt.domain.Book; 11 | import info.ewai.sbmt.domain.BookRepository; 12 | 13 | @Service 14 | public class BookService { 15 | 16 | @Autowired 17 | private BookRepository bookRepository; 18 | 19 | public List findAll() { 20 | return this.bookRepository.findAll(); 21 | } 22 | 23 | public List findByBookNameLikeAndTagLike(String bookName, String tag) { 24 | if (StringUtils.isEmpty(bookName) && (StringUtils.isEmpty(tag))) { 25 | return this.findAll(); 26 | } 27 | return this.bookRepository.findByBookNameLikeAndTagLike("%" + bookName + "%", "%" + tag + "%"); 28 | } 29 | 30 | public Book findOne(Long bookId) { 31 | return this.bookRepository.findOne(bookId.longValue()); 32 | } 33 | 34 | @Transactional 35 | public Book save(Book book) { 36 | return this.bookRepository.save(book); 37 | } 38 | 39 | @Transactional 40 | public void delete(Book book) { 41 | this.bookRepository.delete(book); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/test/java/info/ewai/sbmt/domain/BookRepositoryTests.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; 8 | import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; 9 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | import static org.assertj.core.api.Assertions.*; 13 | 14 | import java.util.List; 15 | 16 | @RunWith(SpringRunner.class) 17 | @DataJpaTest 18 | @AutoConfigureTestDatabase(replace=Replace.NONE) 19 | public class BookRepositoryTests { 20 | 21 | @Autowired 22 | private BookRepository repository; 23 | 24 | @Test 25 | public void test_findByBookNameLikeAndTagLike_findName() throws Exception { 26 | List list = this.repository.findByBookNameLikeAndTagLike("%Spring%", "%%"); 27 | assertThat(3).isEqualTo(list.size()); 28 | } 29 | 30 | @Test 31 | public void test_findByBookNameLikeAndTagLike_findTag() throws Exception { 32 | List list = this.repository.findByBookNameLikeAndTagLike("%%", "%Docker%"); 33 | assertThat(1).isEqualTo(list.size()); 34 | } 35 | 36 | @Test 37 | public void test_findByBookNameLikeAndTagLike() throws Exception { 38 | List list = this.repository.findByBookNameLikeAndTagLike("%はじめて%", "%Spring%"); 39 | assertThat(1).isEqualTo(list.size()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/web/form/BookForm.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.web.form; 2 | 3 | import java.io.Serializable; 4 | 5 | import info.ewai.sbmt.domain.Book; 6 | 7 | public class BookForm implements Serializable { 8 | 9 | private static final long serialVersionUID = 1L; 10 | 11 | public BookForm() { 12 | } 13 | 14 | public BookForm(Book book) { 15 | this.setBookId(book.getBookId()); 16 | this.setBookName(book.getBookName()); 17 | this.setDescription(book.getDescription()); 18 | this.setLink(book.getLink()); 19 | this.setImg(book.getImg()); 20 | this.setTag(book.getTag()); 21 | } 22 | 23 | private Long bookId; 24 | private String bookName; 25 | private String link; 26 | private String img; 27 | private String description; 28 | private String tag; 29 | 30 | public Long getBookId() { 31 | return bookId; 32 | } 33 | 34 | public void setBookId(Long bookId) { 35 | this.bookId = bookId; 36 | } 37 | 38 | public String getBookName() { 39 | return bookName; 40 | } 41 | 42 | public void setBookName(String bookName) { 43 | this.bookName = bookName; 44 | } 45 | 46 | public String getImg() { 47 | return img; 48 | } 49 | 50 | public void setImg(String img) { 51 | this.img = img; 52 | } 53 | 54 | public String getLink() { 55 | return link; 56 | } 57 | 58 | public void setLink(String link) { 59 | this.link = link; 60 | } 61 | 62 | public String getDescription() { 63 | return description; 64 | } 65 | 66 | public void setDescription(String description) { 67 | this.description = description; 68 | } 69 | 70 | public String getTag() { 71 | return tag; 72 | } 73 | 74 | public void setTag(String tag) { 75 | this.tag = tag; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /bin/templates/common.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 10 | 12 | 14 | 16 | 17 | 18 | 26 | 27 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/resources/templates/common.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 10 | 12 | 14 | 16 | 17 | 18 | 26 | 27 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /bin/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | ログイン 7 | 8 | 9 | 10 | 11 |
12 |
13 |

ログイン

14 |
15 | 18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 26 | 27 |
28 |
29 | 30 | 31 |
32 |
33 | 34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/main/resources/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | ログイン 7 | 8 | 9 | 10 | 11 |
12 |
13 |

ログイン

14 |
15 | 18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | 26 | 27 |
28 |
29 | 30 | 31 |
32 |
33 | 34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/domain/Book.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.GenerationType; 8 | import javax.persistence.Id; 9 | import javax.persistence.Table; 10 | 11 | import info.ewai.sbmt.web.form.BookForm; 12 | 13 | @Entity 14 | @Table(name = "book") 15 | public class Book implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | public Book() { 20 | } 21 | 22 | public Book(BookForm book) { 23 | this.setBookId(book.getBookId()); 24 | this.setBookName(book.getBookName()); 25 | this.setDescription(book.getDescription()); 26 | this.setLink(book.getLink()); 27 | this.setImg(book.getImg()); 28 | this.setTag(book.getTag()); 29 | } 30 | 31 | @Id 32 | @GeneratedValue(strategy=GenerationType.AUTO) 33 | private Long bookId; 34 | private String bookName; 35 | private String link; 36 | private String img; 37 | private String description; 38 | private String tag; 39 | 40 | public Long getBookId() { 41 | return bookId; 42 | } 43 | 44 | public void setBookId(Long bookId) { 45 | this.bookId = bookId; 46 | } 47 | 48 | public String getBookName() { 49 | return bookName; 50 | } 51 | 52 | public void setBookName(String bookName) { 53 | this.bookName = bookName; 54 | } 55 | 56 | public String getImg() { 57 | return img; 58 | } 59 | 60 | public void setImg(String img) { 61 | this.img = img; 62 | } 63 | 64 | public String getLink() { 65 | return link; 66 | } 67 | 68 | public void setLink(String link) { 69 | this.link = link; 70 | } 71 | 72 | public String getDescription() { 73 | return description; 74 | } 75 | 76 | public void setDescription(String description) { 77 | this.description = description; 78 | } 79 | 80 | public String getTag() { 81 | return tag; 82 | } 83 | 84 | public void setTag(String tag) { 85 | this.tag = tag; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 6 | import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter; 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 | import org.springframework.security.config.annotation.web.builders.WebSecurity; 9 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 10 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 11 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 12 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 13 | 14 | import info.ewai.sbmt.service.UserService; 15 | 16 | @Configuration 17 | @EnableWebSecurity 18 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 19 | 20 | @Override 21 | public void configure(WebSecurity web) throws Exception { 22 | web.ignoring().antMatchers("/img/**", "/css/**", "/js/**", "/webjars/**"); 23 | } 24 | 25 | @Override 26 | protected void configure(HttpSecurity http) throws Exception { 27 | http.authorizeRequests() 28 | .antMatchers("/").permitAll() 29 | .anyRequest().authenticated() 30 | .and() 31 | .formLogin() 32 | .loginPage("/login") 33 | .usernameParameter("username") 34 | .passwordParameter("password").permitAll().and() 35 | .logout() 36 | .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 37 | .logoutSuccessUrl("/") 38 | .deleteCookies("JSESSIONID") 39 | .invalidateHttpSession(true).permitAll(); 40 | } 41 | 42 | @Configuration 43 | protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { 44 | @Autowired 45 | UserService userService; 46 | 47 | @Override 48 | public void init(AuthenticationManagerBuilder auth) throws Exception { 49 | auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder()); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/test/java/info/ewai/sbmt/service/BookServiceTests.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.service; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.junit.Assert.*; 5 | 6 | import java.util.List; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.test.context.SpringBootTest; 12 | import org.springframework.test.annotation.Rollback; 13 | import org.springframework.test.context.junit4.SpringRunner; 14 | 15 | import info.ewai.sbmt.domain.Book; 16 | 17 | @RunWith(SpringRunner.class) 18 | @SpringBootTest 19 | public class BookServiceTests { 20 | 21 | @Autowired 22 | BookService bookService; 23 | 24 | @Test 25 | public void testFindAll() throws Exception { 26 | List resultList = this.bookService.findAll(); 27 | assertEquals(5, resultList.size()); 28 | } 29 | 30 | @Test 31 | public void testFindByBookNameLikeAndTagLike_findAll() throws Exception { 32 | List resultList = this.bookService.findByBookNameLikeAndTagLike("", ""); 33 | assertEquals(5, resultList.size()); 34 | } 35 | 36 | @Test 37 | public void test_findByBookNameLikeAndTagLike_findName() throws Exception { 38 | List list = this.bookService.findByBookNameLikeAndTagLike("Spring", ""); 39 | assertThat(3).isEqualTo(list.size()); 40 | } 41 | 42 | @Test 43 | public void test_findByBookNameLikeAndTagLike_findTag() throws Exception { 44 | List list = this.bookService.findByBookNameLikeAndTagLike("", "%Docker%"); 45 | assertThat(1).isEqualTo(list.size()); 46 | } 47 | 48 | @Test 49 | public void test_findByBookNameLikeAndTagLike() throws Exception { 50 | List list = this.bookService.findByBookNameLikeAndTagLike("はじめて", ""); 51 | assertThat(1).isEqualTo(list.size()); 52 | } 53 | 54 | @Test 55 | public void testFindOne() throws Exception { 56 | Book result = this.bookService.findOne(new Long(1)); 57 | assertEquals(new Long(1), result.getBookId()); 58 | } 59 | 60 | @Test 61 | @Rollback 62 | public void testSave() throws Exception { 63 | Book book = new Book(); 64 | book.setBookId(new Long(999)); 65 | book.setBookName("test"); 66 | 67 | Book result = this.bookService.save(book); 68 | assertEquals(book.getBookName(), result.getBookName()); 69 | } 70 | 71 | @Test 72 | @Rollback 73 | public void testDelete() throws Exception { 74 | Book book = new Book(); 75 | book.setBookId(new Long(1)); 76 | book.setBookName("test"); 77 | 78 | this.bookService.delete(book); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /bin/templates/book.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Book Search 7 | 8 | 9 | 10 | 11 |
12 |
13 |
14 | 15 |
16 | 17 |
18 |
19 |
20 | 21 |
22 | 23 |
24 |
25 |
26 |
27 | 28 |
29 |
30 |
31 | 32 |
33 | 新規登録 34 |
35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 57 | 58 | 59 |
書籍画像タグ説明
53 |
54 | 編集 / 削除 55 |
56 |
60 |
61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/main/java/info/ewai/sbmt/domain/User.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.domain; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | 7 | import javax.persistence.CascadeType; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.Id; 11 | import javax.persistence.OneToMany; 12 | import javax.persistence.Table; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | import org.springframework.security.core.GrantedAuthority; 17 | import org.springframework.security.core.authority.SimpleGrantedAuthority; 18 | import org.springframework.security.core.userdetails.UserDetails; 19 | 20 | @Entity 21 | @Table(name = "user_info") 22 | public class User implements UserDetails { 23 | 24 | private static final long serialVersionUID = 1L; 25 | 26 | private static Logger logger = LoggerFactory.getLogger(User.class); 27 | 28 | @Id 29 | private Long userId; 30 | private String username; 31 | private String password; 32 | private String email; 33 | 34 | @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.EAGER) 35 | private List authorities; 36 | 37 | @Override 38 | public Collection getAuthorities() { 39 | List result = new ArrayList(); 40 | if (this.authorities != null) { 41 | logger.info("userName:" + this.getUsername()); 42 | for(Authorities auth: this.authorities){ 43 | logger.info("auth:" + auth.getId().getAuthority()); 44 | result.add(new SimpleGrantedAuthority(auth.getId().getAuthority())); 45 | } 46 | } 47 | return result; 48 | } 49 | 50 | @Override 51 | public boolean isAccountNonExpired() { 52 | return true; 53 | } 54 | 55 | @Override 56 | public boolean isAccountNonLocked() { 57 | return true; 58 | } 59 | 60 | @Override 61 | public boolean isCredentialsNonExpired() { 62 | return true; 63 | } 64 | 65 | @Override 66 | public boolean isEnabled() { 67 | return true; 68 | } 69 | 70 | public Long getUserId() { 71 | return userId; 72 | } 73 | 74 | public void setId(Long userId) { 75 | this.userId = userId; 76 | } 77 | 78 | public String getPassword() { 79 | return password; 80 | } 81 | 82 | public void setPassword(String password) { 83 | this.password = password; 84 | } 85 | 86 | public String getUsername() { 87 | return username; 88 | } 89 | 90 | public void setUsername(String username) { 91 | this.username = username; 92 | } 93 | 94 | public String getEmail() { 95 | return email; 96 | } 97 | 98 | public void setEmail(String email) { 99 | this.email = email; 100 | } 101 | } -------------------------------------------------------------------------------- /src/main/resources/templates/book.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Book Search 7 | 8 | 9 | 10 | 11 |
12 |
13 |
14 | 15 |
16 | 17 |
18 |
19 |
20 | 21 |
22 | 23 |
24 |
25 |
26 |
27 | 28 |
29 |
30 |
31 | 32 |
33 | 新規登録 34 |
35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 57 | 58 | 59 |
書籍画像タグ説明
53 |
54 | 編集 / 削除 55 |
56 |
60 |
61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /src/test/java/info/ewai/sbmt/web/BookControllerTests.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.web; 2 | 3 | import static org.hamcrest.CoreMatchers.containsString; 4 | import static org.hamcrest.CoreMatchers.not; 5 | import static org.junit.Assert.assertThat; 6 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; 7 | import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; 8 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 9 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 16 | import org.springframework.boot.test.context.SpringBootTest; 17 | import org.springframework.test.context.junit4.SpringRunner; 18 | import org.springframework.test.web.servlet.MockMvc; 19 | import org.springframework.test.web.servlet.MvcResult; 20 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 21 | import org.springframework.web.context.WebApplicationContext; 22 | 23 | @RunWith(SpringRunner.class) 24 | @SpringBootTest 25 | @AutoConfigureMockMvc 26 | public class BookControllerTests { 27 | 28 | @Autowired 29 | private WebApplicationContext context; 30 | 31 | @Autowired 32 | private MockMvc mvc; 33 | 34 | @Before 35 | public void setup() { 36 | this.mvc = MockMvcBuilders 37 | .webAppContextSetup(context) 38 | .apply(springSecurity()) 39 | .build(); 40 | } 41 | 42 | /** 43 | * Top page tests 44 | */ 45 | @Test 46 | public void testGet_topPage_hasNotAuth() throws Exception { 47 | this.mvc.perform(get("/book")).andExpect(status().is3xxRedirection()); 48 | } 49 | /* 50 | @Test 51 | public void testGet_topPage_hasAuthUser() throws Exception { 52 | MvcResult result = this.mvc.perform(get("/book").with(user("sbt").roles("USER"))) 53 | .andExpect(status().isOk()).andReturn(); 54 | String content = result.getResponse().getContentAsString(); 55 | 56 | assertThat(content, not(containsString(">ログアウト"))); // authenticated 57 | assertThat(content, containsString(">書籍検索")); 58 | assertThat(content, not(containsString(">書籍登録"))); // has not auth 59 | } 60 | 61 | @Test 62 | public void testGet_topPage_hasAuthAdmin() throws Exception { 63 | MvcResult result = this.mvc.perform(get("/").with(user("admin").roles("ADMIN"))) 64 | .andExpect(status().isOk()).andReturn(); 65 | String content = result.getResponse().getContentAsString(); 66 | 67 | assertThat(content, not(containsString(">ログイン"))); // authenticated 68 | assertThat(content, containsString(">書籍検索")); 69 | assertThat(content, containsString(">書籍登録")); // has auth 70 | } 71 | 72 | 73 | */ 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/info/ewai/sbmt/config/SecurityConfigTests.java: -------------------------------------------------------------------------------- 1 | package info.ewai.sbmt.config; 2 | 3 | import static org.hamcrest.CoreMatchers.equalTo; 4 | import static org.junit.Assert.assertThat; 5 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin; 6 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.logout; 7 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; 8 | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; 9 | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated; 10 | import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; 11 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 12 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 13 | 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 19 | import org.springframework.boot.test.context.SpringBootTest; 20 | import org.springframework.test.context.junit4.SpringRunner; 21 | import org.springframework.test.web.servlet.MockMvc; 22 | import org.springframework.test.web.servlet.MvcResult; 23 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 24 | import org.springframework.web.context.WebApplicationContext; 25 | 26 | @RunWith(SpringRunner.class) 27 | @SpringBootTest 28 | @AutoConfigureMockMvc 29 | public class SecurityConfigTests { 30 | 31 | @Autowired 32 | private WebApplicationContext context; 33 | 34 | @Autowired 35 | private MockMvc mvc; 36 | 37 | @Before 38 | public void setup() { 39 | this.mvc = MockMvcBuilders 40 | .webAppContextSetup(context) 41 | .apply(springSecurity()) 42 | .build(); 43 | } 44 | 45 | /** 46 | * Login tests 47 | */ 48 | @Test 49 | public void test_login_user() throws Exception { 50 | this.mvc.perform(formLogin().user("sbt").password("sbt")) 51 | .andExpect(authenticated().withRoles("USER")); 52 | } 53 | 54 | @Test 55 | public void test_login_admin() throws Exception { 56 | this.mvc.perform(formLogin().user("admin").password("admin")) 57 | .andExpect(authenticated()); //.withRoles("ADMIN", "ACTUATOR")); 58 | } 59 | 60 | @Test 61 | public void test_login_invalid() throws Exception { 62 | this.mvc.perform(formLogin().user("sbt").password("invalid")) 63 | .andExpect(unauthenticated()); 64 | } 65 | 66 | @Test 67 | public void testPost_csrf() throws Exception { 68 | this.mvc.perform(post("/").with(csrf())); 69 | } 70 | 71 | /** 72 | * Logout tests 73 | */ 74 | @Test 75 | public void test_logout() throws Exception { 76 | MvcResult result = this.mvc.perform(logout()) 77 | .andExpect(status().is3xxRedirection()).andReturn(); 78 | assertThat("/", equalTo(result.getResponse().getHeader("Location"))); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /bin/templates/book-edit.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | Book 7 | 8 | 9 | 10 | 11 |
12 |
13 | 16 | 17 | 18 |
19 | 20 |
21 | 22 | 23 |
24 |
25 |
26 | 27 |
28 | 29 |
30 |
31 |
32 | 33 |
34 | 35 | 36 |
37 |
38 |
39 | 40 |
41 |