From 444f059c8eed17852aca4751fcc13dd90e388558 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 20 May 2018 12:07:44 +0300 Subject: [PATCH] boot2 upgrade --- spring-security-mvc-boot/pom.xml | 35 +----- .../main/java/org/baeldung/Application.java | 19 --- .../org/baeldung/acl/config/ACLContext.java | 80 ------------ .../AclMethodSecurityConfiguration.java | 21 ---- .../acl/config/JPAPersistenceConfig.java | 16 --- .../dao/NoticeMessageRepository.java | 24 ---- .../acl/persistence/entity/NoticeMessage.java | 29 ----- .../baeldung/config/PersistenceConfig.java | 82 ------------ .../java/org/baeldung/custom/Application.java | 16 +++ .../config/MethodSecurityConfig.java | 6 +- .../{ => custom}/config/MvcConfig.java | 7 +- .../{ => custom}/config/SecurityConfig.java | 4 +- .../{ => custom}/persistence/SetupData.java | 14 +-- .../dao/OrganizationRepository.java | 4 +- .../persistence/dao/PrivilegeRepository.java | 4 +- .../persistence/dao/UserRepository.java | 4 +- .../{ => custom}/persistence/model/Foo.java | 2 +- .../persistence/model/Organization.java | 2 +- .../persistence/model/Privilege.java | 2 +- .../{ => custom}/persistence/model/User.java | 2 +- ...CustomMethodSecurityExpressionHandler.java | 2 +- .../CustomMethodSecurityExpressionRoot.java | 4 +- .../security/CustomPermissionEvaluator.java | 2 +- .../security/MySecurityExpressionRoot.java | 4 +- .../security/MyUserDetailsService.java | 6 +- .../security/MyUserPrincipal.java | 6 +- .../{ => custom}/web/MainController.java | 10 +- .../MultipleAuthController.java | 4 +- .../MultipleAuthProvidersSecurityConfig.java | 10 +- .../MultipleEntryPointsSecurityConfig.java | 11 +- .../multiplelogin/MultipleLoginMvcConfig.java | 5 +- .../MultipleLoginSecurityConfig.java | 18 +-- .../multiplelogin/UsersController.java | 14 +-- .../RolesAuthoritiesApplication.java | 2 +- .../rolesauthorities/config/MvcConfig.java | 5 +- .../persistence/PersistenceJPAConfig.java | 77 ------------ .../org/baeldung/voter/VoterMvcConfig.java | 3 +- .../org/baeldung/voter/WebSecurityConfig.java | 9 +- .../src/main/resources/acl-data.sql | 28 ----- .../src/main/resources/acl-schema.sql | 58 --------- .../application-rolesauthorities.properties | 10 -- .../src/main/resources/application.properties | 1 - .../org.baeldung.acl.datasource.properties | 12 -- .../resources/persistence-derby.properties | 12 -- .../src/main/resources/persistence.properties | 11 -- .../SpringContextIntegrationTest.java | 1 + .../acl/SpringAclIntegrationTest.java | 119 ------------------ .../org/baeldung/web/ApplicationLiveTest.java | 18 +-- ...stomUserDetailsServiceIntegrationTest.java | 12 +- 49 files changed, 130 insertions(+), 717 deletions(-) delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/Application.java delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/ACLContext.java delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/AclMethodSecurityConfiguration.java delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/JPAPersistenceConfig.java delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/dao/NoticeMessageRepository.java delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/entity/NoticeMessage.java delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/config/PersistenceConfig.java create mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/custom/Application.java rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/config/MethodSecurityConfig.java (84%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/config/MvcConfig.java (90%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/config/SecurityConfig.java (96%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/SetupData.java (84%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/dao/OrganizationRepository.java (65%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/dao/PrivilegeRepository.java (65%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/dao/UserRepository.java (76%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/model/Foo.java (97%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/model/Organization.java (97%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/model/Privilege.java (97%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/persistence/model/User.java (98%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/security/CustomMethodSecurityExpressionHandler.java (97%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/security/CustomMethodSecurityExpressionRoot.java (93%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/security/CustomPermissionEvaluator.java (97%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/security/MySecurityExpressionRoot.java (98%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/security/MyUserDetailsService.java (84%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/security/MyUserPrincipal.java (90%) rename spring-security-mvc-boot/src/main/java/org/baeldung/{ => custom}/web/MainController.java (86%) delete mode 100644 spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java delete mode 100644 spring-security-mvc-boot/src/main/resources/acl-data.sql delete mode 100644 spring-security-mvc-boot/src/main/resources/acl-schema.sql delete mode 100644 spring-security-mvc-boot/src/main/resources/application-rolesauthorities.properties delete mode 100644 spring-security-mvc-boot/src/main/resources/org.baeldung.acl.datasource.properties delete mode 100644 spring-security-mvc-boot/src/main/resources/persistence-derby.properties delete mode 100644 spring-security-mvc-boot/src/main/resources/persistence.properties delete mode 100644 spring-security-mvc-boot/src/test/java/org/baeldung/acl/SpringAclIntegrationTest.java diff --git a/spring-security-mvc-boot/pom.xml b/spring-security-mvc-boot/pom.xml index bbc701313a..fe95461eab 100644 --- a/spring-security-mvc-boot/pom.xml +++ b/spring-security-mvc-boot/pom.xml @@ -12,10 +12,10 @@ Spring Security MVC Boot - parent-boot-5 + parent-boot-2 com.baeldung 0.0.1-SNAPSHOT - ../parent-boot-5 + ../parent-boot-2 @@ -79,26 +79,6 @@ test - - org.apache.derby - derby - ${derby.version} - - - org.apache.derby - derbyclient - ${derby.version} - - - org.apache.derby - derbynet - ${derby.version} - - - org.apache.derby - derbytools - ${derby.version} - taglibs standard @@ -107,13 +87,11 @@ org.springframework.security spring-security-taglibs - ${spring-security-taglibs.version} org.springframework.security spring-security-core - ${spring-security-core.version} @@ -122,10 +100,6 @@ ${jstl.version} - - org.springframework.security - spring-security-acl - org.springframework.security spring-security-config @@ -268,12 +242,7 @@ comment the tag above and uncomment the one below --> - 10.13.1.1 1.1.2 - 4.2.2.RELEASE - 4.2.2.RELEASE - 4.2.2.RELEASE - 1.2 1.6.1 diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java deleted file mode 100644 index 8a40744bdc..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.baeldung; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.web.support.SpringBootServletInitializer; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@EnableAutoConfiguration -@ComponentScan({ "org.baeldung.config", "org.baeldung.persistence", "org.baeldung.security", "org.baeldung.web" }) -// @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleauthproviders.*"), -// @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*"), -// @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.rolesauthorities.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.acl.*") }) -public class Application extends SpringBootServletInitializer { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/ACLContext.java b/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/ACLContext.java deleted file mode 100644 index 63a4ea58ef..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/ACLContext.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.baeldung.acl.config; - -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.cache.ehcache.EhCacheFactoryBean; -import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; -import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; -import org.springframework.security.acls.AclPermissionCacheOptimizer; -import org.springframework.security.acls.AclPermissionEvaluator; -import org.springframework.security.acls.domain.AclAuthorizationStrategy; -import org.springframework.security.acls.domain.AclAuthorizationStrategyImpl; -import org.springframework.security.acls.domain.ConsoleAuditLogger; -import org.springframework.security.acls.domain.DefaultPermissionGrantingStrategy; -import org.springframework.security.acls.domain.EhCacheBasedAclCache; -import org.springframework.security.acls.jdbc.BasicLookupStrategy; -import org.springframework.security.acls.jdbc.JdbcMutableAclService; -import org.springframework.security.acls.jdbc.LookupStrategy; -import org.springframework.security.acls.model.PermissionGrantingStrategy; -import org.springframework.security.core.authority.SimpleGrantedAuthority; - -@Configuration -@EnableAutoConfiguration -public class ACLContext { - - @Autowired - DataSource dataSource; - - @Bean - public EhCacheBasedAclCache aclCache() { - return new EhCacheBasedAclCache(aclEhCacheFactoryBean().getObject(), permissionGrantingStrategy(), aclAuthorizationStrategy()); - } - - @Bean - public EhCacheFactoryBean aclEhCacheFactoryBean() { - EhCacheFactoryBean ehCacheFactoryBean = new EhCacheFactoryBean(); - ehCacheFactoryBean.setCacheManager(aclCacheManager().getObject()); - ehCacheFactoryBean.setCacheName("aclCache"); - return ehCacheFactoryBean; - } - - @Bean - public EhCacheManagerFactoryBean aclCacheManager() { - return new EhCacheManagerFactoryBean(); - } - - @Bean - public PermissionGrantingStrategy permissionGrantingStrategy() { - return new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger()); - } - - @Bean - public AclAuthorizationStrategy aclAuthorizationStrategy() { - return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_ADMIN")); - } - - @Bean - public MethodSecurityExpressionHandler defaultMethodSecurityExpressionHandler() { - DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler(); - AclPermissionEvaluator permissionEvaluator = new AclPermissionEvaluator(aclService()); - expressionHandler.setPermissionEvaluator(permissionEvaluator); - expressionHandler.setPermissionCacheOptimizer(new AclPermissionCacheOptimizer(aclService())); - return expressionHandler; - } - - @Bean - public LookupStrategy lookupStrategy() { - return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), new ConsoleAuditLogger()); - } - - @Bean - public JdbcMutableAclService aclService() { - return new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache()); - } - -} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/AclMethodSecurityConfiguration.java b/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/AclMethodSecurityConfiguration.java deleted file mode 100644 index 110c4a6d24..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/AclMethodSecurityConfiguration.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.baeldung.acl.config; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; -import org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; - -@Configuration -@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) -public class AclMethodSecurityConfiguration extends GlobalMethodSecurityConfiguration { - - @Autowired - MethodSecurityExpressionHandler defaultMethodSecurityExpressionHandler; - - @Override - protected MethodSecurityExpressionHandler createExpressionHandler() { - return defaultMethodSecurityExpressionHandler; - } - -} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/JPAPersistenceConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/JPAPersistenceConfig.java deleted file mode 100644 index 9b87efa92c..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/config/JPAPersistenceConfig.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.baeldung.acl.config; - -import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@Configuration -@EnableTransactionManagement -@EnableJpaRepositories(basePackages = "org.baeldung.acl.persistence.dao") -@PropertySource("classpath:org.baeldung.acl.datasource.properties") -@EntityScan(basePackages={ "org.baeldung.acl.persistence.entity" }) -public class JPAPersistenceConfig { - -} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/dao/NoticeMessageRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/dao/NoticeMessageRepository.java deleted file mode 100644 index 8662c88d6c..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/dao/NoticeMessageRepository.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.baeldung.acl.persistence.dao; - -import java.util.List; - -import org.baeldung.acl.persistence.entity.NoticeMessage; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.query.Param; -import org.springframework.security.access.prepost.PostAuthorize; -import org.springframework.security.access.prepost.PostFilter; -import org.springframework.security.access.prepost.PreAuthorize; - -public interface NoticeMessageRepository extends JpaRepository{ - - @PostFilter("hasPermission(filterObject, 'READ')") - List findAll(); - - @PostAuthorize("hasPermission(returnObject, 'READ')") - NoticeMessage findById(Integer id); - - @SuppressWarnings("unchecked") - @PreAuthorize("hasPermission(#noticeMessage, 'WRITE')") - NoticeMessage save(@Param("noticeMessage")NoticeMessage noticeMessage); - -} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/entity/NoticeMessage.java b/spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/entity/NoticeMessage.java deleted file mode 100644 index 23f01a8edb..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/acl/persistence/entity/NoticeMessage.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.baeldung.acl.persistence.entity; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(name="system_message") -public class NoticeMessage { - - @Id - @Column - private Integer id; - @Column - private String content; - public Integer getId() { - return id; - } - public void setId(Integer id) { - this.id = id; - } - public String getContent() { - return content; - } - public void setContent(String content) { - this.content = content; - } -} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/config/PersistenceConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/config/PersistenceConfig.java deleted file mode 100644 index 1c4cb0124a..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/config/PersistenceConfig.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.baeldung.config; - -import java.util.Properties; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@Configuration -@EnableTransactionManagement -@PropertySource({ "classpath:persistence-derby.properties" }) -@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") -public class PersistenceConfig { - - @Autowired - private Environment env; - - public PersistenceConfig() { - super(); - } - - // beans - - @Bean - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); - em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); - - final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - em.setJpaVendorAdapter(vendorAdapter); - em.setJpaProperties(additionalProperties()); - - return em; - } - - @Bean - public DataSource dataSource() { - final DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); - dataSource.setUrl(env.getProperty("jdbc.url")); - dataSource.setUsername(env.getProperty("jdbc.user")); - dataSource.setPassword(env.getProperty("jdbc.pass")); - - return dataSource; - } - - @Bean - public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) { - final JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(emf); - return transactionManager; - } - - @Bean - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { - return new PersistenceExceptionTranslationPostProcessor(); - } - - final Properties additionalProperties() { - final Properties hibernateProperties = new Properties(); - hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); - hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache")); - hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache")); - // hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); - return hibernateProperties; - } -} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/custom/Application.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/Application.java new file mode 100644 index 0000000000..f932ac3066 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/Application.java @@ -0,0 +1,16 @@ +package org.baeldung.custom; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/config/MethodSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/MethodSecurityConfig.java similarity index 84% rename from spring-security-mvc-boot/src/main/java/org/baeldung/config/MethodSecurityConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/MethodSecurityConfig.java index c4624e85e0..6a005153dc 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/config/MethodSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/MethodSecurityConfig.java @@ -1,7 +1,7 @@ -package org.baeldung.config; +package org.baeldung.custom.config; -import org.baeldung.security.CustomMethodSecurityExpressionHandler; -import org.baeldung.security.CustomPermissionEvaluator; +import org.baeldung.custom.security.CustomMethodSecurityExpressionHandler; +import org.baeldung.custom.security.CustomPermissionEvaluator; import org.springframework.context.annotation.Configuration; import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/config/MvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/MvcConfig.java similarity index 90% rename from spring-security-mvc-boot/src/main/java/org/baeldung/config/MvcConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/MvcConfig.java index 9ade60e54c..58d11ea9ae 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/config/MvcConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/MvcConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.config; +package org.baeldung.custom.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -7,11 +7,11 @@ import org.springframework.web.servlet.config.annotation.DefaultServletHandlerCo import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc -public class MvcConfig extends WebMvcConfigurerAdapter { +public class MvcConfig implements WebMvcConfigurer { public MvcConfig() { super(); @@ -30,7 +30,6 @@ public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); registry.addViewController("/").setViewName("forward:/index"); registry.addViewController("/index"); } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/config/SecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/SecurityConfig.java similarity index 96% rename from spring-security-mvc-boot/src/main/java/org/baeldung/config/SecurityConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/SecurityConfig.java index acb7e6820a..f2da5af92c 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/config/SecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/config/SecurityConfig.java @@ -1,6 +1,6 @@ -package org.baeldung.config; +package org.baeldung.custom.config; -import org.baeldung.security.MyUserDetailsService; +import org.baeldung.custom.security.MyUserDetailsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/SetupData.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/SetupData.java similarity index 84% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/SetupData.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/SetupData.java index 2cc86167e2..f0fcce3908 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/SetupData.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/SetupData.java @@ -1,16 +1,16 @@ -package org.baeldung.persistence; +package org.baeldung.custom.persistence; import java.util.Arrays; import java.util.HashSet; import javax.annotation.PostConstruct; -import org.baeldung.persistence.dao.OrganizationRepository; -import org.baeldung.persistence.dao.PrivilegeRepository; -import org.baeldung.persistence.dao.UserRepository; -import org.baeldung.persistence.model.Organization; -import org.baeldung.persistence.model.Privilege; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.persistence.dao.OrganizationRepository; +import org.baeldung.custom.persistence.dao.PrivilegeRepository; +import org.baeldung.custom.persistence.dao.UserRepository; +import org.baeldung.custom.persistence.model.Organization; +import org.baeldung.custom.persistence.model.Privilege; +import org.baeldung.custom.persistence.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/OrganizationRepository.java similarity index 65% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/OrganizationRepository.java index a20d24057b..1319a7b9f8 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/OrganizationRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package org.baeldung.custom.persistence.dao; -import org.baeldung.persistence.model.Organization; +import org.baeldung.custom.persistence.model.Organization; import org.springframework.data.jpa.repository.JpaRepository; public interface OrganizationRepository extends JpaRepository { diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/PrivilegeRepository.java similarity index 65% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/PrivilegeRepository.java index edf9002c3d..c232bb986c 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/PrivilegeRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package org.baeldung.custom.persistence.dao; -import org.baeldung.persistence.model.Privilege; +import org.baeldung.custom.persistence.model.Privilege; import org.springframework.data.jpa.repository.JpaRepository; public interface PrivilegeRepository extends JpaRepository { diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/UserRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/UserRepository.java similarity index 76% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/UserRepository.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/UserRepository.java index 337106a187..68dd1d756c 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/UserRepository.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/dao/UserRepository.java @@ -1,6 +1,6 @@ -package org.baeldung.persistence.dao; +package org.baeldung.custom.persistence.dao; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.persistence.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.transaction.annotation.Transactional; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Foo.java similarity index 97% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Foo.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Foo.java index 29c19cf22e..f139382eea 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Foo.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package org.baeldung.custom.persistence.model; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Organization.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Organization.java similarity index 97% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Organization.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Organization.java index 645285b5e9..1fdb88e320 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Organization.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Organization.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package org.baeldung.custom.persistence.model; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Privilege.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Privilege.java similarity index 97% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Privilege.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Privilege.java index ff3ae62c25..ed3edd5085 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Privilege.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/Privilege.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package org.baeldung.custom.persistence.model; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/User.java similarity index 98% rename from spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/User.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/User.java index 2ff8beebf0..c14ef034b4 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/persistence/model/User.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package org.baeldung.custom.persistence.model; import java.util.Set; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomMethodSecurityExpressionHandler.java similarity index 97% rename from spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomMethodSecurityExpressionHandler.java index e040a0b109..646f5a387f 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomMethodSecurityExpressionHandler.java @@ -1,4 +1,4 @@ -package org.baeldung.security; +package org.baeldung.custom.security; import org.aopalliance.intercept.MethodInvocation; import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomMethodSecurityExpressionRoot.java similarity index 93% rename from spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomMethodSecurityExpressionRoot.java index 2d84536a14..b2f2be8cf5 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomMethodSecurityExpressionRoot.java @@ -1,6 +1,6 @@ -package org.baeldung.security; +package org.baeldung.custom.security; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.persistence.model.User; import org.springframework.security.access.expression.SecurityExpressionRoot; import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations; import org.springframework.security.core.Authentication; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomPermissionEvaluator.java similarity index 97% rename from spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomPermissionEvaluator.java index 5d96673a8f..f436b4488b 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/CustomPermissionEvaluator.java @@ -1,4 +1,4 @@ -package org.baeldung.security; +package org.baeldung.custom.security; import java.io.Serializable; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MySecurityExpressionRoot.java similarity index 98% rename from spring-security-mvc-boot/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MySecurityExpressionRoot.java index 4d3561b325..03d18cb755 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MySecurityExpressionRoot.java @@ -1,11 +1,11 @@ -package org.baeldung.security; +package org.baeldung.custom.security; import java.io.Serializable; import java.util.Collection; import java.util.HashSet; import java.util.Set; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.persistence.model.User; import org.springframework.security.access.PermissionEvaluator; import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations; import org.springframework.security.access.hierarchicalroles.RoleHierarchy; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MyUserDetailsService.java similarity index 84% rename from spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserDetailsService.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MyUserDetailsService.java index 685219728f..b9b40fbcb9 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserDetailsService.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MyUserDetailsService.java @@ -1,7 +1,7 @@ -package org.baeldung.security; +package org.baeldung.custom.security; -import org.baeldung.persistence.dao.UserRepository; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.persistence.dao.UserRepository; +import org.baeldung.custom.persistence.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserPrincipal.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MyUserPrincipal.java similarity index 90% rename from spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserPrincipal.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MyUserPrincipal.java index 437bb02cdb..7d57227316 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserPrincipal.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/security/MyUserPrincipal.java @@ -1,11 +1,11 @@ -package org.baeldung.security; +package org.baeldung.custom.security; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.baeldung.persistence.model.Privilege; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.persistence.model.Privilege; +import org.baeldung.custom.persistence.model.User; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/web/MainController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/web/MainController.java similarity index 86% rename from spring-security-mvc-boot/src/main/java/org/baeldung/web/MainController.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/custom/web/MainController.java index 4752f7bdd9..6572954427 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/web/MainController.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/custom/web/MainController.java @@ -1,8 +1,8 @@ -package org.baeldung.web; +package org.baeldung.custom.web; -import org.baeldung.persistence.dao.OrganizationRepository; -import org.baeldung.persistence.model.Foo; -import org.baeldung.persistence.model.Organization; +import org.baeldung.custom.persistence.dao.OrganizationRepository; +import org.baeldung.custom.persistence.model.Foo; +import org.baeldung.custom.persistence.model.Organization; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.security.access.prepost.PreAuthorize; @@ -52,7 +52,7 @@ public class MainController { @RequestMapping(method = RequestMethod.GET, value = "/organizations/{id}") @ResponseBody public Organization findOrgById(@PathVariable final long id) { - return organizationRepository.findOne(id); + return organizationRepository.findById(id).orElse(null); } } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthController.java index 80573c1c7d..b63169bb00 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthController.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthController.java @@ -1,12 +1,12 @@ package org.baeldung.multipleauthproviders; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MultipleAuthController { - @RequestMapping("/api/ping") + @GetMapping("/api/ping") public String getPing() { return "OK"; } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthProvidersSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthProvidersSecurityConfig.java index 432cc61e87..3819e981a2 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthProvidersSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multipleauthproviders/MultipleAuthProvidersSecurityConfig.java @@ -1,10 +1,13 @@ package org.baeldung.multipleauthproviders; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; 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.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; @EnableWebSecurity public class MultipleAuthProvidersSecurityConfig extends WebSecurityConfigurerAdapter { @@ -19,7 +22,7 @@ public class MultipleAuthProvidersSecurityConfig extends WebSecurityConfigurerAd auth.inMemoryAuthentication() .withUser("memuser") - .password("pass") + .password(passwordEncoder().encode("pass")) .roles("USER"); } @@ -31,4 +34,9 @@ public class MultipleAuthProvidersSecurityConfig extends WebSecurityConfigurerAd .antMatchers("/api/**") .authenticated(); } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multipleentrypoints/MultipleEntryPointsSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multipleentrypoints/MultipleEntryPointsSecurityConfig.java index 9f2eba0a2e..dc89c83cde 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multipleentrypoints/MultipleEntryPointsSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multipleentrypoints/MultipleEntryPointsSecurityConfig.java @@ -8,6 +8,8 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; @@ -21,10 +23,15 @@ public class MultipleEntryPointsSecurityConfig { @Bean public UserDetailsService userDetailsService() throws Exception { InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser(User.withUsername("user").password("userPass").roles("USER").build()); - manager.createUser(User.withUsername("admin").password("adminPass").roles("ADMIN").build()); + manager.createUser(User.withUsername("user").password(encoder().encode("userPass")).roles("USER").build()); + manager.createUser(User.withUsername("admin").password(encoder().encode("adminPass")).roles("ADMIN").build()); return manager; } + + @Bean + public PasswordEncoder encoder() { + return new BCryptPasswordEncoder(); + } @Configuration @Order(1) diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java index ec76be1909..204b186411 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java @@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @@ -13,7 +14,7 @@ import org.springframework.context.annotation.ComponentScan; @EnableWebMvc @Configuration @ComponentScan("org.baeldung.controller") -public class MultipleLoginMvcConfig extends WebMvcConfigurerAdapter { +public class MultipleLoginMvcConfig implements WebMvcConfigurer { public MultipleLoginMvcConfig() { super(); @@ -23,8 +24,6 @@ public class MultipleLoginMvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - registry.addViewController("/anonymous.html"); registry.addViewController("/login.html"); diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java index 8327e7e5d3..9962bf41a9 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java @@ -1,17 +1,16 @@ package org.baeldung.multiplelogin; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; -import org.springframework.security.authentication.AuthenticationProvider; -import org.springframework.security.authentication.TestingAuthenticationProvider; 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.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @Configuration @@ -21,10 +20,15 @@ public class MultipleLoginSecurityConfig { @Bean public UserDetailsService userDetailsService() throws Exception { InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser(User.withUsername("user").password("userPass").roles("USER").build()); - manager.createUser(User.withUsername("admin").password("adminPass").roles("ADMIN").build()); + manager.createUser(User.withUsername("user").password(encoder().encode("userPass")).roles("USER").build()); + manager.createUser(User.withUsername("admin").password(encoder().encode("adminPass")).roles("ADMIN").build()); return manager; } + + @Bean + public static PasswordEncoder encoder() { + return new BCryptPasswordEncoder(); + } @Configuration @Order(1) @@ -36,7 +40,7 @@ public class MultipleLoginSecurityConfig { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN"); + auth.inMemoryAuthentication().withUser("admin").password(encoder().encode("admin")).roles("ADMIN"); } @Override @@ -59,7 +63,7 @@ public class MultipleLoginSecurityConfig { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("user").password("user").roles("USER"); + auth.inMemoryAuthentication().withUser("user").password(encoder().encode("user")).roles("USER"); } protected void configure(HttpSecurity http) throws Exception { diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java index 0672a760af..61d7da127c 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java @@ -1,37 +1,37 @@ package org.baeldung.multiplelogin; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.GetMapping; @Controller public class UsersController { - @RequestMapping("/protectedLinks") + @GetMapping("/protectedLinks") public String getAnonymousPage() { return "protectedLinks"; } - @RequestMapping("/userPage") + @GetMapping("/userPage") public String getUserPage() { return "userPage"; } - @RequestMapping("/adminPage") + @GetMapping("/adminPage") public String getAdminPage() { return "adminPage"; } - @RequestMapping("/loginAdmin") + @GetMapping("/loginAdmin") public String getAdminLoginPage() { return "loginAdmin"; } - @RequestMapping("/loginUser") + @GetMapping("/loginUser") public String getUserLoginPage() { return "loginUser"; } - @RequestMapping("/403") + @GetMapping("/403") public String getAccessDeniedPage() { return "403"; } diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/RolesAuthoritiesApplication.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/RolesAuthoritiesApplication.java index 1c55c145b3..3c4e6f7b5a 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/RolesAuthoritiesApplication.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/RolesAuthoritiesApplication.java @@ -2,7 +2,7 @@ package org.baeldung.rolesauthorities; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/MvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/MvcConfig.java index d2edfed749..c42958457e 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/MvcConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/config/MvcConfig.java @@ -7,11 +7,11 @@ import org.springframework.web.servlet.config.annotation.DefaultServletHandlerCo import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc -public class MvcConfig extends WebMvcConfigurerAdapter { +public class MvcConfig implements WebMvcConfigurer { public MvcConfig() { super(); @@ -30,7 +30,6 @@ public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); registry.addViewController("/").setViewName("forward:/home"); registry.addViewController("/protectedbynothing").setViewName("rolesauthorities/protectedbynothing"); registry.addViewController("/protectedbyrole").setViewName("rolesauthorities/protectedbyrole"); diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java deleted file mode 100644 index a993e41a83..0000000000 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/rolesauthorities/persistence/PersistenceJPAConfig.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.baeldung.rolesauthorities.persistence; - -import java.util.Properties; - -import javax.sql.DataSource; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.core.env.Environment; -import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.jdbc.datasource.DriverManagerDataSource; -import org.springframework.orm.jpa.JpaTransactionManager; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@Configuration -@EnableTransactionManagement -@PropertySource({ "classpath:persistence.properties" }) -@ComponentScan({ "org.baeldung.rolesauthorities.persistence" }) -@EnableJpaRepositories(basePackages = "org.baeldung.rolesauthorities.persistence") -public class PersistenceJPAConfig { - - @Autowired - private Environment env; - - public PersistenceJPAConfig() { - super(); - } - - // - - @Bean - public LocalContainerEntityManagerFactoryBean entityManagerFactory() { - LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); - em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.rolesauthorities" }); - HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); - em.setJpaVendorAdapter(vendorAdapter); - em.setJpaProperties(additionalProperties()); - return em; - } - - @Bean - public DataSource dataSource() { - DriverManagerDataSource dataSource = new DriverManagerDataSource(); - dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); - dataSource.setUrl(env.getProperty("jdbc.url")); - dataSource.setUsername(env.getProperty("jdbc.user")); - dataSource.setPassword(env.getProperty("jdbc.pass")); - return dataSource; - } - - @Bean - public JpaTransactionManager transactionManager() { - JpaTransactionManager transactionManager = new JpaTransactionManager(); - transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); - return transactionManager; - } - - @Bean - public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { - return new PersistenceExceptionTranslationPostProcessor(); - } - - protected Properties additionalProperties() { - Properties hibernateProperties = new Properties(); - hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); - hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); - return hibernateProperties; - } - -} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterMvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterMvcConfig.java index e282c794d0..8f41153f06 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterMvcConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterMvcConfig.java @@ -3,6 +3,7 @@ package org.baeldung.voter; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** @@ -10,7 +11,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter */ @Configuration -public class VoterMvcConfig extends WebMvcConfigurerAdapter { +public class VoterMvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("private"); diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java index 06fb4d2316..84ed070e8e 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java @@ -12,6 +12,8 @@ import org.springframework.security.config.annotation.authentication.builders.Au 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.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.access.expression.WebExpressionVoter; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @@ -24,7 +26,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { // @formatter: off - auth.inMemoryAuthentication().withUser("user").password("pass").roles("USER").and().withUser("admin").password("pass").roles("ADMIN"); + auth.inMemoryAuthentication().withUser("user").password(passwordEncoder().encode("pass")).roles("USER").and().withUser("admin").password(passwordEncoder().encode("pass")).roles("ADMIN"); // @formatter: on } @@ -45,4 +47,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { // @formatter: on return new UnanimousBased(decisionVoters); } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } } diff --git a/spring-security-mvc-boot/src/main/resources/acl-data.sql b/spring-security-mvc-boot/src/main/resources/acl-data.sql deleted file mode 100644 index 6c01eaacc2..0000000000 --- a/spring-security-mvc-boot/src/main/resources/acl-data.sql +++ /dev/null @@ -1,28 +0,0 @@ -INSERT INTO system_message(id,content) VALUES (1,'First Level Message'); -INSERT INTO system_message(id,content) VALUES (2,'Second Level Message'); -INSERT INTO system_message(id,content) VALUES (3,'Third Level Message'); - -INSERT INTO acl_class (id, class) VALUES -(1, 'org.baeldung.acl.persistence.entity.NoticeMessage'); - -INSERT INTO acl_sid (id, principal, sid) VALUES -(1, 1, 'manager'), -(2, 1, 'hr'), -(3, 1, 'admin'), -(4, 0, 'ROLE_EDITOR'); - -INSERT INTO acl_object_identity (id, object_id_class, object_id_identity, parent_object, owner_sid, entries_inheriting) VALUES -(1, 1, 1, NULL, 3, 0), -(2, 1, 2, NULL, 3, 0), -(3, 1, 3, NULL, 3, 0) -; - -INSERT INTO acl_entry (id, acl_object_identity, ace_order, sid, mask, granting, audit_success, audit_failure) VALUES -(1, 1, 1, 1, 1, 1, 1, 1), -(2, 1, 2, 1, 2, 1, 1, 1), -(3, 1, 3, 4, 1, 1, 1, 1), -(4, 2, 1, 2, 1, 1, 1, 1), -(5, 2, 2, 4, 1, 1, 1, 1), -(6, 3, 1, 4, 1, 1, 1, 1), -(7, 3, 2, 4, 2, 1, 1, 1) -; \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/acl-schema.sql b/spring-security-mvc-boot/src/main/resources/acl-schema.sql deleted file mode 100644 index 58e9394b2b..0000000000 --- a/spring-security-mvc-boot/src/main/resources/acl-schema.sql +++ /dev/null @@ -1,58 +0,0 @@ -create table system_message (id integer not null, content varchar(255), primary key (id)); - -CREATE TABLE IF NOT EXISTS acl_sid ( - id bigint(20) NOT NULL AUTO_INCREMENT, - principal tinyint(1) NOT NULL, - sid varchar(100) NOT NULL, - PRIMARY KEY (id), - UNIQUE KEY unique_uk_1 (sid,principal) -); - -CREATE TABLE IF NOT EXISTS acl_class ( - id bigint(20) NOT NULL AUTO_INCREMENT, - class varchar(255) NOT NULL, - PRIMARY KEY (id), - UNIQUE KEY unique_uk_2 (class) -); - -CREATE TABLE IF NOT EXISTS acl_entry ( - id bigint(20) NOT NULL AUTO_INCREMENT, - acl_object_identity bigint(20) NOT NULL, - ace_order int(11) NOT NULL, - sid bigint(20) NOT NULL, - mask int(11) NOT NULL, - granting tinyint(1) NOT NULL, - audit_success tinyint(1) NOT NULL, - audit_failure tinyint(1) NOT NULL, - PRIMARY KEY (id), - UNIQUE KEY unique_uk_4 (acl_object_identity,ace_order) -); - -CREATE TABLE IF NOT EXISTS acl_object_identity ( - id bigint(20) NOT NULL AUTO_INCREMENT, - object_id_class bigint(20) NOT NULL, - object_id_identity bigint(20) NOT NULL, - parent_object bigint(20) DEFAULT NULL, - owner_sid bigint(20) DEFAULT NULL, - entries_inheriting tinyint(1) NOT NULL, - PRIMARY KEY (id), - UNIQUE KEY unique_uk_3 (object_id_class,object_id_identity) -); - -ALTER TABLE acl_entry -ADD FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity(id); - -ALTER TABLE acl_entry -ADD FOREIGN KEY (sid) REFERENCES acl_sid(id); - --- --- Constraints for table acl_object_identity --- -ALTER TABLE acl_object_identity -ADD FOREIGN KEY (parent_object) REFERENCES acl_object_identity (id); - -ALTER TABLE acl_object_identity -ADD FOREIGN KEY (object_id_class) REFERENCES acl_class (id); - -ALTER TABLE acl_object_identity -ADD FOREIGN KEY (owner_sid) REFERENCES acl_sid (id); \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/application-rolesauthorities.properties b/spring-security-mvc-boot/src/main/resources/application-rolesauthorities.properties deleted file mode 100644 index 030c79c542..0000000000 --- a/spring-security-mvc-boot/src/main/resources/application-rolesauthorities.properties +++ /dev/null @@ -1,10 +0,0 @@ -server.port=8082 -server.context-path=/ -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.url=jdbc:h2:mem:security_permission;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE -spring.datasource.username=sa -spring.datasource.password= -spring.jpa.hibernate.ddl-auto=create-drop -spring.jpa.database=H2 -spring.jpa.show-sql=false -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/application.properties b/spring-security-mvc-boot/src/main/resources/application.properties index d29b5f6bf1..f015086a4f 100644 --- a/spring-security-mvc-boot/src/main/resources/application.properties +++ b/spring-security-mvc-boot/src/main/resources/application.properties @@ -1,5 +1,4 @@ server.port=8082 -server.context-path=/spring-security-mvc-boot spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:security_permission;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=sa diff --git a/spring-security-mvc-boot/src/main/resources/org.baeldung.acl.datasource.properties b/spring-security-mvc-boot/src/main/resources/org.baeldung.acl.datasource.properties deleted file mode 100644 index 739dd3f07c..0000000000 --- a/spring-security-mvc-boot/src/main/resources/org.baeldung.acl.datasource.properties +++ /dev/null @@ -1,12 +0,0 @@ -spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driverClassName=org.h2.Driver -spring.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect - -spring.h2.console.path=/myconsole -spring.h2.console.enabled=true -spring.datasource.initialize=true -spring.datasource.schema=classpath:acl-schema.sql -spring.datasource.data=classpath:acl-data.sql \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/persistence-derby.properties b/spring-security-mvc-boot/src/main/resources/persistence-derby.properties deleted file mode 100644 index e808fdc288..0000000000 --- a/spring-security-mvc-boot/src/main/resources/persistence-derby.properties +++ /dev/null @@ -1,12 +0,0 @@ -# jdbc.X -jdbc.driverClassName=org.apache.derby.jdbc.EmbeddedDriver -jdbc.url=jdbc:derby:memory:spring_custom_user_service;create=true -jdbc.user=tutorialuser -jdbc.pass=tutorialpass - -# hibernate.X -hibernate.dialect=org.hibernate.dialect.DerbyDialect -hibernate.show_sql=false -hibernate.hbm2ddl.auto=create -hibernate.cache.use_second_level_cache=false -hibernate.cache.use_query_cache=false \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/persistence.properties b/spring-security-mvc-boot/src/main/resources/persistence.properties deleted file mode 100644 index b2255cd479..0000000000 --- a/spring-security-mvc-boot/src/main/resources/persistence.properties +++ /dev/null @@ -1,11 +0,0 @@ -####### H2 -#################### DataSource Configuration ########################## -jdbc.driverClassName=org.h2.Driver -jdbc.url=jdbc:h2:mem:registration_02;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE -jdbc.user=sa -jdbc.pass= -init-db=false -#################### Hibernate Configuration ########################## -hibernate.dialect=org.hibernate.dialect.H2Dialect -hibernate.show_sql=false -hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java index cf1ac7de89..f01cac1c87 100644 --- a/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -1,5 +1,6 @@ package org.baeldung; +import org.baeldung.custom.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/acl/SpringAclIntegrationTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/acl/SpringAclIntegrationTest.java deleted file mode 100644 index 38e1a2a9e7..0000000000 --- a/spring-security-mvc-boot/src/test/java/org/baeldung/acl/SpringAclIntegrationTest.java +++ /dev/null @@ -1,119 +0,0 @@ -package org.baeldung.acl; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.List; - -import org.baeldung.acl.persistence.dao.NoticeMessageRepository; -import org.baeldung.acl.persistence.entity.NoticeMessage; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.TestExecutionListeners; -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; -import org.springframework.test.context.support.DirtiesContextTestExecutionListener; -import org.springframework.test.context.transaction.TransactionalTestExecutionListener; -import org.springframework.test.context.web.ServletTestExecutionListener; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -@TestExecutionListeners(listeners={ServletTestExecutionListener.class, - DependencyInjectionTestExecutionListener.class, - DirtiesContextTestExecutionListener.class, - TransactionalTestExecutionListener.class, - WithSecurityContextTestExecutionListener.class}) -public class SpringAclIntegrationTest extends AbstractJUnit4SpringContextTests{ - - private static Integer FIRST_MESSAGE_ID = 1; - private static Integer SECOND_MESSAGE_ID = 2; - private static Integer THIRD_MESSAGE_ID = 3; - private static String EDITTED_CONTENT = "EDITED"; - - @Configuration - @ComponentScan("org.baeldung.acl.*") - public static class SpringConfig { - - } - - @Autowired - NoticeMessageRepository repo; - - @Test - @WithMockUser(username="manager") - public void givenUsernameManager_whenFindAllMessage_thenReturnFirstMessage(){ - List details = repo.findAll(); - assertNotNull(details); - assertEquals(1,details.size()); - assertEquals(FIRST_MESSAGE_ID,details.get(0).getId()); - } - - @Test - @WithMockUser(username="manager") - public void givenUsernameManager_whenFindFirstMessageByIdAndUpdateFirstMessageContent_thenOK(){ - NoticeMessage firstMessage = repo.findById(FIRST_MESSAGE_ID); - assertNotNull(firstMessage); - assertEquals(FIRST_MESSAGE_ID,firstMessage.getId()); - - firstMessage.setContent(EDITTED_CONTENT); - repo.save(firstMessage); - - NoticeMessage editedFirstMessage = repo.findById(FIRST_MESSAGE_ID); - assertNotNull(editedFirstMessage); - assertEquals(FIRST_MESSAGE_ID,editedFirstMessage.getId()); - assertEquals(EDITTED_CONTENT,editedFirstMessage.getContent()); - } - - @Test - @WithMockUser(username="hr") - public void givenUsernameHr_whenFindMessageById2_thenOK(){ - NoticeMessage secondMessage = repo.findById(SECOND_MESSAGE_ID); - assertNotNull(secondMessage); - assertEquals(SECOND_MESSAGE_ID,secondMessage.getId()); - } - - @Test(expected=AccessDeniedException.class) - @WithMockUser(username="hr") - public void givenUsernameHr_whenUpdateMessageWithId2_thenFail(){ - NoticeMessage secondMessage = new NoticeMessage(); - secondMessage.setId(SECOND_MESSAGE_ID); - secondMessage.setContent(EDITTED_CONTENT); - repo.save(secondMessage); - } - - @Test - @WithMockUser(roles={"EDITOR"}) - public void givenRoleEditor_whenFindAllMessage_thenReturnThreeMessage(){ - List details = repo.findAll(); - assertNotNull(details); - assertEquals(3,details.size()); - } - - @Test - @WithMockUser(roles={"EDITOR"}) - public void givenRoleEditor_whenUpdateThirdMessage_thenOK(){ - NoticeMessage thirdMessage = new NoticeMessage(); - thirdMessage.setId(THIRD_MESSAGE_ID); - thirdMessage.setContent(EDITTED_CONTENT); - repo.save(thirdMessage); - } - - @Test(expected=AccessDeniedException.class) - @WithMockUser(roles={"EDITOR"}) - public void givenRoleEditor_whenFindFirstMessageByIdAndUpdateFirstMessageContent_thenFail(){ - NoticeMessage firstMessage = repo.findById(FIRST_MESSAGE_ID); - assertNotNull(firstMessage); - assertEquals(FIRST_MESSAGE_ID,firstMessage.getId()); - firstMessage.setContent(EDITTED_CONTENT); - repo.save(firstMessage); - } -} - \ No newline at end of file diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/web/ApplicationLiveTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/ApplicationLiveTest.java index 371e53070d..dfcfcad609 100644 --- a/spring-security-mvc-boot/src/test/java/org/baeldung/web/ApplicationLiveTest.java +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/web/ApplicationLiveTest.java @@ -2,12 +2,14 @@ package org.baeldung.web; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; + +import org.baeldung.custom.persistence.model.Foo; + import io.restassured.RestAssured; import io.restassured.authentication.FormAuthConfig; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; -import org.baeldung.persistence.model.Foo; import org.junit.Test; import org.springframework.http.MediaType; @@ -16,20 +18,20 @@ public class ApplicationLiveTest { @Test public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/foos/1"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/foos/1"); assertEquals(200, response.getStatusCode()); assertTrue(response.asString().contains("id")); } @Test public void givenUserWithNoWritePrivilegeAndHasPermission_whenPostFoo_thenForbidden() { - final Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-mvc-boot/foos"); + final Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/foos"); assertEquals(403, response.getStatusCode()); } @Test public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() { - final Response response = givenAuth("tom", "111").and().body(new Foo("sample")).and().contentType(MediaType.APPLICATION_JSON_VALUE).post("http://localhost:8082/spring-security-mvc-boot/foos"); + final Response response = givenAuth("tom", "111").and().body(new Foo("sample")).and().contentType(MediaType.APPLICATION_JSON_VALUE).post("http://localhost:8082/foos"); assertEquals(201, response.getStatusCode()); assertTrue(response.asString().contains("id")); } @@ -38,14 +40,14 @@ public class ApplicationLiveTest { @Test public void givenUserMemberInOrganization_whenGetOrganization_thenOK() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/organizations/1"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/organizations/1"); assertEquals(200, response.getStatusCode()); assertTrue(response.asString().contains("id")); } @Test public void givenUserMemberNotInOrganization_whenGetOrganization_thenForbidden() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/organizations/2"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/organizations/2"); assertEquals(403, response.getStatusCode()); } @@ -53,13 +55,13 @@ public class ApplicationLiveTest { @Test public void givenDisabledSecurityExpression_whenGetFooByName_thenError() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/foos?name=sample"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/foos?name=sample"); assertEquals(500, response.getStatusCode()); assertTrue(response.asString().contains("method hasAuthority() not allowed")); } // private RequestSpecification givenAuth(String username, String password) { - return RestAssured.given().log().uri().auth().form(username, password, new FormAuthConfig("/spring-security-mvc-boot/login","username","password")); + return RestAssured.given().log().uri().auth().form(username, password, new FormAuthConfig("/login","username","password")); } } \ No newline at end of file diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java index 503354256f..fb25175902 100644 --- a/spring-security-mvc-boot/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java @@ -2,11 +2,11 @@ package org.baeldung.web; import static org.junit.Assert.assertEquals; -import org.baeldung.config.MvcConfig; -import org.baeldung.config.PersistenceConfig; -import org.baeldung.config.SecurityConfig; -import org.baeldung.persistence.dao.UserRepository; -import org.baeldung.persistence.model.User; +import org.baeldung.custom.Application; +import org.baeldung.custom.config.MvcConfig; +import org.baeldung.custom.config.SecurityConfig; +import org.baeldung.custom.persistence.dao.UserRepository; +import org.baeldung.custom.persistence.model.User; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; @@ -21,7 +21,7 @@ import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.boot.test.context.SpringBootTest; @RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = {MvcConfig.class, SecurityConfig.class, PersistenceConfig.class}) +@SpringBootTest(classes = {Application.class}) @WebAppConfiguration public class CustomUserDetailsServiceIntegrationTest {