clean up spring-jpa
This commit is contained in:
parent
678d99b6e5
commit
4b7e5066d8
|
@ -2,7 +2,7 @@
|
||||||
<faceted-project>
|
<faceted-project>
|
||||||
<fixed facet="wst.jsdt.web"/>
|
<fixed facet="wst.jsdt.web"/>
|
||||||
<installed facet="java" version="1.8"/>
|
<installed facet="java" version="1.8"/>
|
||||||
<installed facet="jst.web" version="3.1"/>
|
<installed facet="jst.web" version="3.0"/>
|
||||||
<installed facet="wst.jsdt.web" version="1.0"/>
|
<installed facet="wst.jsdt.web" version="1.0"/>
|
||||||
<installed facet="jpt.jpa" version="2.1"/>
|
<installed facet="jpt.jpa" version="2.1"/>
|
||||||
</faceted-project>
|
</faceted-project>
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
</configSuffixes>
|
</configSuffixes>
|
||||||
<enableImports><![CDATA[false]]></enableImports>
|
<enableImports><![CDATA[false]]></enableImports>
|
||||||
<configs>
|
<configs>
|
||||||
<config>src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml</config>
|
|
||||||
</configs>
|
</configs>
|
||||||
<autoconfigs>
|
<autoconfigs>
|
||||||
</autoconfigs>
|
</autoconfigs>
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>spring-jpa</name>
|
<name>spring-jpa</name>
|
||||||
<packaging>war</packaging>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
@ -126,78 +125,6 @@
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-core</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-web</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>servlet-api</artifactId>
|
|
||||||
<version>3.0-alpha-1</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>10.12.1.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derbyclient</artifactId>
|
|
||||||
<version>10.12.1.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derbynet</artifactId>
|
|
||||||
<version>10.12.1.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.apache.derby</groupId>
|
|
||||||
<artifactId>derbytools</artifactId>
|
|
||||||
<version>10.12.1.1</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>taglibs</groupId>
|
|
||||||
<artifactId>standard</artifactId>
|
|
||||||
<version>1.1.2</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.security</groupId>
|
|
||||||
<artifactId>spring-security-taglibs</artifactId>
|
|
||||||
<version>4.1.3.RELEASE</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet.jsp.jstl</groupId>
|
|
||||||
<artifactId>jstl-api</artifactId>
|
|
||||||
<version>1.2</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -221,12 +148,6 @@
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
|
||||||
<version>${maven-war-plugin.version}</version>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
@ -268,7 +189,6 @@
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<org.springframework.security.version>4.1.3.RELEASE</org.springframework.security.version>
|
|
||||||
<org.springframework.version>4.3.2.RELEASE</org.springframework.version>
|
<org.springframework.version>4.3.2.RELEASE</org.springframework.version>
|
||||||
<javassist.version>3.20.0-GA</javassist.version>
|
<javassist.version>3.20.0-GA</javassist.version>
|
||||||
|
|
||||||
|
@ -301,7 +221,6 @@
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
||||||
|
|
|
@ -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,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<GrantedAuthority> authorities = new ArrayList<>();
|
|
||||||
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
|
|
||||||
return new User(user.getUsername(), user.getPassword(), authorities);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<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 EntityManager getEntityManager() {
|
|
||||||
return entityManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEntityManager(final EntityManager entityManager) {
|
|
||||||
this.entityManager = entityManager;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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,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
|
|
|
@ -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.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
|
|
||||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.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>
|
|
Loading…
Reference in New Issue