From 8260747f5016c24d0c5f3837a9d33bc1ba2dabca Mon Sep 17 00:00:00 2001 From: Ambrus Adrian-Zoltan Date: Thu, 27 Oct 2016 12:21:50 +0300 Subject: [PATCH] Removed the spring-security-custom-voter module altogether. Integrated the custom voter example into the spring-security-custom-permission module. --- spring-security-custom-permission/pom.xml | 4 + .../main/java/org/baeldung/Application.java | 11 ++- .../org/baeldung/voter}/MinuteBasedVoter.java | 2 +- .../org/baeldung/voter/VoterApplication.java | 17 ++++ .../org/baeldung/voter/VoterMvcConfig.java | 4 +- .../baeldung/voter}/WebSecurityConfig.java | 6 +- .../baeldung/voter}/XmlSecurityConfig.java | 4 +- .../src/main/resources/spring-security.xml | 6 +- .../src/main/resources/templates/private.html | 0 spring-security-custom-voter/pom.xml | 96 ------------------- .../main/java/org/baeldung/Application.java | 12 --- .../src/main/webapp/WEB-INF/web.xml | 47 --------- .../test/java/org/baeldung/web/LiveTest.java | 42 -------- 13 files changed, 40 insertions(+), 211 deletions(-) rename {spring-security-custom-voter/src/main/java/org/baeldung/security => spring-security-custom-permission/src/main/java/org/baeldung/voter}/MinuteBasedVoter.java (97%) create mode 100644 spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java rename spring-security-custom-voter/src/main/java/org/baeldung/web/MvcConfig.java => spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterMvcConfig.java (85%) rename {spring-security-custom-voter/src/main/java/org/baeldung/security => spring-security-custom-permission/src/main/java/org/baeldung/voter}/WebSecurityConfig.java (97%) rename {spring-security-custom-voter/src/main/java/org/baeldung/security => spring-security-custom-permission/src/main/java/org/baeldung/voter}/XmlSecurityConfig.java (77%) rename {spring-security-custom-voter => spring-security-custom-permission}/src/main/resources/spring-security.xml (91%) rename {spring-security-custom-voter => spring-security-custom-permission}/src/main/resources/templates/private.html (100%) delete mode 100644 spring-security-custom-voter/pom.xml delete mode 100644 spring-security-custom-voter/src/main/java/org/baeldung/Application.java delete mode 100644 spring-security-custom-voter/src/main/webapp/WEB-INF/web.xml delete mode 100644 spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java diff --git a/spring-security-custom-permission/pom.xml b/spring-security-custom-permission/pom.xml index 94f6006dc8..288cc3d6ba 100644 --- a/spring-security-custom-permission/pom.xml +++ b/spring-security-custom-permission/pom.xml @@ -128,6 +128,7 @@ spring-security-taglibs 4.1.3.RELEASE + javax.servlet.jsp.jstl jstl-api @@ -239,6 +240,9 @@ + org.baeldung.Application + + UTF-8 1.8 2.4.0 diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/Application.java b/spring-security-custom-permission/src/main/java/org/baeldung/Application.java index b4e8d04b49..2d59fab6be 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/Application.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/Application.java @@ -1,12 +1,17 @@ package org.baeldung; import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.web.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; -@SpringBootApplication +@Configuration +@EnableAutoConfiguration +@ComponentScan(excludeFilters = + @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*")) public class Application extends SpringBootServletInitializer { - public static void main(String[] args) { SpringApplication.run(Application.class, args); } diff --git a/spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java b/spring-security-custom-permission/src/main/java/org/baeldung/voter/MinuteBasedVoter.java similarity index 97% rename from spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java rename to spring-security-custom-permission/src/main/java/org/baeldung/voter/MinuteBasedVoter.java index a08079fcff..ca9072bcac 100644 --- a/spring-security-custom-voter/src/main/java/org/baeldung/security/MinuteBasedVoter.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/voter/MinuteBasedVoter.java @@ -1,4 +1,4 @@ -package org.baeldung.security; +package org.baeldung.voter; import java.time.LocalDateTime; import java.util.Collection; diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java b/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java new file mode 100644 index 0000000000..046eca9be7 --- /dev/null +++ b/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java @@ -0,0 +1,17 @@ +package org.baeldung.voter; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +@Configuration +@EnableAutoConfiguration +@ComponentScan(basePackages = {"org.baeldung.voter"}) +public class VoterApplication { + + public static void main(String[] args) { + SpringApplication.run(VoterApplication.class, args); + } +} diff --git a/spring-security-custom-voter/src/main/java/org/baeldung/web/MvcConfig.java b/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterMvcConfig.java similarity index 85% rename from spring-security-custom-voter/src/main/java/org/baeldung/web/MvcConfig.java rename to spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterMvcConfig.java index 5d38dca1ff..e282c794d0 100644 --- a/spring-security-custom-voter/src/main/java/org/baeldung/web/MvcConfig.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterMvcConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.web; +package org.baeldung.voter; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -10,7 +10,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter */ @Configuration -public class MvcConfig extends WebMvcConfigurerAdapter { +public class VoterMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("private"); diff --git a/spring-security-custom-voter/src/main/java/org/baeldung/security/WebSecurityConfig.java b/spring-security-custom-permission/src/main/java/org/baeldung/voter/WebSecurityConfig.java similarity index 97% rename from spring-security-custom-voter/src/main/java/org/baeldung/security/WebSecurityConfig.java rename to spring-security-custom-permission/src/main/java/org/baeldung/voter/WebSecurityConfig.java index b3fb196424..3330fda7ec 100644 --- a/spring-security-custom-voter/src/main/java/org/baeldung/security/WebSecurityConfig.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/voter/WebSecurityConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.security; +package org.baeldung.voter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -18,8 +18,8 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import java.util.Arrays; import java.util.List; -@Configuration -@EnableWebSecurity +//@Configuration +//@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { diff --git a/spring-security-custom-voter/src/main/java/org/baeldung/security/XmlSecurityConfig.java b/spring-security-custom-permission/src/main/java/org/baeldung/voter/XmlSecurityConfig.java similarity index 77% rename from spring-security-custom-voter/src/main/java/org/baeldung/security/XmlSecurityConfig.java rename to spring-security-custom-permission/src/main/java/org/baeldung/voter/XmlSecurityConfig.java index 45e095c66e..c206df7d1f 100644 --- a/spring-security-custom-voter/src/main/java/org/baeldung/security/XmlSecurityConfig.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/voter/XmlSecurityConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.security; +package org.baeldung.voter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @@ -7,7 +7,7 @@ import org.springframework.context.annotation.ImportResource; * Created by ambrusadrianz on 09/10/2016. */ @Configuration -//@ImportResource({"classpath:spring-security.xml"}) +@ImportResource({"classpath:spring-security.xml"}) public class XmlSecurityConfig { public XmlSecurityConfig() { super(); diff --git a/spring-security-custom-voter/src/main/resources/spring-security.xml b/spring-security-custom-permission/src/main/resources/spring-security.xml similarity index 91% rename from spring-security-custom-voter/src/main/resources/spring-security.xml rename to spring-security-custom-permission/src/main/resources/spring-security.xml index 117638289e..382dbf5dff 100644 --- a/spring-security-custom-voter/src/main/resources/spring-security.xml +++ b/spring-security-custom-permission/src/main/resources/spring-security.xml @@ -4,7 +4,7 @@ xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/security - http://www.springframework.org/schema/security/spring-security-4.1.xsd + http://www.springframework.org/schema/security/spring-security-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> @@ -22,12 +22,12 @@ - + - + diff --git a/spring-security-custom-voter/src/main/resources/templates/private.html b/spring-security-custom-permission/src/main/resources/templates/private.html similarity index 100% rename from spring-security-custom-voter/src/main/resources/templates/private.html rename to spring-security-custom-permission/src/main/resources/templates/private.html diff --git a/spring-security-custom-voter/pom.xml b/spring-security-custom-voter/pom.xml deleted file mode 100644 index 727efa48ab..0000000000 --- a/spring-security-custom-voter/pom.xml +++ /dev/null @@ -1,96 +0,0 @@ - - - 4.0.0 - - org.baeldung - spring-security-custom-voter - 0.0.1-SNAPSHOT - war - - spring-security-custom-voter - Custom AccessDecisionVoter with Spring Security - - - org.springframework.boot - spring-boot-starter-parent - 1.4.1.RELEASE - - - - - UTF-8 - UTF-8 - 3.0.1 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - junit - junit - test - - - - org.hamcrest - hamcrest-core - test - - - - org.hamcrest - hamcrest-library - test - - - - org.springframework - spring-test - 4.3.3.RELEASE - - - - org.springframework.security - spring-security-test - 4.1.3.RELEASE - test - - - - org.springframework.security - spring-security-web - 4.1.3.RELEASE - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-security-custom-voter/src/main/java/org/baeldung/Application.java b/spring-security-custom-voter/src/main/java/org/baeldung/Application.java deleted file mode 100644 index a9d6f3b8b1..0000000000 --- a/spring-security-custom-voter/src/main/java/org/baeldung/Application.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.baeldung; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} diff --git a/spring-security-custom-voter/src/main/webapp/WEB-INF/web.xml b/spring-security-custom-voter/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index d69bce35ae..0000000000 --- a/spring-security-custom-voter/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - Spring Secured Application - - - - mvc - org.springframework.web.servlet.DispatcherServlet - 1 - - - mvc - / - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.spring.web.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - - springSecurityFilterChain - org.springframework.web.filter.DelegatingFilterProxy - - - springSecurityFilterChain - /* - - - \ No newline at end of file diff --git a/spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java b/spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java deleted file mode 100644 index 3ddeab5920..0000000000 --- a/spring-security-custom-voter/src/test/java/org/baeldung/web/LiveTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.baeldung.web; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -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.request.SecurityMockMvcRequestBuilders.formLogin; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; -import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; -import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @WebAppConfiguration @SpringBootTest public class LiveTest { - @Autowired private WebApplicationContext context; - - private MockMvc mvc; - - @Before public void setup() { - mvc = MockMvcBuilders.webAppContextSetup(context).apply(springSecurity()).build(); - } - - @Test public void givenUnauthenticatedUser_whenAccessingMainPage_thenRedirect() throws Exception { - mvc.perform(get("/")).andExpect(status().is3xxRedirection()); - } - - @Test public void givenValidUsernameAndPassword_whenLogin_thenOK() throws Exception { - mvc.perform(formLogin("/login").user("user").password("pass")).andExpect(authenticated()); - } - - @Test public void givenAuthenticatedAdmin_whenAccessingMainPage_thenOK() throws Exception { - mvc.perform(get("/").with(user("admin").password("pass").roles("ADMIN"))).andExpect(status().isOk()); - } -}