SpringConfiguration.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package pl.sudra.configuration;
  2. import jakarta.annotation.Resource;
  3. import jakarta.persistence.EntityManagerFactory;
  4. import org.springframework.context.MessageSource;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.ComponentScan;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.support.ReloadableResourceBundleMessageSource;
  9. import org.springframework.format.FormatterRegistry;
  10. import org.springframework.orm.jpa.JpaTransactionManager;
  11. import org.springframework.scheduling.annotation.EnableScheduling;
  12. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  13. import org.springframework.security.crypto.password.PasswordEncoder;
  14. import org.springframework.transaction.PlatformTransactionManager;
  15. import org.springframework.transaction.annotation.EnableTransactionManagement;
  16. import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
  17. import org.springframework.web.servlet.LocaleResolver;
  18. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  19. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  20. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  21. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  22. import org.springframework.web.servlet.i18n.CookieLocaleResolver;
  23. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  24. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  25. import pl.sudra.securityController.CustomInterceptor;
  26. import pl.sudra.service.RoleService;
  27. import pl.sudra.utils.RoleConverter;
  28. import pl.sudra.utils.RoleListConverter;
  29. import java.time.Duration;
  30. import java.util.Locale;
  31. @Configuration
  32. @EnableWebMvc
  33. @EnableScheduling
  34. @EnableTransactionManagement
  35. @ComponentScan("pl.sudra")
  36. //@Import({SecurityConfiguration.class})
  37. public class SpringConfiguration implements WebMvcConfigurer {
  38. @Bean
  39. public InternalResourceViewResolver viewResolver() {
  40. InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
  41. viewResolver.setPrefix("/");
  42. viewResolver.setSuffix(".jsp");
  43. return viewResolver;
  44. }
  45. @Bean
  46. public MessageSource messageSource() {
  47. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  48. messageSource.setBasename("/resources/i18n/messages");
  49. messageSource.setDefaultEncoding("UTF-8");
  50. return messageSource;
  51. }
  52. @Bean
  53. public LocaleResolver localeResolver() {
  54. CookieLocaleResolver resolver = new CookieLocaleResolver("myLocaleCookie");
  55. resolver.setDefaultLocale(new Locale("en"));
  56. resolver.setCookieMaxAge(Duration.ofSeconds(4800));
  57. return resolver;
  58. }
  59. @Override
  60. public void addInterceptors(InterceptorRegistry registry) {
  61. LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
  62. interceptor.setParamName("lang");
  63. registry.addInterceptor(interceptor);
  64. registry.addInterceptor(new CustomInterceptor());
  65. }
  66. @Override
  67. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  68. registry
  69. .addResourceHandler("/resources/**")
  70. .addResourceLocations("/resources/");
  71. }
  72. @Bean
  73. @Override
  74. public LocalValidatorFactoryBean getValidator() {
  75. LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
  76. bean.setValidationMessageSource(messageSource());
  77. return bean;
  78. }
  79. @Bean
  80. public PasswordEncoder passwordEncoder() {
  81. return new BCryptPasswordEncoder();
  82. }
  83. // @Resource(name = "addressService")
  84. // private AddressService addressService;
  85. //
  86. @Resource(name = "roleService")
  87. private RoleService roleService;
  88. @Override
  89. public void addFormatters(FormatterRegistry formatterRegistry) {
  90. formatterRegistry.addConverter(getRoleConverter());
  91. formatterRegistry.addConverter(getRoleListConverter());
  92. }
  93. // @Bean
  94. // public AddressConverter getMyAddressConverter() {
  95. // return new AddressConverter(addressService);
  96. // }
  97. //
  98. @Bean
  99. public RoleConverter getRoleConverter() {
  100. return new RoleConverter(roleService);
  101. }
  102. @Bean
  103. public RoleListConverter getRoleListConverter() {
  104. return new RoleListConverter(roleService);
  105. }
  106. @Bean
  107. public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
  108. JpaTransactionManager transactionManager = new JpaTransactionManager();
  109. transactionManager.setEntityManagerFactory(entityManagerFactory);
  110. return transactionManager;
  111. }
  112. }