forked from gii-is-DP2/spring-petclinic
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimal security configuration added.
- Loading branch information
Showing
75 changed files
with
1,960 additions
and
1,164 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
35 changes: 0 additions & 35 deletions
35
src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
src/main/java/org/springframework/samples/petclinic/PetclinicInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...ain/java/org/springframework/samples/petclinic/SpringBoot3LayersPetclinicApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...va/org/springframework/samples/petclinic/configuration/ExceptionHandlerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/springframework/samples/petclinic/configuration/SecurityConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.