remove unused module
This commit is contained in:
parent
01a447d6d3
commit
b651206a8b
5
pom.xml
5
pom.xml
|
@ -755,8 +755,6 @@
|
||||||
|
|
||||||
<module>spring-thymeleaf</module>
|
<module>spring-thymeleaf</module>
|
||||||
|
|
||||||
<module>spring-userservice</module>
|
|
||||||
|
|
||||||
<module>spring-vault</module>
|
<module>spring-vault</module>
|
||||||
<module>spring-vertx</module>
|
<module>spring-vertx</module>
|
||||||
|
|
||||||
|
@ -925,7 +923,6 @@
|
||||||
<module>spring-state-machine</module>
|
<module>spring-state-machine</module>
|
||||||
<module>spring-swagger-codegen/spring-swagger-codegen-app</module>
|
<module>spring-swagger-codegen/spring-swagger-codegen-app</module>
|
||||||
<module>spring-thymeleaf</module>
|
<module>spring-thymeleaf</module>
|
||||||
<module>spring-userservice</module>
|
|
||||||
<module>spring-vault</module>
|
<module>spring-vault</module>
|
||||||
<module>spring-vertx</module>
|
<module>spring-vertx</module>
|
||||||
<module>spring-zuul/spring-zuul-foos-resource</module>
|
<module>spring-zuul/spring-zuul-foos-resource</module>
|
||||||
|
@ -1421,8 +1418,6 @@
|
||||||
|
|
||||||
<module>spring-thymeleaf</module>
|
<module>spring-thymeleaf</module>
|
||||||
|
|
||||||
<module>spring-userservice</module>
|
|
||||||
|
|
||||||
<module>spring-vault</module>
|
<module>spring-vault</module>
|
||||||
<module>spring-vertx</module>
|
<module>spring-vertx</module>
|
||||||
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
derby.log
|
|
|
@ -1,15 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beansProjectDescription>
|
|
||||||
<version>1</version>
|
|
||||||
<pluginVersion><![CDATA[3.7.3.201602250914-RELEASE]]></pluginVersion>
|
|
||||||
<configSuffixes>
|
|
||||||
<configSuffix><![CDATA[xml]]></configSuffix>
|
|
||||||
</configSuffixes>
|
|
||||||
<enableImports><![CDATA[false]]></enableImports>
|
|
||||||
<configs>
|
|
||||||
</configs>
|
|
||||||
<autoconfigs>
|
|
||||||
</autoconfigs>
|
|
||||||
<configSets>
|
|
||||||
</configSets>
|
|
||||||
</beansProjectDescription>
|
|
|
@ -1 +0,0 @@
|
||||||
spring-userservice is using a in memory derby db. Right click -> run on server to run the project
|
|
|
@ -1,240 +0,0 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<groupId>spring-userservice</groupId>
|
|
||||||
<artifactId>spring-userservice</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<packaging>war</packaging>
|
|
||||||
<name>spring-userservice</name>
|
|
||||||
|
|
||||||
<parent>
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>parent-spring-4</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<relativePath>../parent-spring-4</relativePath>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<!-- Spring -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-orm</artifactId>
|
|
||||||
<version>${spring.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-context</artifactId>
|
|
||||||
<version>${spring.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- persistence -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-entitymanager</artifactId>
|
|
||||||
<version>${hibernate.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-ehcache</artifactId>
|
|
||||||
<version>${hibernate.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>xml-apis</groupId>
|
|
||||||
<artifactId>xml-apis</artifactId>
|
|
||||||
<version>${xml-apis.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.javassist</groupId>
|
|
||||||
<artifactId>javassist</artifactId>
|
|
||||||
<version>${javassist.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>mysql</groupId>
|
|
||||||
<artifactId>mysql-connector-java</artifactId>
|
|
||||||
<version>${mysql-connector-java.version}</version>
|
|
||||||
<scope>runtime</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.data</groupId>
|
|
||||||
<artifactId>spring-data-jpa</artifactId>
|
|
||||||
<version>${spring-data-jpa.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.h2database</groupId>
|
|
||||||
<artifactId>h2</artifactId>
|
|
||||||
<version>${h2.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- validation -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-validator</artifactId>
|
|
||||||
<version>${hibernate-validator.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.el</groupId>
|
|
||||||
<artifactId>javax.el-api</artifactId>
|
|
||||||
<version>${javax.el-api.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- utils -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.google.guava</groupId>
|
|
||||||
<artifactId>guava</artifactId>
|
|
||||||
<version>${guava.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- test scoped -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.commons</groupId>
|
|
||||||
<artifactId>commons-lang3</artifactId>
|
|
||||||
<version>${commons-lang3.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-test</artifactId>
|
|
||||||
<version>${spring.version}</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-core</artifactId>
|
|
||||||
<version>${spring.version}</version>
|
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<artifactId>commons-logging</artifactId>
|
|
||||||
<groupId>commons-logging</groupId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-web</artifactId>
|
|
||||||
<version>${spring.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
<version>${spring.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-core</artifactId>
|
|
||||||
<version>${org.springframework.security.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-web</artifactId>
|
|
||||||
<version>${org.springframework.security.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-config</artifactId>
|
|
||||||
<version>${org.springframework.security.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derby</artifactId>
|
|
||||||
<version>${derby.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derbyclient</artifactId>
|
|
||||||
<version>${derby.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derbynet</artifactId>
|
|
||||||
<version>${derby.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derbytools</artifactId>
|
|
||||||
<version>${derby.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>taglibs</groupId>
|
|
||||||
<artifactId>standard</artifactId>
|
|
||||||
<version>${taglibs-standard.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-taglibs</artifactId>
|
|
||||||
<version>${org.springframework.security.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
|
||||||
<artifactId>jstl-api</artifactId>
|
|
||||||
<version>${jstl.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-test</artifactId>
|
|
||||||
<version>${spring-boot.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot</artifactId>
|
|
||||||
<version>${spring-boot.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
|
||||||
<version>${javax.servlet-api.version}</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<finalName>spring-userservice</finalName>
|
|
||||||
<resources>
|
|
||||||
<resource>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<filtering>true</filtering>
|
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<plugins>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
|
||||||
<version>${maven-war-plugin.version}</version>
|
|
||||||
</plugin>
|
|
||||||
</plugins>
|
|
||||||
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<!-- Spring -->
|
|
||||||
<org.springframework.security.version>4.2.6.RELEASE</org.springframework.security.version>
|
|
||||||
<spring-boot.version>1.5.14.RELEASE</spring-boot.version>
|
|
||||||
<javassist.version>3.21.0-GA</javassist.version>
|
|
||||||
|
|
||||||
<!-- persistence -->
|
|
||||||
<hibernate.version>5.2.10.Final</hibernate.version>
|
|
||||||
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
|
|
||||||
<spring-data-jpa.version>1.10.5.RELEASE</spring-data-jpa.version>
|
|
||||||
<derby.version>10.13.1.1</derby.version>
|
|
||||||
|
|
||||||
<!-- various -->
|
|
||||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
|
||||||
<javax.el-api.version>2.2.5</javax.el-api.version>
|
|
||||||
<taglibs-standard.version>1.1.2</taglibs-standard.version>
|
|
||||||
|
|
||||||
<!-- util -->
|
|
||||||
<guava.version>19.0</guava.version>
|
|
||||||
<xml-apis.version>1.4.01</xml-apis.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,35 +0,0 @@
|
||||||
package org.baeldung.security;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
return new User(user.getUsername(), user.getPassword(), Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,51 +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";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,46 +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<MyUser> 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 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEntityManager(final EntityManager entityManager) {
|
|
||||||
this.entityManager = entityManager;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,49 +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;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeUserByUsername(String username) {
|
|
||||||
myUserDAO.removeUserByUsername(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean usernameExists(final String username) {
|
|
||||||
final MyUser user = myUserDAO.findByUsername(username);
|
|
||||||
if (user != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<configuration>
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder>
|
|
||||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
|
||||||
</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
|
|
||||||
<root level="INFO">
|
|
||||||
<appender-ref ref="STDOUT" />
|
|
||||||
</root>
|
|
||||||
</configuration>
|
|
|
@ -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=update
|
|
||||||
hibernate.cache.use_second_level_cache=false
|
|
||||||
hibernate.cache.use_query_cache=false
|
|
|
@ -1,3 +0,0 @@
|
||||||
Manifest-Version: 1.0
|
|
||||||
Class-Path:
|
|
||||||
|
|
|
@ -1,88 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns:context="http://www.springframework.org/schema/context"
|
|
||||||
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.2.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.3.xsd
|
|
||||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
|
|
||||||
|
|
||||||
<context:annotation-config></context:annotation-config>
|
|
||||||
|
|
||||||
<security:http auto-config="true">
|
|
||||||
<security:intercept-url pattern="/" access="permitAll" />
|
|
||||||
<security:intercept-url pattern="/login*" access="permitAll" />
|
|
||||||
<security:intercept-url pattern="/logout*" access="permitAll" />
|
|
||||||
<security:intercept-url pattern="/register*" access="permitAll" />
|
|
||||||
|
|
||||||
<security:csrf disabled="true"/>
|
|
||||||
|
|
||||||
<security:form-login login-page='/login' login-processing-url="/login" default-target-url="/" authentication-failure-url="/login?error=true"
|
|
||||||
always-use-default-target="true"/>
|
|
||||||
|
|
||||||
<security:logout logout-url="/logout" delete-cookies="JSESSIONID" logout-success-url="/"/>
|
|
||||||
</security:http>
|
|
||||||
|
|
||||||
<security:authentication-manager>
|
|
||||||
<security:authentication-provider user-service-ref="myUserDetailsService" >
|
|
||||||
<security:password-encoder ref="passwordEncoder"></security:password-encoder>
|
|
||||||
</security:authentication-provider>
|
|
||||||
</security:authentication-manager>
|
|
||||||
|
|
||||||
<bean id="myUserDetailsService" class="org.baeldung.security.MyUserDetailsService" />
|
|
||||||
|
|
||||||
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
|
|
||||||
<constructor-arg value="11"/>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<context:component-scan base-package="org.baeldung.security"></context:component-scan>
|
|
||||||
<context:component-scan base-package="org.baeldung.user"></context:component-scan>
|
|
||||||
|
|
||||||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
|
||||||
<property name="prefix">
|
|
||||||
<value>/WEB-INF/views/</value>
|
|
||||||
</property>
|
|
||||||
<property name="suffix">
|
|
||||||
<value>.jsp</value>
|
|
||||||
</property>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<context:property-placeholder location="classpath:persistence-derby.properties"/>
|
|
||||||
|
|
||||||
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
|
|
||||||
<property name="dataSource" ref="dataSource"/>
|
|
||||||
<property name="packagesToScan" value="org.baeldung.persistence.model"/>
|
|
||||||
<property name="jpaVendorAdapter">
|
|
||||||
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
|
|
||||||
<!-- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> <property name="generateDdl" value="${jpa.generateDdl}" /> <property name="databasePlatform"
|
|
||||||
value="${persistence.dialect}" /> </bean> -->
|
|
||||||
</property>
|
|
||||||
<property name="jpaProperties">
|
|
||||||
<props>
|
|
||||||
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
|
|
||||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
|
||||||
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
|
|
||||||
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
|
|
||||||
</props>
|
|
||||||
</property>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
|
||||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
|
||||||
<property name="url" value="${jdbc.url}"/>
|
|
||||||
<property name="username" value="${jdbc.user}"/>
|
|
||||||
<property name="password" value="${jdbc.pass}"/>
|
|
||||||
</bean>
|
|
||||||
|
|
||||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
|
||||||
<property name="entityManagerFactory" ref="myEmf"/>
|
|
||||||
</bean>
|
|
||||||
<tx:annotation-driven/>
|
|
||||||
|
|
||||||
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
|
||||||
|
|
||||||
</beans>
|
|
|
@ -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" %>
|
|
||||||
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
||||||
<title>Welcome!</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<c:url value="/register" var="registerUrl" />
|
|
||||||
<c:url value="/login" var="loginUrl"/>
|
|
||||||
<c:url value="/logout" var="logoutUrl"/>
|
|
||||||
|
|
||||||
<a href="${registerUrl }">Register</a>
|
|
||||||
<br /><br/>
|
|
||||||
<sec:authorize access="!isAuthenticated()">
|
|
||||||
<a href="${loginUrl }">Login</a>
|
|
||||||
</sec:authorize>
|
|
||||||
<br><br>
|
|
||||||
${message }
|
|
||||||
<br><br>
|
|
||||||
<sec:authorize access="isAuthenticated()">
|
|
||||||
Hello, ${name }!
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<a href="${logoutUrl }">Logout</a>
|
|
||||||
</sec:authorize>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,29 +0,0 @@
|
||||||
<%@ taglib prefix="c"
|
|
||||||
uri="http://java.sun.com/jsp/jstl/core" %>
|
|
||||||
|
|
||||||
<html>
|
|
||||||
<head></head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<h1>Login</h1>
|
|
||||||
|
|
||||||
<form name='f' action="login" method='POST'>
|
|
||||||
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<td>User:</td>
|
|
||||||
<td><input type='text' name='username' value=''></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Password:</td>
|
|
||||||
<td><input type='password' name='password' /></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td><input name="submit" type="submit" value="submit" /></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
|
|
||||||
</form>
|
|
||||||
<c:if test="${param.error }">Username or password invalid! </c:if>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -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" %>
|
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
|
||||||
<title>Welcome!</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<c:url value="/register" var="registerUrl" />
|
|
||||||
|
|
||||||
Register here:<br><br>
|
|
||||||
<form action="${registerUrl }" method="POST">
|
|
||||||
Username: <input type="text" name="username"/><br>
|
|
||||||
Password: <input type="password" name="password"/><br>
|
|
||||||
<input type="submit" value="Register"/>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
</html>
|
|
|
@ -1,51 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
|
||||||
version="3.1">
|
|
||||||
|
|
||||||
<display-name>Spring MVC Application</display-name>
|
|
||||||
|
|
||||||
|
|
||||||
<!--
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextClass</param-name>
|
|
||||||
<param-value>
|
|
||||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
|
||||||
</param-value>
|
|
||||||
</context-param>
|
|
||||||
<context-param>
|
|
||||||
<param-name>contextConfigLocation</param-name>
|
|
||||||
<param-value>org.baeldung.custom.config</param-value>
|
|
||||||
</context-param>
|
|
||||||
|
|
||||||
<listener>
|
|
||||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
|
||||||
</listener>
|
|
||||||
-->
|
|
||||||
<!-- Spring child -->
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>mvc-dispatcher</servlet-name>
|
|
||||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
|
||||||
<load-on-startup>1</load-on-startup>
|
|
||||||
</servlet>
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>mvc-dispatcher</servlet-name>
|
|
||||||
<url-pattern>/</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<!-- Spring Security -->
|
|
||||||
<filter>
|
|
||||||
<filter-name>springSecurityFilterChain</filter-name>
|
|
||||||
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
|
|
||||||
</filter>
|
|
||||||
<filter-mapping>
|
|
||||||
<filter-name>springSecurityFilterChain</filter-name>
|
|
||||||
<url-pattern>/*</url-pattern>
|
|
||||||
</filter-mapping>
|
|
||||||
|
|
||||||
<welcome-file-list>
|
|
||||||
<welcome-file>index.jsp</welcome-file>
|
|
||||||
</welcome-file-list>
|
|
||||||
|
|
||||||
</web-app>
|
|
|
@ -1,18 +0,0 @@
|
||||||
package org.baeldung;
|
|
||||||
|
|
||||||
import org.baeldung.custom.config.MvcConfig;
|
|
||||||
import org.baeldung.custom.config.PersistenceDerbyJPAConfig;
|
|
||||||
import org.baeldung.custom.config.SecSecurityConfig;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class })
|
|
||||||
public class SpringContextIntegrationTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,82 +0,0 @@
|
||||||
package org.baeldung.userservice;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
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.context.SpringBootTest;
|
|
||||||
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.test.context.junit4.SpringRunner;
|
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = { MvcConfig.class, PersistenceDerbyJPAConfig.class, SecSecurityConfig.class })
|
|
||||||
@WebAppConfiguration
|
|
||||||
public class CustomUserDetailsServiceIntegrationTest {
|
|
||||||
|
|
||||||
private static final Logger LOG = Logger.getLogger("CustomUserDetailsServiceTest");
|
|
||||||
|
|
||||||
public static final String USERNAME = "user";
|
|
||||||
public static final String PASSWORD = "pass";
|
|
||||||
public static final String USERNAME2 = "user2";
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
MyUserService myUserService;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
AuthenticationProvider authenticationProvider;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenExistingUser_whenAuthenticate_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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue