From 491a4b1f95e1ea117c22fc07b47a678a689fec4f Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 4 Sep 2016 19:18:58 +0300 Subject: [PATCH 01/13] BAEL-10 custom model, dao, service, userdetails service, controller and views for register and login, configurations --- ....eclipse.wst.common.project.facet.core.xml | 2 +- spring-jpa/.springBeans | 16 ++++ spring-jpa/pom.xml | 81 +++++++++++++++++ .../org/baeldung/custom/config/MvcConfig.java | 42 +++++++++ .../config/PersistenceDerbyJPAConfig.java | 87 +++++++++++++++++++ .../custom/config/SecSecurityConfig.java | 75 ++++++++++++++++ .../baeldung/persistence/model/MyUser.java | 76 ++++++++++++++++ .../multiple/dao/user/MyUserDAO.java | 40 +++++++++ .../persistence/service/MyUserService.java | 48 ++++++++++ .../security/MyUserDetailsService.java | 36 ++++++++ .../org/baeldung/security/UserController.java | 52 +++++++++++ .../main/java/org/baeldung/web/MyUserDto.java | 41 +++++++++ .../resources/persistence-derby.properties | 10 +++ .../webapp/WEB-INF/mvc-dispatcher-servlet.xml | 86 ++++++++++++++++++ .../src/main/webapp/WEB-INF/views/index.jsp | 35 ++++++++ .../src/main/webapp/WEB-INF/views/login.jsp | 29 +++++++ .../main/webapp/WEB-INF/views/register.jsp | 24 +++++ spring-jpa/src/main/webapp/WEB-INF/web.xml | 50 +++++++++++ 18 files changed, 829 insertions(+), 1 deletion(-) create mode 100644 spring-jpa/.springBeans create mode 100644 spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java create mode 100644 spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java create mode 100644 spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java create mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java create mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java create mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java create mode 100644 spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java create mode 100644 spring-jpa/src/main/java/org/baeldung/security/UserController.java create mode 100644 spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java create mode 100644 spring-jpa/src/main/resources/persistence-derby.properties create mode 100644 spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml create mode 100644 spring-jpa/src/main/webapp/WEB-INF/views/index.jsp create mode 100644 spring-jpa/src/main/webapp/WEB-INF/views/login.jsp create mode 100644 spring-jpa/src/main/webapp/WEB-INF/views/register.jsp create mode 100644 spring-jpa/src/main/webapp/WEB-INF/web.xml diff --git a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml index b1c99d7726..a8a65de481 100644 --- a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,7 +1,7 @@ - + diff --git a/spring-jpa/.springBeans b/spring-jpa/.springBeans new file mode 100644 index 0000000000..85bcd37cff --- /dev/null +++ b/spring-jpa/.springBeans @@ -0,0 +1,16 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml + + + + + + diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index 25dd960435..531cfe3468 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -6,6 +6,7 @@ 0.1-SNAPSHOT spring-jpa + war @@ -115,6 +116,78 @@ test + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + javax.servlet + servlet-api + 3.0-alpha-1 + + + org.springframework.security + spring-security-core + ${org.springframework.security.version} + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + + org.apache.derby + derby + 10.12.1.1 + + + org.apache.derby + derbyclient + 10.12.1.1 + + + org.apache.derby + derbynet + 10.12.1.1 + + + org.apache.derby + derbytools + 10.12.1.1 + + + taglibs + standard + 1.1.2 + + + org.springframework.security + spring-security-taglibs + 4.1.3.RELEASE + + + javax.servlet.jsp.jstl + jstl-api + 1.2 + @@ -138,6 +211,12 @@ + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + org.apache.maven.plugins maven-surefire-plugin @@ -180,6 +259,7 @@ 4.2.5.RELEASE + 4.0.4.RELEASE 3.20.0-GA @@ -210,6 +290,7 @@ 3.5.1 + 2.6 2.19.1 2.7 1.4.18 diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java new file mode 100644 index 0000000000..4a9e737a92 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java @@ -0,0 +1,42 @@ +package org.baeldung.custom.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +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.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@EnableWebMvc +@Configuration +@ComponentScan(basePackages = { "org.baeldung.security" }) +public class MvcConfig extends WebMvcConfigurerAdapter { + + public MvcConfig() { + super(); + } + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + + registry.addViewController("/"); + registry.addViewController("/index"); + registry.addViewController("/login"); + registry.addViewController("/register"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } +} diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java new file mode 100644 index 0000000000..19bf952e70 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java @@ -0,0 +1,87 @@ +package org.baeldung.custom.config; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.baeldung.persistence.multiple.dao.user.MyUserDAO; +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.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; + +import com.google.common.base.Preconditions; + +@Configuration +@EnableTransactionManagement +@PropertySource({ "classpath:persistence-derby.properties" }) +public class PersistenceDerbyJPAConfig { + + @Autowired + private Environment env; + + public PersistenceDerbyJPAConfig() { + super(); + } + + // beans + + @Bean + public LocalContainerEntityManagerFactoryBean myEmf() { + 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(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); + dataSource.setPassword(Preconditions.checkNotNull(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.globally_quoted_identifiers", "true"); + return hibernateProperties; + } + + @Bean + public MyUserDAO myUserDAO() { + final MyUserDAO myUserDAO = new MyUserDAO(); + return myUserDAO; + } +} \ No newline at end of file diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java new file mode 100644 index 0000000000..d20674844a --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java @@ -0,0 +1,75 @@ +package org.baeldung.custom.config; + +import org.baeldung.persistence.service.MyUserService; +import org.baeldung.security.MyUserDetailsService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +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; + +@Configuration +@EnableWebSecurity +@Profile("!https") +public class SecSecurityConfig extends WebSecurityConfigurerAdapter { + + public SecSecurityConfig() { + super(); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + // @formatter:off + auth.authenticationProvider(authenticationProvider()); + // @formatter:on + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + // @formatter:off + http + .csrf().disable() + .authorizeRequests() + .antMatchers("/*").permitAll() + .and() + .formLogin() + .loginPage("/login") + .loginProcessingUrl("/login") + .defaultSuccessUrl("/",true) + .failureUrl("/login?error=true") + .and() + .logout() + .logoutUrl("/logout") + .deleteCookies("JSESSIONID") + .logoutSuccessUrl("/"); + // @formatter:on + } + + @Bean + public DaoAuthenticationProvider authenticationProvider() { + final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); + authProvider.setUserDetailsService(myUserDetailsService()); + authProvider.setPasswordEncoder(encoder()); + return authProvider; + } + + @Bean + public PasswordEncoder encoder() { + return new BCryptPasswordEncoder(11); + } + + @Bean + public MyUserDetailsService myUserDetailsService() { + return new MyUserDetailsService(); + } + + @Bean + public MyUserService myUserService() { + return new MyUserService(); + } +} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java new file mode 100644 index 0000000000..0a097f2782 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java @@ -0,0 +1,76 @@ +package org.baeldung.persistence.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(schema = "spring_custom_user_service") +public class MyUser { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + private String name; + + @Column(unique = true, nullable = false) + private String username; + + @Column(nullable = false) + private String password; + + private boolean enabled; + + public MyUser() { + } + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getUsername() { + return username; + } + + public void setUsername(final String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(final boolean enabled) { + this.enabled = enabled; + } + + @Override + public String toString() { + return "MyUser [name=" + name + ", username=" + username + ", password=" + password + ", enabled=" + enabled + "]"; + } + +} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java b/spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java new file mode 100644 index 0000000000..1de8c61442 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java @@ -0,0 +1,40 @@ +package org.baeldung.persistence.multiple.dao.user; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import org.baeldung.persistence.model.MyUser; +import org.springframework.stereotype.Repository; + +@Repository +public class MyUserDAO { + + @PersistenceContext + private EntityManager entityManager; + + public MyUser findByUsername(final String username) { + final Query query = entityManager.createQuery("from MyUser where username=:username", MyUser.class); + query.setParameter("username", username); + final List result = query.getResultList(); + if (result != null && result.size() > 0) { + return result.get(0); + } else + return null; + } + + public MyUser save(final MyUser user) { + entityManager.persist(user); + return user; + } + + public EntityManager getEntityManager() { + return entityManager; + } + + public void setEntityManager(final EntityManager entityManager) { + this.entityManager = entityManager; + } +} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java b/spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java new file mode 100644 index 0000000000..a382e92664 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java @@ -0,0 +1,48 @@ +package org.baeldung.persistence.service; + +import javax.transaction.Transactional; + +import org.baeldung.persistence.model.MyUser; +import org.baeldung.persistence.multiple.dao.user.MyUserDAO; +import org.baeldung.web.MyUserDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +@Service +@Transactional +public class MyUserService { + + @Autowired + private PasswordEncoder passwordEncoder; + + @Autowired + MyUserDAO myUserDAO; + + public MyUser registerNewUserAccount(final MyUserDto accountDto) throws Exception { + if (usernameExists(accountDto.getUsername())) { + throw new Exception("There is an account with that username: " + accountDto.getUsername()); + } + final MyUser user = new MyUser(); + + user.setUsername(accountDto.getUsername()); + user.setPassword(passwordEncoder.encode(accountDto.getPassword())); + user.setName(accountDto.getName()); + user.setEnabled(true); + return myUserDAO.save(user); + } + + public MyUser getUserByUsername(final String username) { + final MyUser user = myUserDAO.findByUsername(username); + return user; + } + + private boolean usernameExists(final String username) { + final MyUser user = myUserDAO.findByUsername(username); + if (user != null) { + return true; + } + return false; + } + +} diff --git a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java new file mode 100644 index 0000000000..0299d7cdcf --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java @@ -0,0 +1,36 @@ +package org.baeldung.security; + +import java.util.ArrayList; +import java.util.Collection; + +import org.baeldung.persistence.model.MyUser; +import org.baeldung.persistence.multiple.dao.user.MyUserDAO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service("userDetailsService") +public class MyUserDetailsService implements UserDetailsService { + + @Autowired + MyUserDAO myUserDAO; + + @Override + public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { + final MyUser user = myUserDAO.findByUsername(username); + + if (user == null) { + throw new UsernameNotFoundException("No user found with username: " + username); + } + else { + final Collection authorities = new ArrayList<>(); + authorities.add(new SimpleGrantedAuthority("ROLE_USER")); + return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities); + } + } + +} diff --git a/spring-jpa/src/main/java/org/baeldung/security/UserController.java b/spring-jpa/src/main/java/org/baeldung/security/UserController.java new file mode 100644 index 0000000000..8664816a32 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/security/UserController.java @@ -0,0 +1,52 @@ +package org.baeldung.security; + +import javax.annotation.Resource; + +import org.baeldung.persistence.service.MyUserService; +import org.baeldung.web.MyUserDto; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class UserController { + + @Resource + MyUserService myUserService; + + @RequestMapping(value = "/register", method = RequestMethod.POST) + public String registerUserAccount(final MyUserDto accountDto, final Model model) { + final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + model.addAttribute("name", auth.getName()); + try { + myUserService.registerNewUserAccount(accountDto); + model.addAttribute("message", "Registration successful"); + return "index"; + } + catch(final Exception exc){ + model.addAttribute("message", "Registration failed"); + + return "index"; + } + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String getHomepage(final Model model) { + final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + model.addAttribute("name", auth.getName()); + return "index"; + } + + @RequestMapping(value = "/register", method = RequestMethod.GET) + public String getRegister() { + return "register"; + } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String getLogin() { + return "login"; + } +} diff --git a/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java b/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java new file mode 100644 index 0000000000..ec95ec43bf --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java @@ -0,0 +1,41 @@ +package org.baeldung.web; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +public class MyUserDto { + @NotNull + @Size(min = 1) + private String username; + + @NotNull + @Size(min = 1) + private String name; + + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(final String username) { + this.username = username; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } + +} diff --git a/spring-jpa/src/main/resources/persistence-derby.properties b/spring-jpa/src/main/resources/persistence-derby.properties new file mode 100644 index 0000000000..2e2dee983b --- /dev/null +++ b/spring-jpa/src/main/resources/persistence-derby.properties @@ -0,0 +1,10 @@ +# 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 \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml new file mode 100644 index 0000000000..2b33a1384a --- /dev/null +++ b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /WEB-INF/views/ + + + .jsp + + + + + + + + + + + + + + + ${hibernate.hbm2ddl.auto} + ${hibernate.dialect} + + + + + + + + + + + + + + + + + + + diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/index.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/index.jsp new file mode 100644 index 0000000000..0c89257cd2 --- /dev/null +++ b/spring-jpa/src/main/webapp/WEB-INF/views/index.jsp @@ -0,0 +1,35 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="c" + uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + + + + +Welcome! + + + + + + + +Register +

+ +Login + +

+${message } +

+ +Hello, ${name }! +
+
+Logout +
+ + + \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/login.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/login.jsp new file mode 100644 index 0000000000..29431f426d --- /dev/null +++ b/spring-jpa/src/main/webapp/WEB-INF/views/login.jsp @@ -0,0 +1,29 @@ +<%@ taglib prefix="c" + uri="http://java.sun.com/jsp/jstl/core" %> + + + + + +

Login

+ +
+ + + + + + + + + + + + + +
User:
Password:
+ +
+ Username or password invalid! + + \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp new file mode 100644 index 0000000000..68b3ac14b0 --- /dev/null +++ b/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp @@ -0,0 +1,24 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="c" + uri="http://java.sun.com/jsp/jstl/core" %> + + + + +Welcome! + + + + + +Register here:

+
+Username:
+Password:
+Name:

+ +
+ + + \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/web.xml b/spring-jpa/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..2eb6e88c31 --- /dev/null +++ b/spring-jpa/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,50 @@ + + + + Spring MVC Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.custom.config + + + + org.springframework.web.context.ContextLoaderListener + + + + + mvc-dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + mvc-dispatcher + / + + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + springSecurityFilterChain + /* + + + + index.jsp + + + \ No newline at end of file From ff3d29a8f30b44c9ff16e4b6e0fba96711412dea Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 4 Sep 2016 19:44:26 +0300 Subject: [PATCH 02/13] fix conflict with existing tests --- .../baeldung/custom/config/PersistenceDerbyJPAConfig.java | 2 +- .../java/org/baeldung/custom/config/SecSecurityConfig.java | 2 +- .../java/org/baeldung/security/MyUserDetailsService.java | 2 +- .../src/main/java/org/baeldung/security/UserController.java | 2 +- .../multiple/dao/user => user/dao}/MyUserDAO.java | 2 +- .../{persistence => user}/service/MyUserService.java | 4 ++-- .../src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml | 2 +- spring-jpa/src/main/webapp/WEB-INF/web.xml | 5 +++-- 8 files changed, 11 insertions(+), 10 deletions(-) rename spring-jpa/src/main/java/org/baeldung/{persistence/multiple/dao/user => user/dao}/MyUserDAO.java (95%) rename spring-jpa/src/main/java/org/baeldung/{persistence => user}/service/MyUserService.java (93%) diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java index 19bf952e70..812709111d 100644 --- a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java +++ b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java @@ -5,7 +5,7 @@ import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; -import org.baeldung.persistence.multiple.dao.user.MyUserDAO; +import org.baeldung.user.dao.MyUserDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java index d20674844a..44df02980f 100644 --- a/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java +++ b/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java @@ -1,7 +1,7 @@ package org.baeldung.custom.config; -import org.baeldung.persistence.service.MyUserService; import org.baeldung.security.MyUserDetailsService; +import org.baeldung.user.service.MyUserService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; diff --git a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java index 0299d7cdcf..21e3985bac 100644 --- a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java +++ b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.Collection; import org.baeldung.persistence.model.MyUser; -import org.baeldung.persistence.multiple.dao.user.MyUserDAO; +import org.baeldung.user.dao.MyUserDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; diff --git a/spring-jpa/src/main/java/org/baeldung/security/UserController.java b/spring-jpa/src/main/java/org/baeldung/security/UserController.java index 8664816a32..b1c96e72c0 100644 --- a/spring-jpa/src/main/java/org/baeldung/security/UserController.java +++ b/spring-jpa/src/main/java/org/baeldung/security/UserController.java @@ -2,7 +2,7 @@ package org.baeldung.security; import javax.annotation.Resource; -import org.baeldung.persistence.service.MyUserService; +import org.baeldung.user.service.MyUserService; import org.baeldung.web.MyUserDto; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java b/spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java similarity index 95% rename from spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java rename to spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java index 1de8c61442..5741d19bf2 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/multiple/dao/user/MyUserDAO.java +++ b/spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.multiple.dao.user; +package org.baeldung.user.dao; import java.util.List; diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java similarity index 93% rename from spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java rename to spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java index a382e92664..866ac88f29 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/service/MyUserService.java +++ b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java @@ -1,9 +1,9 @@ -package org.baeldung.persistence.service; +package org.baeldung.user.service; import javax.transaction.Transactional; import org.baeldung.persistence.model.MyUser; -import org.baeldung.persistence.multiple.dao.user.MyUserDAO; +import org.baeldung.user.dao.MyUserDAO; import org.baeldung.web.MyUserDto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; diff --git a/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml index 2b33a1384a..47082453b9 100644 --- a/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml +++ b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml @@ -40,7 +40,7 @@ - + diff --git a/spring-jpa/src/main/webapp/WEB-INF/web.xml b/spring-jpa/src/main/webapp/WEB-INF/web.xml index 2eb6e88c31..b526774179 100644 --- a/spring-jpa/src/main/webapp/WEB-INF/web.xml +++ b/spring-jpa/src/main/webapp/WEB-INF/web.xml @@ -6,7 +6,8 @@ Spring MVC Application - + + mvc-dispatcher From 6a25a24eaebb08e792d52c945ca9216216f7d95d Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 4 Sep 2016 20:44:10 +0300 Subject: [PATCH 03/13] fix issues after merge --- .../.settings/org.eclipse.wst.common.project.facet.core.xml | 5 +++-- spring-jpa/pom.xml | 2 +- .../baeldung/custom/config/PersistenceDerbyJPAConfig.java | 2 ++ .../main/java/org/baeldung/user/service/MyUserService.java | 3 +-- spring-jpa/src/main/resources/persistence-derby.properties | 4 +++- .../src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml | 4 +++- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml index a8a65de481..5f3c0f7702 100644 --- a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,7 +1,8 @@ - + - + + diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index 9d497774ca..71dd3df270 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -268,7 +268,7 @@ - 4.0.4.RELEASE + 4.1.3.RELEASE 4.3.2.RELEASE 3.20.0-GA diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java index 812709111d..6be7053b78 100644 --- a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java +++ b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java @@ -75,6 +75,8 @@ public class PersistenceDerbyJPAConfig { 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; } diff --git a/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java index 866ac88f29..edbb1651a3 100644 --- a/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java +++ b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java @@ -1,13 +1,12 @@ package org.baeldung.user.service; -import javax.transaction.Transactional; - import org.baeldung.persistence.model.MyUser; import org.baeldung.user.dao.MyUserDAO; import org.baeldung.web.MyUserDto; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @Transactional diff --git a/spring-jpa/src/main/resources/persistence-derby.properties b/spring-jpa/src/main/resources/persistence-derby.properties index 2e2dee983b..e808fdc288 100644 --- a/spring-jpa/src/main/resources/persistence-derby.properties +++ b/spring-jpa/src/main/resources/persistence-derby.properties @@ -7,4 +7,6 @@ jdbc.pass=tutorialpass # hibernate.X hibernate.dialect=org.hibernate.dialect.DerbyDialect hibernate.show_sql=false -hibernate.hbm2ddl.auto=create \ No newline at end of file +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-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml index 47082453b9..25d1d4d22f 100644 --- a/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml +++ b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml @@ -5,7 +5,7 @@ xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" - xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd + xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd @@ -65,6 +65,8 @@ ${hibernate.hbm2ddl.auto} ${hibernate.dialect} + ${hibernate.cache.use_second_level_cache} + ${hibernate.cache.use_query_cache} From 2da3ca4e7cf6a131bbbc47fa826b99970a983e08 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Tue, 13 Sep 2016 23:20:44 +0300 Subject: [PATCH 04/13] update MyUser and User constructor --- .../baeldung/persistence/model/MyUser.java | 26 ------------------- .../security/MyUserDetailsService.java | 3 ++- .../baeldung/user/service/MyUserService.java | 2 -- .../main/java/org/baeldung/web/MyUserDto.java | 14 +--------- .../main/webapp/WEB-INF/views/register.jsp | 1 - 5 files changed, 3 insertions(+), 43 deletions(-) diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java index 0a097f2782..804d391641 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java @@ -15,16 +15,12 @@ public class MyUser { @GeneratedValue(strategy = GenerationType.AUTO) private int id; - private String name; - @Column(unique = true, nullable = false) private String username; @Column(nullable = false) private String password; - private boolean enabled; - public MyUser() { } @@ -36,14 +32,6 @@ public class MyUser { this.id = id; } - public String getName() { - return name; - } - - public void setName(final String name) { - this.name = name; - } - public String getUsername() { return username; } @@ -59,18 +47,4 @@ public class MyUser { public void setPassword(final String password) { this.password = password; } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(final boolean enabled) { - this.enabled = enabled; - } - - @Override - public String toString() { - return "MyUser [name=" + name + ", username=" + username + ", password=" + password + ", enabled=" + enabled + "]"; - } - } diff --git a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java index 21e3985bac..4c02f53d20 100644 --- a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java +++ b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java @@ -8,6 +8,7 @@ import org.baeldung.user.dao.MyUserDAO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; @@ -29,7 +30,7 @@ public class MyUserDetailsService implements UserDetailsService { else { final Collection authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority("ROLE_USER")); - return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities); + return new User(user.getUsername(), user.getPassword(), authorities); } } diff --git a/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java index edbb1651a3..f4705f3193 100644 --- a/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java +++ b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java @@ -26,8 +26,6 @@ public class MyUserService { user.setUsername(accountDto.getUsername()); user.setPassword(passwordEncoder.encode(accountDto.getPassword())); - user.setName(accountDto.getName()); - user.setEnabled(true); return myUserDAO.save(user); } diff --git a/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java b/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java index ec95ec43bf..c572208913 100644 --- a/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java +++ b/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java @@ -8,10 +8,6 @@ public class MyUserDto { @Size(min = 1) private String username; - @NotNull - @Size(min = 1) - private String name; - private String password; public String getUsername() { @@ -21,15 +17,7 @@ public class MyUserDto { public void setUsername(final String username) { this.username = username; } - - public String getName() { - return name; - } - - public void setName(final String name) { - this.name = name; - } - + public String getPassword() { return password; } diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp index 68b3ac14b0..e6e9d373a0 100644 --- a/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp +++ b/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp @@ -16,7 +16,6 @@ Register here:

Username:
Password:
-Name:

From 678d99b6e5f33535b781b31612ac9f5cd2a7d493 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 14 Sep 2016 21:52:09 +0300 Subject: [PATCH 05/13] custom userservice new project --- spring-userservice/.springBeans | 15 + spring-userservice/pom.xml | 310 ++++++++++++++++++ .../org/baeldung/custom/config/MvcConfig.java | 42 +++ .../config/PersistenceDerbyJPAConfig.java | 89 +++++ .../custom/config/SecSecurityConfig.java | 75 +++++ .../baeldung/persistence/model/MyUser.java | 50 +++ .../security/MyUserDetailsService.java | 37 +++ .../org/baeldung/security/UserController.java | 52 +++ .../java/org/baeldung/user/dao/MyUserDAO.java | 40 +++ .../baeldung/user/service/MyUserService.java | 45 +++ .../main/java/org/baeldung/web/MyUserDto.java | 29 ++ .../resources/persistence-derby.properties | 12 + .../src/main/webapp/META-INF/MANIFEST.MF | 3 + .../webapp/WEB-INF/mvc-dispatcher-servlet.xml | 88 +++++ .../src/main/webapp/WEB-INF/views/index.jsp | 35 ++ .../src/main/webapp/WEB-INF/views/login.jsp | 29 ++ .../main/webapp/WEB-INF/views/register.jsp | 23 ++ .../src/main/webapp/WEB-INF/web.xml | 51 +++ 18 files changed, 1025 insertions(+) create mode 100644 spring-userservice/.springBeans create mode 100644 spring-userservice/pom.xml create mode 100644 spring-userservice/src/main/java/org/baeldung/custom/config/MvcConfig.java create mode 100644 spring-userservice/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java create mode 100644 spring-userservice/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java create mode 100644 spring-userservice/src/main/java/org/baeldung/persistence/model/MyUser.java create mode 100644 spring-userservice/src/main/java/org/baeldung/security/MyUserDetailsService.java create mode 100644 spring-userservice/src/main/java/org/baeldung/security/UserController.java create mode 100644 spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java create mode 100644 spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java create mode 100644 spring-userservice/src/main/java/org/baeldung/web/MyUserDto.java create mode 100644 spring-userservice/src/main/resources/persistence-derby.properties create mode 100644 spring-userservice/src/main/webapp/META-INF/MANIFEST.MF create mode 100644 spring-userservice/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml create mode 100644 spring-userservice/src/main/webapp/WEB-INF/views/index.jsp create mode 100644 spring-userservice/src/main/webapp/WEB-INF/views/login.jsp create mode 100644 spring-userservice/src/main/webapp/WEB-INF/views/register.jsp create mode 100644 spring-userservice/src/main/webapp/WEB-INF/web.xml diff --git a/spring-userservice/.springBeans b/spring-userservice/.springBeans new file mode 100644 index 0000000000..ff32b84d3b --- /dev/null +++ b/spring-userservice/.springBeans @@ -0,0 +1,15 @@ + + + 1 + + + + + + + + + + + + diff --git a/spring-userservice/pom.xml b/spring-userservice/pom.xml new file mode 100644 index 0000000000..5edaf15d9b --- /dev/null +++ b/spring-userservice/pom.xml @@ -0,0 +1,310 @@ + + 4.0.0 + spring-userservice + spring-userservice + 0.0.1-SNAPSHOT + war + + + + + + + org.springframework + spring-orm + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + + + + org.hibernate + hibernate-entitymanager + ${hibernate.version} + + + org.hibernate + hibernate-ehcache + ${hibernate.version} + + + xml-apis + xml-apis + 1.4.01 + + + org.javassist + javassist + ${javassist.version} + + + mysql + mysql-connector-java + ${mysql-connector-java.version} + runtime + + + org.springframework.data + spring-data-jpa + ${spring-data-jpa.version} + + + com.h2database + h2 + ${h2.version} + + + + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + javax.el + javax.el-api + 2.2.5 + + + + + + com.google.guava + guava + ${guava.version} + + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + test + + + + org.springframework + spring-test + ${org.springframework.version} + test + + + + junit + junit + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + javax.servlet + servlet-api + 3.0-alpha-1 + + + org.springframework.security + spring-security-core + ${org.springframework.security.version} + + + org.springframework.security + spring-security-web + ${org.springframework.security.version} + + + org.springframework.security + spring-security-config + ${org.springframework.security.version} + + + + org.apache.derby + derby + 10.12.1.1 + + + org.apache.derby + derbyclient + 10.12.1.1 + + + org.apache.derby + derbynet + 10.12.1.1 + + + org.apache.derby + derbytools + 10.12.1.1 + + + taglibs + standard + 1.1.2 + + + org.springframework.security + spring-security-taglibs + 4.1.3.RELEASE + + + javax.servlet.jsp.jstl + jstl-api + 1.2 + + + + + spring-userservice + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + + 8082 + + + + + + + + + + + + 4.1.3.RELEASE + 4.3.2.RELEASE + 3.20.0-GA + + + 5.2.2.Final + 5.1.38 + 1.10.2.RELEASE + 1.4.192 + + + 1.7.13 + 1.1.3 + + + 5.2.2.Final + + + 19.0 + 3.4 + + + 1.3 + 4.12 + 1.10.19 + + 4.4.1 + 4.5 + + 2.9.0 + + + 3.5.1 + 2.6 + 2.19.1 + 2.7 + 1.4.18 + + + + + + \ No newline at end of file diff --git a/spring-userservice/src/main/java/org/baeldung/custom/config/MvcConfig.java b/spring-userservice/src/main/java/org/baeldung/custom/config/MvcConfig.java new file mode 100644 index 0000000000..4a9e737a92 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/custom/config/MvcConfig.java @@ -0,0 +1,42 @@ +package org.baeldung.custom.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +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.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@EnableWebMvc +@Configuration +@ComponentScan(basePackages = { "org.baeldung.security" }) +public class MvcConfig extends WebMvcConfigurerAdapter { + + public MvcConfig() { + super(); + } + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + + registry.addViewController("/"); + registry.addViewController("/index"); + registry.addViewController("/login"); + registry.addViewController("/register"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } +} diff --git a/spring-userservice/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java b/spring-userservice/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java new file mode 100644 index 0000000000..6be7053b78 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java @@ -0,0 +1,89 @@ +package org.baeldung.custom.config; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.baeldung.user.dao.MyUserDAO; +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.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; + +import com.google.common.base.Preconditions; + +@Configuration +@EnableTransactionManagement +@PropertySource({ "classpath:persistence-derby.properties" }) +public class PersistenceDerbyJPAConfig { + + @Autowired + private Environment env; + + public PersistenceDerbyJPAConfig() { + super(); + } + + // beans + + @Bean + public LocalContainerEntityManagerFactoryBean myEmf() { + 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(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); + dataSource.setPassword(Preconditions.checkNotNull(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; + } + + @Bean + public MyUserDAO myUserDAO() { + final MyUserDAO myUserDAO = new MyUserDAO(); + return myUserDAO; + } +} \ No newline at end of file diff --git a/spring-userservice/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java b/spring-userservice/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java new file mode 100644 index 0000000000..44df02980f --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java @@ -0,0 +1,75 @@ +package org.baeldung.custom.config; + +import org.baeldung.security.MyUserDetailsService; +import org.baeldung.user.service.MyUserService; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.authentication.dao.DaoAuthenticationProvider; +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; + +@Configuration +@EnableWebSecurity +@Profile("!https") +public class SecSecurityConfig extends WebSecurityConfigurerAdapter { + + public SecSecurityConfig() { + super(); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + // @formatter:off + auth.authenticationProvider(authenticationProvider()); + // @formatter:on + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + // @formatter:off + http + .csrf().disable() + .authorizeRequests() + .antMatchers("/*").permitAll() + .and() + .formLogin() + .loginPage("/login") + .loginProcessingUrl("/login") + .defaultSuccessUrl("/",true) + .failureUrl("/login?error=true") + .and() + .logout() + .logoutUrl("/logout") + .deleteCookies("JSESSIONID") + .logoutSuccessUrl("/"); + // @formatter:on + } + + @Bean + public DaoAuthenticationProvider authenticationProvider() { + final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); + authProvider.setUserDetailsService(myUserDetailsService()); + authProvider.setPasswordEncoder(encoder()); + return authProvider; + } + + @Bean + public PasswordEncoder encoder() { + return new BCryptPasswordEncoder(11); + } + + @Bean + public MyUserDetailsService myUserDetailsService() { + return new MyUserDetailsService(); + } + + @Bean + public MyUserService myUserService() { + return new MyUserService(); + } +} diff --git a/spring-userservice/src/main/java/org/baeldung/persistence/model/MyUser.java b/spring-userservice/src/main/java/org/baeldung/persistence/model/MyUser.java new file mode 100644 index 0000000000..804d391641 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/persistence/model/MyUser.java @@ -0,0 +1,50 @@ +package org.baeldung.persistence.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(schema = "spring_custom_user_service") +public class MyUser { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private int id; + + @Column(unique = true, nullable = false) + private String username; + + @Column(nullable = false) + private String password; + + public MyUser() { + } + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(final String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } +} diff --git a/spring-userservice/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-userservice/src/main/java/org/baeldung/security/MyUserDetailsService.java new file mode 100644 index 0000000000..4c02f53d20 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/security/MyUserDetailsService.java @@ -0,0 +1,37 @@ +package org.baeldung.security; + +import java.util.ArrayList; +import java.util.Collection; + +import org.baeldung.persistence.model.MyUser; +import org.baeldung.user.dao.MyUserDAO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@Service("userDetailsService") +public class MyUserDetailsService implements UserDetailsService { + + @Autowired + MyUserDAO myUserDAO; + + @Override + public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { + final MyUser user = myUserDAO.findByUsername(username); + + if (user == null) { + throw new UsernameNotFoundException("No user found with username: " + username); + } + else { + final Collection authorities = new ArrayList<>(); + authorities.add(new SimpleGrantedAuthority("ROLE_USER")); + return new User(user.getUsername(), user.getPassword(), authorities); + } + } + +} diff --git a/spring-userservice/src/main/java/org/baeldung/security/UserController.java b/spring-userservice/src/main/java/org/baeldung/security/UserController.java new file mode 100644 index 0000000000..b1c96e72c0 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/security/UserController.java @@ -0,0 +1,52 @@ +package org.baeldung.security; + +import javax.annotation.Resource; + +import org.baeldung.user.service.MyUserService; +import org.baeldung.web.MyUserDto; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class UserController { + + @Resource + MyUserService myUserService; + + @RequestMapping(value = "/register", method = RequestMethod.POST) + public String registerUserAccount(final MyUserDto accountDto, final Model model) { + final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + model.addAttribute("name", auth.getName()); + try { + myUserService.registerNewUserAccount(accountDto); + model.addAttribute("message", "Registration successful"); + return "index"; + } + catch(final Exception exc){ + model.addAttribute("message", "Registration failed"); + + return "index"; + } + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String getHomepage(final Model model) { + final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + model.addAttribute("name", auth.getName()); + return "index"; + } + + @RequestMapping(value = "/register", method = RequestMethod.GET) + public String getRegister() { + return "register"; + } + + @RequestMapping(value = "/login", method = RequestMethod.GET) + public String getLogin() { + return "login"; + } +} diff --git a/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java b/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java new file mode 100644 index 0000000000..5741d19bf2 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java @@ -0,0 +1,40 @@ +package org.baeldung.user.dao; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +import org.baeldung.persistence.model.MyUser; +import org.springframework.stereotype.Repository; + +@Repository +public class MyUserDAO { + + @PersistenceContext + private EntityManager entityManager; + + public MyUser findByUsername(final String username) { + final Query query = entityManager.createQuery("from MyUser where username=:username", MyUser.class); + query.setParameter("username", username); + final List result = query.getResultList(); + if (result != null && result.size() > 0) { + return result.get(0); + } else + return null; + } + + public MyUser save(final MyUser user) { + entityManager.persist(user); + return user; + } + + public EntityManager getEntityManager() { + return entityManager; + } + + public void setEntityManager(final EntityManager entityManager) { + this.entityManager = entityManager; + } +} diff --git a/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java b/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java new file mode 100644 index 0000000000..f4705f3193 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java @@ -0,0 +1,45 @@ +package org.baeldung.user.service; + +import org.baeldung.persistence.model.MyUser; +import org.baeldung.user.dao.MyUserDAO; +import org.baeldung.web.MyUserDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional +public class MyUserService { + + @Autowired + private PasswordEncoder passwordEncoder; + + @Autowired + MyUserDAO myUserDAO; + + public MyUser registerNewUserAccount(final MyUserDto accountDto) throws Exception { + if (usernameExists(accountDto.getUsername())) { + throw new Exception("There is an account with that username: " + accountDto.getUsername()); + } + final MyUser user = new MyUser(); + + user.setUsername(accountDto.getUsername()); + user.setPassword(passwordEncoder.encode(accountDto.getPassword())); + return myUserDAO.save(user); + } + + public MyUser getUserByUsername(final String username) { + final MyUser user = myUserDAO.findByUsername(username); + return user; + } + + private boolean usernameExists(final String username) { + final MyUser user = myUserDAO.findByUsername(username); + if (user != null) { + return true; + } + return false; + } + +} diff --git a/spring-userservice/src/main/java/org/baeldung/web/MyUserDto.java b/spring-userservice/src/main/java/org/baeldung/web/MyUserDto.java new file mode 100644 index 0000000000..c572208913 --- /dev/null +++ b/spring-userservice/src/main/java/org/baeldung/web/MyUserDto.java @@ -0,0 +1,29 @@ +package org.baeldung.web; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +public class MyUserDto { + @NotNull + @Size(min = 1) + private String username; + + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(final String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } + +} diff --git a/spring-userservice/src/main/resources/persistence-derby.properties b/spring-userservice/src/main/resources/persistence-derby.properties new file mode 100644 index 0000000000..e808fdc288 --- /dev/null +++ b/spring-userservice/src/main/resources/persistence-derby.properties @@ -0,0 +1,12 @@ +# 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-userservice/src/main/webapp/META-INF/MANIFEST.MF b/spring-userservice/src/main/webapp/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..254272e1c0 --- /dev/null +++ b/spring-userservice/src/main/webapp/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/spring-userservice/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml b/spring-userservice/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml new file mode 100644 index 0000000000..25d1d4d22f --- /dev/null +++ b/spring-userservice/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + /WEB-INF/views/ + + + .jsp + + + + + + + + + + + + + + + ${hibernate.hbm2ddl.auto} + ${hibernate.dialect} + ${hibernate.cache.use_second_level_cache} + ${hibernate.cache.use_query_cache} + + + + + + + + + + + + + + + + + + + diff --git a/spring-userservice/src/main/webapp/WEB-INF/views/index.jsp b/spring-userservice/src/main/webapp/WEB-INF/views/index.jsp new file mode 100644 index 0000000000..0c89257cd2 --- /dev/null +++ b/spring-userservice/src/main/webapp/WEB-INF/views/index.jsp @@ -0,0 +1,35 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="c" + uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> + + + + + +Welcome! + + + + + + + +Register +

+ +Login + +

+${message } +

+ +Hello, ${name }! +
+
+Logout +
+ + + \ No newline at end of file diff --git a/spring-userservice/src/main/webapp/WEB-INF/views/login.jsp b/spring-userservice/src/main/webapp/WEB-INF/views/login.jsp new file mode 100644 index 0000000000..29431f426d --- /dev/null +++ b/spring-userservice/src/main/webapp/WEB-INF/views/login.jsp @@ -0,0 +1,29 @@ +<%@ taglib prefix="c" + uri="http://java.sun.com/jsp/jstl/core" %> + + + + + +

Login

+ +
+ + + + + + + + + + + + + +
User:
Password:
+ +
+ Username or password invalid! + + \ No newline at end of file diff --git a/spring-userservice/src/main/webapp/WEB-INF/views/register.jsp b/spring-userservice/src/main/webapp/WEB-INF/views/register.jsp new file mode 100644 index 0000000000..e6e9d373a0 --- /dev/null +++ b/spring-userservice/src/main/webapp/WEB-INF/views/register.jsp @@ -0,0 +1,23 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="c" + uri="http://java.sun.com/jsp/jstl/core" %> + + + + +Welcome! + + + + + +Register here:

+
+Username:
+Password:
+ +
+ + + \ No newline at end of file diff --git a/spring-userservice/src/main/webapp/WEB-INF/web.xml b/spring-userservice/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..b526774179 --- /dev/null +++ b/spring-userservice/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,51 @@ + + + + Spring MVC Application + + + + + + mvc-dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + mvc-dispatcher + / + + + + + springSecurityFilterChain + org.springframework.web.filter.DelegatingFilterProxy + + + springSecurityFilterChain + /* + + + + index.jsp + + + \ No newline at end of file From 4b7e5066d88a43307616b4e2eb01446af027a2da Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 14 Sep 2016 22:10:29 +0300 Subject: [PATCH 06/13] clean up spring-jpa --- ....eclipse.wst.common.project.facet.core.xml | 2 +- spring-jpa/.springBeans | 1 - spring-jpa/pom.xml | 81 ----------------- .../org/baeldung/custom/config/MvcConfig.java | 42 --------- .../config/PersistenceDerbyJPAConfig.java | 89 ------------------- .../custom/config/SecSecurityConfig.java | 75 ---------------- .../baeldung/persistence/model/MyUser.java | 50 ----------- .../security/MyUserDetailsService.java | 37 -------- .../org/baeldung/security/UserController.java | 52 ----------- .../java/org/baeldung/user/dao/MyUserDAO.java | 40 --------- .../baeldung/user/service/MyUserService.java | 45 ---------- .../main/java/org/baeldung/web/MyUserDto.java | 29 ------ .../resources/persistence-derby.properties | 12 --- .../webapp/WEB-INF/mvc-dispatcher-servlet.xml | 88 ------------------ .../src/main/webapp/WEB-INF/views/index.jsp | 35 -------- .../src/main/webapp/WEB-INF/views/login.jsp | 29 ------ .../main/webapp/WEB-INF/views/register.jsp | 23 ----- spring-jpa/src/main/webapp/WEB-INF/web.xml | 51 ----------- 18 files changed, 1 insertion(+), 780 deletions(-) delete mode 100644 spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/security/UserController.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java delete mode 100644 spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java delete mode 100644 spring-jpa/src/main/resources/persistence-derby.properties delete mode 100644 spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml delete mode 100644 spring-jpa/src/main/webapp/WEB-INF/views/index.jsp delete mode 100644 spring-jpa/src/main/webapp/WEB-INF/views/login.jsp delete mode 100644 spring-jpa/src/main/webapp/WEB-INF/views/register.jsp delete mode 100644 spring-jpa/src/main/webapp/WEB-INF/web.xml diff --git a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml index 5f3c0f7702..24342c1b93 100644 --- a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -2,7 +2,7 @@ - + diff --git a/spring-jpa/.springBeans b/spring-jpa/.springBeans index 85bcd37cff..ff32b84d3b 100644 --- a/spring-jpa/.springBeans +++ b/spring-jpa/.springBeans @@ -7,7 +7,6 @@ - src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index 71dd3df270..5acdae7765 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -6,7 +6,6 @@ 0.1-SNAPSHOT spring-jpa - war @@ -126,78 +125,6 @@ test - - org.springframework - spring-core - ${org.springframework.version} - - - org.springframework - spring-web - ${org.springframework.version} - - - org.springframework - spring-webmvc - ${org.springframework.version} - - - - javax.servlet - servlet-api - 3.0-alpha-1 - - - org.springframework.security - spring-security-core - ${org.springframework.security.version} - - - org.springframework.security - spring-security-web - ${org.springframework.security.version} - - - org.springframework.security - spring-security-config - ${org.springframework.security.version} - - - - org.apache.derby - derby - 10.12.1.1 - - - org.apache.derby - derbyclient - 10.12.1.1 - - - org.apache.derby - derbynet - 10.12.1.1 - - - org.apache.derby - derbytools - 10.12.1.1 - - - taglibs - standard - 1.1.2 - - - org.springframework.security - spring-security-taglibs - 4.1.3.RELEASE - - - javax.servlet.jsp.jstl - jstl-api - 1.2 - @@ -221,12 +148,6 @@
- - org.apache.maven.plugins - maven-war-plugin - ${maven-war-plugin.version} - - org.apache.maven.plugins maven-surefire-plugin @@ -268,7 +189,6 @@ - 4.1.3.RELEASE 4.3.2.RELEASE 3.20.0-GA @@ -301,7 +221,6 @@ 3.5.1 - 2.6 2.19.1 2.7 1.4.18 diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java deleted file mode 100644 index 4a9e737a92..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/custom/config/MvcConfig.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.baeldung.custom.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -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.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.view.InternalResourceViewResolver; -import org.springframework.web.servlet.view.JstlView; - -@EnableWebMvc -@Configuration -@ComponentScan(basePackages = { "org.baeldung.security" }) -public class MvcConfig extends WebMvcConfigurerAdapter { - - public MvcConfig() { - super(); - } - - @Override - public void addViewControllers(final ViewControllerRegistry registry) { - super.addViewControllers(registry); - - registry.addViewController("/"); - registry.addViewController("/index"); - registry.addViewController("/login"); - registry.addViewController("/register"); - } - - @Bean - public ViewResolver viewResolver() { - final InternalResourceViewResolver bean = new InternalResourceViewResolver(); - - bean.setViewClass(JstlView.class); - bean.setPrefix("/WEB-INF/view/"); - bean.setSuffix(".jsp"); - - return bean; - } -} diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java deleted file mode 100644 index 6be7053b78..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/custom/config/PersistenceDerbyJPAConfig.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.baeldung.custom.config; - -import java.util.Properties; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; - -import org.baeldung.user.dao.MyUserDAO; -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.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; - -import com.google.common.base.Preconditions; - -@Configuration -@EnableTransactionManagement -@PropertySource({ "classpath:persistence-derby.properties" }) -public class PersistenceDerbyJPAConfig { - - @Autowired - private Environment env; - - public PersistenceDerbyJPAConfig() { - super(); - } - - // beans - - @Bean - public LocalContainerEntityManagerFactoryBean myEmf() { - 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(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); - dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); - dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); - dataSource.setPassword(Preconditions.checkNotNull(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; - } - - @Bean - public MyUserDAO myUserDAO() { - final MyUserDAO myUserDAO = new MyUserDAO(); - return myUserDAO; - } -} \ No newline at end of file diff --git a/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java b/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java deleted file mode 100644 index 44df02980f..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/custom/config/SecSecurityConfig.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.baeldung.custom.config; - -import org.baeldung.security.MyUserDetailsService; -import org.baeldung.user.service.MyUserService; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.security.authentication.dao.DaoAuthenticationProvider; -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; - -@Configuration -@EnableWebSecurity -@Profile("!https") -public class SecSecurityConfig extends WebSecurityConfigurerAdapter { - - public SecSecurityConfig() { - super(); - } - - @Override - protected void configure(final AuthenticationManagerBuilder auth) throws Exception { - // @formatter:off - auth.authenticationProvider(authenticationProvider()); - // @formatter:on - } - - @Override - protected void configure(final HttpSecurity http) throws Exception { - // @formatter:off - http - .csrf().disable() - .authorizeRequests() - .antMatchers("/*").permitAll() - .and() - .formLogin() - .loginPage("/login") - .loginProcessingUrl("/login") - .defaultSuccessUrl("/",true) - .failureUrl("/login?error=true") - .and() - .logout() - .logoutUrl("/logout") - .deleteCookies("JSESSIONID") - .logoutSuccessUrl("/"); - // @formatter:on - } - - @Bean - public DaoAuthenticationProvider authenticationProvider() { - final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); - authProvider.setUserDetailsService(myUserDetailsService()); - authProvider.setPasswordEncoder(encoder()); - return authProvider; - } - - @Bean - public PasswordEncoder encoder() { - return new BCryptPasswordEncoder(11); - } - - @Bean - public MyUserDetailsService myUserDetailsService() { - return new MyUserDetailsService(); - } - - @Bean - public MyUserService myUserService() { - return new MyUserService(); - } -} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java deleted file mode 100644 index 804d391641..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/MyUser.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.baeldung.persistence.model; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -@Entity -@Table(schema = "spring_custom_user_service") -public class MyUser { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - @Column(unique = true, nullable = false) - private String username; - - @Column(nullable = false) - private String password; - - public MyUser() { - } - - public int getId() { - return id; - } - - public void setId(final int id) { - this.id = id; - } - - public String getUsername() { - return username; - } - - public void setUsername(final String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(final String password) { - this.password = password; - } -} diff --git a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java deleted file mode 100644 index 4c02f53d20..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/security/MyUserDetailsService.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.baeldung.security; - -import java.util.ArrayList; -import java.util.Collection; - -import org.baeldung.persistence.model.MyUser; -import org.baeldung.user.dao.MyUserDAO; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.authority.SimpleGrantedAuthority; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.core.userdetails.UsernameNotFoundException; -import org.springframework.stereotype.Service; - -@Service("userDetailsService") -public class MyUserDetailsService implements UserDetailsService { - - @Autowired - MyUserDAO myUserDAO; - - @Override - public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException { - final MyUser user = myUserDAO.findByUsername(username); - - if (user == null) { - throw new UsernameNotFoundException("No user found with username: " + username); - } - else { - final Collection authorities = new ArrayList<>(); - authorities.add(new SimpleGrantedAuthority("ROLE_USER")); - return new User(user.getUsername(), user.getPassword(), authorities); - } - } - -} diff --git a/spring-jpa/src/main/java/org/baeldung/security/UserController.java b/spring-jpa/src/main/java/org/baeldung/security/UserController.java deleted file mode 100644 index b1c96e72c0..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/security/UserController.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.baeldung.security; - -import javax.annotation.Resource; - -import org.baeldung.user.service.MyUserService; -import org.baeldung.web.MyUserDto; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -@Controller -public class UserController { - - @Resource - MyUserService myUserService; - - @RequestMapping(value = "/register", method = RequestMethod.POST) - public String registerUserAccount(final MyUserDto accountDto, final Model model) { - final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - model.addAttribute("name", auth.getName()); - try { - myUserService.registerNewUserAccount(accountDto); - model.addAttribute("message", "Registration successful"); - return "index"; - } - catch(final Exception exc){ - model.addAttribute("message", "Registration failed"); - - return "index"; - } - } - - @RequestMapping(value = "/", method = RequestMethod.GET) - public String getHomepage(final Model model) { - final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - model.addAttribute("name", auth.getName()); - return "index"; - } - - @RequestMapping(value = "/register", method = RequestMethod.GET) - public String getRegister() { - return "register"; - } - - @RequestMapping(value = "/login", method = RequestMethod.GET) - public String getLogin() { - return "login"; - } -} diff --git a/spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java b/spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java deleted file mode 100644 index 5741d19bf2..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/user/dao/MyUserDAO.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.baeldung.user.dao; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - -import org.baeldung.persistence.model.MyUser; -import org.springframework.stereotype.Repository; - -@Repository -public class MyUserDAO { - - @PersistenceContext - private EntityManager entityManager; - - public MyUser findByUsername(final String username) { - final Query query = entityManager.createQuery("from MyUser where username=:username", MyUser.class); - query.setParameter("username", username); - final List result = query.getResultList(); - if (result != null && result.size() > 0) { - return result.get(0); - } else - return null; - } - - public MyUser save(final MyUser user) { - entityManager.persist(user); - return user; - } - - public EntityManager getEntityManager() { - return entityManager; - } - - public void setEntityManager(final EntityManager entityManager) { - this.entityManager = entityManager; - } -} diff --git a/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java b/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java deleted file mode 100644 index f4705f3193..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/user/service/MyUserService.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.baeldung.user.service; - -import org.baeldung.persistence.model.MyUser; -import org.baeldung.user.dao.MyUserDAO; -import org.baeldung.web.MyUserDto; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional -public class MyUserService { - - @Autowired - private PasswordEncoder passwordEncoder; - - @Autowired - MyUserDAO myUserDAO; - - public MyUser registerNewUserAccount(final MyUserDto accountDto) throws Exception { - if (usernameExists(accountDto.getUsername())) { - throw new Exception("There is an account with that username: " + accountDto.getUsername()); - } - final MyUser user = new MyUser(); - - user.setUsername(accountDto.getUsername()); - user.setPassword(passwordEncoder.encode(accountDto.getPassword())); - return myUserDAO.save(user); - } - - public MyUser getUserByUsername(final String username) { - final MyUser user = myUserDAO.findByUsername(username); - return user; - } - - private boolean usernameExists(final String username) { - final MyUser user = myUserDAO.findByUsername(username); - if (user != null) { - return true; - } - return false; - } - -} diff --git a/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java b/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java deleted file mode 100644 index c572208913..0000000000 --- a/spring-jpa/src/main/java/org/baeldung/web/MyUserDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.baeldung.web; - -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; - -public class MyUserDto { - @NotNull - @Size(min = 1) - private String username; - - private String password; - - public String getUsername() { - return username; - } - - public void setUsername(final String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(final String password) { - this.password = password; - } - -} diff --git a/spring-jpa/src/main/resources/persistence-derby.properties b/spring-jpa/src/main/resources/persistence-derby.properties deleted file mode 100644 index e808fdc288..0000000000 --- a/spring-jpa/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-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml b/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml deleted file mode 100644 index 25d1d4d22f..0000000000 --- a/spring-jpa/src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml +++ /dev/null @@ -1,88 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /WEB-INF/views/ - - - .jsp - - - - - - - - - - - - - - - ${hibernate.hbm2ddl.auto} - ${hibernate.dialect} - ${hibernate.cache.use_second_level_cache} - ${hibernate.cache.use_query_cache} - - - - - - - - - - - - - - - - - - - diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/index.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/index.jsp deleted file mode 100644 index 0c89257cd2..0000000000 --- a/spring-jpa/src/main/webapp/WEB-INF/views/index.jsp +++ /dev/null @@ -1,35 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=ISO-8859-1" - pageEncoding="ISO-8859-1"%> -<%@ taglib prefix="c" - uri="http://java.sun.com/jsp/jstl/core" %> -<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %> - - - - - -Welcome! - - - - - - - -Register -

- -Login - -

-${message } -

- -Hello, ${name }! -
-
-Logout -
- - - \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/login.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/login.jsp deleted file mode 100644 index 29431f426d..0000000000 --- a/spring-jpa/src/main/webapp/WEB-INF/views/login.jsp +++ /dev/null @@ -1,29 +0,0 @@ -<%@ taglib prefix="c" - uri="http://java.sun.com/jsp/jstl/core" %> - - - - - -

Login

- -
- - - - - - - - - - - - - -
User:
Password:
- -
- Username or password invalid! - - \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp b/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp deleted file mode 100644 index e6e9d373a0..0000000000 --- a/spring-jpa/src/main/webapp/WEB-INF/views/register.jsp +++ /dev/null @@ -1,23 +0,0 @@ -<%@ page language="java" contentType="text/html; charset=ISO-8859-1" - pageEncoding="ISO-8859-1"%> -<%@ taglib prefix="c" - uri="http://java.sun.com/jsp/jstl/core" %> - - - - -Welcome! - - - - - -Register here:

-
-Username:
-Password:
- -
- - - \ No newline at end of file diff --git a/spring-jpa/src/main/webapp/WEB-INF/web.xml b/spring-jpa/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index b526774179..0000000000 --- a/spring-jpa/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - Spring MVC Application - - - - - - mvc-dispatcher - org.springframework.web.servlet.DispatcherServlet - 1 - - - mvc-dispatcher - / - - - - - springSecurityFilterChain - org.springframework.web.filter.DelegatingFilterProxy - - - springSecurityFilterChain - /* - - - - index.jsp - - - \ No newline at end of file From a497c42f90b4fb2898a55dab34d30cf5467935c5 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Tue, 20 Sep 2016 13:03:27 -0400 Subject: [PATCH 07/13] readme file --- spring-userservice/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 spring-userservice/README.md diff --git a/spring-userservice/README.md b/spring-userservice/README.md new file mode 100644 index 0000000000..097afc5fc1 --- /dev/null +++ b/spring-userservice/README.md @@ -0,0 +1 @@ +spring-userservice is using a in memory derby db. Right click -> run on server to run the project \ No newline at end of file From d9f89bdad3b572d7e8c82aa93bdef96d95f3017e Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Mon, 26 Sep 2016 20:55:17 -0400 Subject: [PATCH 08/13] removed extra files --- .../org.eclipse.wst.common.project.facet.core.xml | 8 -------- spring-jpa/.springBeans | 15 --------------- 2 files changed, 23 deletions(-) delete mode 100644 spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml delete mode 100644 spring-jpa/.springBeans diff --git a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml deleted file mode 100644 index 24342c1b93..0000000000 --- a/spring-jpa/.settings/org.eclipse.wst.common.project.facet.core.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/spring-jpa/.springBeans b/spring-jpa/.springBeans deleted file mode 100644 index ff32b84d3b..0000000000 --- a/spring-jpa/.springBeans +++ /dev/null @@ -1,15 +0,0 @@ - - - 1 - - - - - - - - - - - - From a24db3ac1e4214c67ce85637aa24e8048cca54eb Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Tue, 27 Sep 2016 20:45:42 -0400 Subject: [PATCH 09/13] add test for custom user details service --- .../java/org/baeldung/user/dao/MyUserDAO.java | 6 ++ .../baeldung/user/service/MyUserService.java | 10 ++++ .../CustomUserDetailsServiceTest.java | 59 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java diff --git a/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java b/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java index 5741d19bf2..4cc9f61b4a 100644 --- a/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java +++ b/spring-userservice/src/main/java/org/baeldung/user/dao/MyUserDAO.java @@ -29,6 +29,12 @@ public class MyUserDAO { entityManager.persist(user); return user; } + + public void removeUserByUsername(String username) { + final Query query = entityManager.createQuery("delete from MyUser where username=:username"); + query.setParameter("username", username); + query.executeUpdate(); + } public EntityManager getEntityManager() { return entityManager; diff --git a/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java b/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java index f4705f3193..891d6863ce 100644 --- a/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java +++ b/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java @@ -1,5 +1,8 @@ package org.baeldung.user.service; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + import org.baeldung.persistence.model.MyUser; import org.baeldung.user.dao.MyUserDAO; import org.baeldung.web.MyUserDto; @@ -14,6 +17,9 @@ public class MyUserService { @Autowired private PasswordEncoder passwordEncoder; + + @PersistenceContext + private EntityManager entityManager; @Autowired MyUserDAO myUserDAO; @@ -33,6 +39,10 @@ public class MyUserService { final MyUser user = myUserDAO.findByUsername(username); return user; } + + public void removeUserByUsername(String username){ + myUserDAO.removeUserByUsername(username); + } private boolean usernameExists(final String username) { final MyUser user = myUserDAO.findByUsername(username); diff --git a/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java b/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java new file mode 100644 index 0000000000..29998b8fea --- /dev/null +++ b/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java @@ -0,0 +1,59 @@ +package org.baeldung.userservice; + +import org.baeldung.custom.config.MvcConfig; +import org.baeldung.custom.config.PersistenceDerbyJPAConfig; +import org.baeldung.custom.config.SecSecurityConfig; +import org.baeldung.user.service.MyUserService; +import org.baeldung.web.MyUserDto; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import static org.junit.Assert.*; + +import java.util.logging.Level; +import java.util.logging.Logger; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class }) +@WebAppConfiguration +public class CustomUserDetailsServiceTest { + + private static final Logger LOG = Logger.getLogger("CustomUserDetailsServiceTest"); + + public static final String USERNAME = "user"; + public static final String PASSWORD = "pass"; + + @Autowired + MyUserService myUserService; + + @Autowired + AuthenticationProvider authenticationProvider; + + @Test + public void whenAuthenticateUser_thenRetrieveFromDb() { + try { + MyUserDto userDTO = new MyUserDto(); + userDTO.setUsername(USERNAME); + userDTO.setPassword(PASSWORD); + + myUserService.registerNewUserAccount(userDTO); + + UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(USERNAME, PASSWORD); + Authentication authentication = authenticationProvider.authenticate(auth); + + assertEquals(authentication.getName(), USERNAME); + + } catch (Exception exc) { + LOG.log(Level.SEVERE, "Error creating account"); + } finally { + myUserService.removeUserByUsername(USERNAME); + } + } + +} From 9226fdc110579861bb201b3ff77e92db9e5d27c1 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Tue, 27 Sep 2016 20:48:05 -0400 Subject: [PATCH 10/13] remove unnecessary code --- .../main/java/org/baeldung/user/service/MyUserService.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java b/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java index 891d6863ce..1e05541998 100644 --- a/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java +++ b/spring-userservice/src/main/java/org/baeldung/user/service/MyUserService.java @@ -1,8 +1,5 @@ package org.baeldung.user.service; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - import org.baeldung.persistence.model.MyUser; import org.baeldung.user.dao.MyUserDAO; import org.baeldung.web.MyUserDto; @@ -17,9 +14,6 @@ public class MyUserService { @Autowired private PasswordEncoder passwordEncoder; - - @PersistenceContext - private EntityManager entityManager; @Autowired MyUserDAO myUserDAO; From e5302d4e75f170b3e220b3bae6c9d0d4abe9193b Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 28 Sep 2016 10:35:21 -0400 Subject: [PATCH 11/13] add failed authentication test --- .../CustomUserDetailsServiceTest.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java b/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java index 29998b8fea..6e1cd67e06 100644 --- a/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java +++ b/spring-userservice/src/test/java/org/baeldung/userservice/CustomUserDetailsServiceTest.java @@ -9,9 +9,12 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.dao.DuplicateKeyException; import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import static org.junit.Assert.*; @@ -28,6 +31,7 @@ public class CustomUserDetailsServiceTest { public static final String USERNAME = "user"; public static final String PASSWORD = "pass"; + public static final String USERNAME2 = "user2"; @Autowired MyUserService myUserService; @@ -36,7 +40,7 @@ public class CustomUserDetailsServiceTest { AuthenticationProvider authenticationProvider; @Test - public void whenAuthenticateUser_thenRetrieveFromDb() { + public void givenExistingUser_whenAuthenticate_thenRetrieveFromDb() { try { MyUserDto userDTO = new MyUserDto(); userDTO.setUsername(USERNAME); @@ -55,5 +59,27 @@ public class CustomUserDetailsServiceTest { myUserService.removeUserByUsername(USERNAME); } } + + @Test (expected = BadCredentialsException.class) + public void givenIncorrectUser_whenAuthenticate_thenBadCredentialsException() { + try { + MyUserDto userDTO = new MyUserDto(); + userDTO.setUsername(USERNAME); + userDTO.setPassword(PASSWORD); + + try { + myUserService.registerNewUserAccount(userDTO); + } + catch (Exception exc) { + LOG.log(Level.SEVERE, "Error creating account"); + } + + UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(USERNAME2, PASSWORD); + Authentication authentication = authenticationProvider.authenticate(auth); + } + finally { + myUserService.removeUserByUsername(USERNAME); + } + } } From 769956a808235ba55e0ea2440df38010a85880cf Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 29 Sep 2016 09:35:12 -0400 Subject: [PATCH 12/13] added pom.xml --- spring-userservice/pom.xml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spring-userservice/pom.xml b/spring-userservice/pom.xml index 5edaf15d9b..341e232ec6 100644 --- a/spring-userservice/pom.xml +++ b/spring-userservice/pom.xml @@ -195,7 +195,17 @@ jstl-api 1.2 - + + org.springframework.boot + spring-boot-test + 1.4.1.RELEASE + + + org.springframework.boot + spring-boot + 1.4.1.RELEASE + + spring-userservice From d12d4e6ca56e9127964d71ed9a832047a6be676c Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Thu, 29 Sep 2016 13:52:11 -0400 Subject: [PATCH 13/13] update dependency --- spring-userservice/pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-userservice/pom.xml b/spring-userservice/pom.xml index 341e232ec6..93b01ee49c 100644 --- a/spring-userservice/pom.xml +++ b/spring-userservice/pom.xml @@ -139,11 +139,6 @@ ${org.springframework.version} - - javax.servlet - servlet-api - 3.0-alpha-1 - org.springframework.security spring-security-core @@ -205,6 +200,11 @@ spring-boot 1.4.1.RELEASE + + javax.servlet + javax.servlet-api + 3.1.0 +