com.swordcode.webcore.security.testui.TestAppConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.swordcode.webcore.security.testui.TestAppConfig.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.swordcode.webcore.security.testui;

import com.swordcode.webcore.security.server.MethodSecurityConfig;
import com.swordcode.webcore.security.server.SecurityConfig;
import com.swordcode.webcore.security.server.WebSecurityConfig;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.transaction.PlatformTransactionManager;

/**
 *
 * @author euclidesflores
 */
@Configuration
@EnableWebSecurity
@Import({ SecurityConfig.class, WebSecurityConfig.class, MethodSecurityConfig.class })
public class TestAppConfig extends WebSecurityConfigurerAdapter implements Serializable {
    @Autowired
    private SecurityConfig _securityConfig;

    public SecurityConfig securityConfig() {
        return _securityConfig;
    }

    //   private @Autowired UserDetailsService _userDetailService;
    //   private @Autowired PasswordEncoder _passwordEncoder;
    //   private @Autowired AuthenticationProvider _authenticationProvider;
    //   private @Autowired EntityManager _em;
    //   private @Autowired SecurityUserRepository _userRepo;

    public TestAppConfig() {
    }

    @Bean
    public TestService getTestService() {
        return new TestServiceImpl();
    }

    /*   @Bean @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
        }
        
       @Override
       public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(_authenticationProvider);
          auth.userDetailsService(_userDetailService);
       }
        
       public EntityManager getEm() {
          return _em;
       }
        
       public SecurityUserRepository getSecurityUserRepo() {
          return _userRepo;
       }
    */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws IOException {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "com.swordcode.webcore.security.server.model" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(jpaProperties());
        return em;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws IOException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

        return transactionManager;
    }

    @Bean
    public DataSource dataSource() throws IOException {
        return new HikariDataSource(new HikariConfig(dataSourceProperties()));
    }

    @Bean
    public Properties dataSourceProperties() throws IOException {
        return PropertiesLoaderUtils.loadAllProperties("datasource.properties");
    }

    @Bean
    public Properties jpaProperties() throws IOException {
        return PropertiesLoaderUtils.loadAllProperties("jpa.properties");
    }
}