| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package pl.sudra.configuration;
- import jakarta.annotation.Resource;
- import jakarta.persistence.EntityManagerFactory;
- import org.springframework.context.MessageSource;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.support.ReloadableResourceBundleMessageSource;
- import org.springframework.format.FormatterRegistry;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
- import org.springframework.web.servlet.LocaleResolver;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- import org.springframework.web.servlet.i18n.CookieLocaleResolver;
- import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- import pl.sudra.securityController.CustomInterceptor;
- import pl.sudra.service.RoleService;
- import pl.sudra.utils.RoleConverter;
- import pl.sudra.utils.RoleListConverter;
- import java.time.Duration;
- import java.util.Locale;
- @Configuration
- @EnableWebMvc
- @EnableScheduling
- @EnableTransactionManagement
- @ComponentScan("pl.sudra")
- //@Import({SecurityConfiguration.class})
- public class SpringConfiguration implements WebMvcConfigurer {
- @Bean
- public InternalResourceViewResolver viewResolver() {
- InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
- viewResolver.setPrefix("/");
- viewResolver.setSuffix(".jsp");
- return viewResolver;
- }
- @Bean
- public MessageSource messageSource() {
- ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
- messageSource.setBasename("/resources/i18n/messages");
- messageSource.setDefaultEncoding("UTF-8");
- return messageSource;
- }
- @Bean
- public LocaleResolver localeResolver() {
- CookieLocaleResolver resolver = new CookieLocaleResolver("myLocaleCookie");
- resolver.setDefaultLocale(new Locale("en"));
- resolver.setCookieMaxAge(Duration.ofSeconds(4800));
- return resolver;
- }
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
- interceptor.setParamName("lang");
- registry.addInterceptor(interceptor);
- registry.addInterceptor(new CustomInterceptor());
- }
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry
- .addResourceHandler("/resources/**")
- .addResourceLocations("/resources/");
- }
- @Bean
- @Override
- public LocalValidatorFactoryBean getValidator() {
- LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
- bean.setValidationMessageSource(messageSource());
- return bean;
- }
- @Bean
- public PasswordEncoder passwordEncoder() {
- return new BCryptPasswordEncoder();
- }
- // @Resource(name = "addressService")
- // private AddressService addressService;
- //
- @Resource(name = "roleService")
- private RoleService roleService;
- @Override
- public void addFormatters(FormatterRegistry formatterRegistry) {
- formatterRegistry.addConverter(getRoleConverter());
- formatterRegistry.addConverter(getRoleListConverter());
- }
- // @Bean
- // public AddressConverter getMyAddressConverter() {
- // return new AddressConverter(addressService);
- // }
- //
- @Bean
- public RoleConverter getRoleConverter() {
- return new RoleConverter(roleService);
- }
- @Bean
- public RoleListConverter getRoleListConverter() {
- return new RoleListConverter(roleService);
- }
- @Bean
- public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactory);
- return transactionManager;
- }
- }
|