Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. package com.example.configs;
  2.  
  3. import org.apache.tomcat.jdbc.pool.DataSource;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  9. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  10.  
  11. @Configuration
  12. @EnableWebSecurity
  13. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  14.  
  15. @Autowired
  16. DataSource dataSource;
  17.  
  18.  
  19. @Override
  20. protected void configure(HttpSecurity http) throws Exception {
  21. http
  22. .authorizeRequests()
  23. .antMatchers("/", "/home").permitAll()
  24. .anyRequest().authenticated()
  25. .and()
  26. .formLogin()
  27. .loginPage("/login")
  28. .permitAll()
  29. .and()
  30. .logout()
  31. .permitAll();
  32. }
  33.  
  34. @Autowired
  35. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  36. auth
  37. .jdbcAuthentication().dataSource(dataSource)
  38. .usersByUsernameQuery(
  39. "select username,password, enabled from users where username=?")
  40. .authoritiesByUsernameQuery(
  41. "select username, role from user_roles where username=?"); }
  42. }
  43.  
  44. package com.example.configs;
  45.  
  46. import java.util.Properties;
  47.  
  48. import javax.sql.DataSource;
  49.  
  50. import org.springframework.beans.factory.annotation.Value;
  51. import org.springframework.context.annotation.Bean;
  52. import org.springframework.context.annotation.Configuration;
  53. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  54. import org.springframework.orm.hibernate4.HibernateTransactionManager;
  55. import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
  56. import org.springframework.transaction.annotation.EnableTransactionManagement;
  57.  
  58. @Configuration
  59. @EnableTransactionManagement
  60. public class DatabaseConfig {
  61.  
  62. @Value("${db.driver}")
  63. private String DB_DRIVER;
  64.  
  65. @Value("${db.password}")
  66. private String DB_PASSWORD;
  67.  
  68. @Value("${db.url}")
  69. private String DB_URL;
  70.  
  71. @Value("${db.username}")
  72. private String DB_USERNAME;
  73.  
  74. @Value("${hibernate.dialect}")
  75. private String HIBERNATE_DIALECT;
  76.  
  77. @Value("${hibernate.show_sql}")
  78. private String HIBERNATE_SHOW_SQL;
  79.  
  80. @Value("${hibernate.hbm2ddl.auto}")
  81. private String HIBERNATE_HBM2DDL_AUTO;
  82.  
  83. @Value("${entitymanager.packagesToScan}")
  84. private String ENTITYMANAGER_PACKAGES_TO_SCAN;
  85.  
  86. @Bean
  87. public DataSource dataSource() {
  88. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  89. dataSource.setDriverClassName(DB_DRIVER);
  90. dataSource.setUrl(DB_URL);
  91. dataSource.setUsername(DB_USERNAME);
  92. dataSource.setPassword(DB_PASSWORD);
  93. return dataSource;
  94. }
  95.  
  96. @Bean
  97. public LocalSessionFactoryBean sessionFactory() {
  98. LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
  99. sessionFactoryBean.setDataSource(dataSource());
  100. sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
  101. Properties hibernateProperties = new Properties();
  102. hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
  103. hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
  104. hibernateProperties.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
  105. sessionFactoryBean.setHibernateProperties(hibernateProperties);
  106.  
  107. return sessionFactoryBean;
  108. }
  109.  
  110. @Bean
  111. public HibernateTransactionManager transactionManager() {
  112. HibernateTransactionManager transactionManager =
  113. new HibernateTransactionManager();
  114. transactionManager.setSessionFactory(sessionFactory().getObject());
  115. return transactionManager;
  116. }
  117.  
  118. } // class DatabaseConfig
  119.  
  120. # Thymeleaf
  121. spring.thymeleaf.cache: false
  122.  
  123. # Database
  124. db.driver: com.mysql.jdbc.Driver
  125. db.url: jdbc:mysql://localhost:3306/springdb
  126. db.username: root
  127. db.password: 1234
  128.  
  129. # Hibernate
  130. hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
  131. hibernate.show_sql: true
  132. hibernate.hbm2ddl.auto: update
  133. entitymanager.packagesToScan: com.example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement