Skip to content

Commit

Permalink
Bootificaiton of the app.
Browse files Browse the repository at this point in the history
Minimal security configuration added.
  • Loading branch information
japarejo committed Jan 29, 2020
1 parent 9387366 commit 1cf8813
Show file tree
Hide file tree
Showing 75 changed files with 1,960 additions and 1,164 deletions.
533 changes: 247 additions & 286 deletions pom.xml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion push-to-pws/button.yml

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.springframework.samples.petclinic;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class PetclinicInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBoot3LayersPetclinicApplication.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.springframework.samples.petclinic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAutoConfiguration
public class SpringBoot3LayersPetclinicApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBoot3LayersPetclinicApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.springframework.samples.petclinic.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;

/**
* This advice is necessary because MockMvc is not a real servlet environment, therefore it does not redirect error
* responses to [ErrorController], which produces validation response. So we need to fake it in tests.
* It's not ideal, but at least we can use classic MockMvc tests for testing error response + document it.
*/
@ControllerAdvice
public class ExceptionHandlerConfiguration
{
@Autowired
private BasicErrorController errorController;
// add any exceptions/validations/binding problems

@ExceptionHandler(Exception.class)
public String defaultErrorHandler(HttpServletRequest request, Exception ex) {
request.setAttribute("javax.servlet.error.request_uri", request.getPathInfo());
request.setAttribute("javax.servlet.error.status_code", 400);
request.setAttribute("exeption", ex);
return "exception";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.springframework.samples.petclinic.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
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.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* @author japarejo
*/
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.GET, "/**").permitAll()
.antMatchers(HttpMethod.POST, "/**").permitAll().and()
// .formLogin()
// .loginPage("/login").failureUrl("/login-error")
.httpBasic()
.and().csrf().disable();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth.inMemoryAuthentication().withUser("spring").password(encoder.encode("password")).roles("USER");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -15,8 +15,6 @@
*/
package org.springframework.samples.petclinic.model;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
Expand All @@ -30,11 +28,11 @@
* @author Juergen Hoeller
*/
@MappedSuperclass
public class BaseEntity implements Serializable {
public class BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
protected Integer id;

public Integer getId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,6 +17,7 @@

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.Size;

/**
* Simple JavaBean domain object adds a name property to <code>BaseEntity</code>. Used as
Expand All @@ -28,6 +29,7 @@
@MappedSuperclass
public class NamedEntity extends BaseEntity {

@Size(min = 3, max = 50)
@Column(name = "name")
private String name;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.owner;
package org.springframework.samples.petclinic.model;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -32,7 +32,6 @@
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person;

/**
* Simple JavaBean domain object representing an owner.
Expand Down Expand Up @@ -104,9 +103,7 @@ public List<Pet> getPets() {
}

public void addPet(Pet pet) {
if (pet.isNew()) {
getPetsInternal().add(pet);
}
getPetsInternal().add(pet);
pet.setOwner(this);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -29,11 +29,11 @@ public class Person extends BaseEntity {

@Column(name = "first_name")
@NotEmpty
private String firstName;
protected String firstName;

@Column(name = "last_name")
@NotEmpty
private String lastName;
protected String lastName;

public String getFirstName() {
return this.firstName;
Expand Down
44 changes: 21 additions & 23 deletions ...ramework/samples/petclinic/owner/Pet.java → ...ramework/samples/petclinic/model/Pet.java
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.samples.petclinic.owner;
package org.springframework.samples.petclinic.model;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.samples.petclinic.model.NamedEntity;
import org.springframework.samples.petclinic.visit.Visit;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Simple business object representing a pet.
Expand All @@ -49,7 +47,7 @@
public class Pet extends NamedEntity {

@Column(name = "birth_date")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy/MM/dd")
private LocalDate birthDate;

@ManyToOne
Expand All @@ -60,8 +58,8 @@ public class Pet extends NamedEntity {
@JoinColumn(name = "owner_id")
private Owner owner;

@Transient
private Set<Visit> visits = new LinkedHashSet<>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.EAGER)
private Set<Visit> visits;

public void setBirthDate(LocalDate birthDate) {
this.birthDate = birthDate;
Expand Down Expand Up @@ -94,8 +92,8 @@ protected Set<Visit> getVisitsInternal() {
return this.visits;
}

protected void setVisitsInternal(Collection<Visit> visits) {
this.visits = new LinkedHashSet<>(visits);
protected void setVisitsInternal(Set<Visit> visits) {
this.visits = visits;
}

public List<Visit> getVisits() {
Expand All @@ -106,7 +104,7 @@ public List<Visit> getVisits() {

public void addVisit(Visit visit) {
getVisitsInternal().add(visit);
visit.setPetId(this.getId());
visit.setPet(this);
}

}
Loading

0 comments on commit 1cf8813

Please sign in to comment.