Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[![CircleCI](https://circleci.com/gh/sfg-beer-works/brewery-monolith.svg?style=svg)](https://circleci.com/gh/sfg-beer-works/brewery-monolith)
# Brewery Spring MVC Monolith

This repository contains source code examples used to support my on-line courses about the Spring Framework.

You can learn more about the courses here:
* [Spring Security Core: Beginner to Guru](https://www.udemy.com/course/spring-boot-microservices-with-spring-cloud-beginner-to-guru/?referralCode=6142D427AE53031FEF38)
* [Spring Security Core: Beginner to Guru](https://www.udemy.com/course/spring-security-core-beginner-to-guru/?referralCode=306F288EB78688C0F3BC)
* [Spring Boot Microservices with Spring Cloud](https://www.udemy.com/course/spring-boot-microservices-with-spring-cloud-beginner-to-guru/?referralCode=6142D427AE53031FEF38)
* [Spring Framework 5: Beginner to Guru](https://www.udemy.com/course/spring-framework-5-beginner-to-guru/?referralCode=6D9ECD1F93988FEE5CE9)
* [Testing Spring Boot: Beginner to Guru](https://www.udemy.com/course/testing-spring-boot-beginner-to-guru/?referralCode=EFFE87DDE96C8541B2EE)
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/guru/sfg/brewery/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package guru.sfg.brewery.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
* Created by jt on 6/13/20.
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests(authorize -> {
authorize.antMatchers("/", "/webjars/**", "/login", "/resources/**").permitAll()
.antMatchers("/beers/find", "/beers*").permitAll();
} )
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().and()
.httpBasic();
}
}
21 changes: 4 additions & 17 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
#
# Copyright 2019 the original author or authors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
spring.application.name=brewery-monolith
spring.jackson.serialization.write-dates-as-timestamps=false
spring.jackson.serialization.write-date-timestamps-as-nanoseconds=true
Expand All @@ -23,4 +7,7 @@ spring.messages.basename=messages/messages
logging.level.guru=debug

# Spring Data hangs when not set under Spring Boot 2.3.0
spring.data.jpa.repositories.bootstrap-mode=default
spring.data.jpa.repositories.bootstrap-mode=default

spring.security.user.name=spring
spring.security.user.password=guru
3 changes: 0 additions & 3 deletions src/main/resources/static/resources/application.properties

This file was deleted.

48 changes: 48 additions & 0 deletions src/test/java/guru/sfg/brewery/web/controllers/BaseIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package guru.sfg.brewery.web.controllers;

import guru.sfg.brewery.repositories.BeerInventoryRepository;
import guru.sfg.brewery.repositories.BeerRepository;
import guru.sfg.brewery.repositories.CustomerRepository;
import guru.sfg.brewery.services.BeerService;
import guru.sfg.brewery.services.BreweryService;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;

/**
* Created by jt on 6/13/20.
*/
public abstract class BaseIT {
@Autowired
WebApplicationContext wac;

MockMvc mockMvc;

@MockBean
BeerRepository beerRepository;

@MockBean
BeerInventoryRepository beerInventoryRepository;

@MockBean
BreweryService breweryService;

@MockBean
CustomerRepository customerRepository;

@MockBean
BeerService beerService;

@BeforeEach
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.apply(springSecurity())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package guru.sfg.brewery.web.controllers;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
* Created by jt on 6/12/20.
*/
@WebMvcTest
public class BeerControllerIT extends BaseIT{

@WithMockUser("spring")
@Test
void findBeers() throws Exception{
mockMvc.perform(get("/beers/find"))
.andExpect(status().isOk())
.andExpect(view().name("beers/findBeers"))
.andExpect(model().attributeExists("beer"));
}

@Test
void findBeersWithHttpBasic() throws Exception{
mockMvc.perform(get("/beers/find").with(httpBasic("spring", "guru")))
.andExpect(status().isOk())
.andExpect(view().name("beers/findBeers"))
.andExpect(model().attributeExists("beer"));
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package guru.sfg.brewery.web.controllers;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
* Created by jt on 6/13/20.
*/
@WebMvcTest
public class IndexControllerIT extends BaseIT {

@Test
void testGetIndexSlash() throws Exception{
mockMvc.perform(get("/" ))
.andExpect(status().isOk());
}
}