diff --git a/pom.xml b/pom.xml index 63f5cbd..95ffd5c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,13 +5,13 @@ org.springframework gs-spring-boot - pom + jar 0.1.0 org.springframework.boot spring-boot-starter-parent - 2.0.2.RELEASE + 2.7.18 @@ -32,6 +32,10 @@ com.h2database h2 + + org.springframework.boot + spring-boot-starter-security + diff --git a/src/main/java/hello/Application.java b/src/main/java/hello/Application.java index 7cf8faf..567cb39 100644 --- a/src/main/java/hello/Application.java +++ b/src/main/java/hello/Application.java @@ -36,8 +36,12 @@ public static void main(String[] args) { } RestTemplate restTemplate = new RestTemplate(); - Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class); - log.info(quote.toString()); + try { + Quote quote = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class); + log.info(quote.toString()); + } catch (Exception e) { + log.warn("Could not reach external quote service: {}", e.getMessage()); + } } @@ -49,9 +53,13 @@ public RestTemplate restTemplate(RestTemplateBuilder builder) { @Bean public CommandLineRunner run(RestTemplate restTemplate) throws Exception { return args -> { - Quote quote = restTemplate.getForObject( - "http://gturnquist-quoters.cfapps.io/api/random", Quote.class); - log.info(quote.toString()); + try { + Quote quote = restTemplate.getForObject( + "https://gturnquist-quoters.cfapps.io/api/random", Quote.class); + log.info(quote.toString()); + } catch (Exception e) { + log.warn("Could not reach external quote service: {}", e.getMessage()); + } }; } @@ -80,8 +88,9 @@ public void run(String... args) throws Exception { log.info("Querying for customer records where first_name = 'Josh':"); jdbcTemplate.query( - "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[]{"Josh"}, - (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")) + "SELECT id, first_name, last_name FROM customers WHERE first_name = ?", + (rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name")), + "Josh" ).forEach(customer -> log.info(customer.toString())); } diff --git a/src/main/java/hello/config/SecurityConfig.java b/src/main/java/hello/config/SecurityConfig.java new file mode 100644 index 0000000..543e17b --- /dev/null +++ b/src/main/java/hello/config/SecurityConfig.java @@ -0,0 +1,26 @@ +package hello.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +@EnableWebSecurity +public class SecurityConfig { + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers(HttpMethod.GET, "/", "/greeting").permitAll() + .anyRequest().authenticated() + .and() + .httpBasic() + .and() + .csrf().disable(); // Disable CSRF for REST API; consider enabling if serving browser forms + return http.build(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..10b9696 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +#server.port = 8081 + +spring.h2.console.enabled=true +spring.datasource.url=jdbc:h2:mem:testdb