Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.34 KB | None | 0 0
  1. https://hellokoding.com/registration-and-login-example-with-spring-xml-configuration-maven-jsp-and-mysql/
  2.  
  3. package spring.config;
  4.  
  5. import javax.annotation.Resource;
  6.  
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.ComponentScan;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.Import;
  11. import org.springframework.context.annotation.PropertySource;
  12. import org.springframework.context.support.ReloadableResourceBundleMessageSource;
  13. import org.springframework.core.env.Environment;
  14. import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
  15. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  16. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  17. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  18. import org.springframework.web.servlet.view.InternalResourceViewResolver;
  19. import org.springframework.security.authentication.AuthenticationManager;
  20. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  21. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  22. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  23. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  24. import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
  25. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  26.  
  27. @Configuration
  28. @EnableWebMvc
  29. @Import({AppConfigurationData.class})
  30. @ComponentScan("com.mobapp")//change
  31. @PropertySource("classpath:application.properties")
  32. public class AppConfiguration extends WebMvcConfigurerAdapter{
  33.  
  34.  
  35. @Resource
  36. private Environment env;
  37.  
  38. @Override
  39. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  40. registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  41. }
  42.  
  43.  
  44. @Override
  45. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  46. configurer.enable();
  47. }
  48.  
  49. @Bean
  50. public InternalResourceViewResolver viewResolver() {
  51. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  52. resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
  53. resolver.setPrefix("/WEB-INF/views/");
  54. resolver.setSuffix(".jsp");
  55. return resolver;
  56. }
  57.  
  58.  
  59. @Bean
  60. public ReloadableResourceBundleMessageSource messageSource() {
  61. ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
  62. messageSource.setBasename("classpath:validation");
  63. // messageSource.setCacheSeconds(10); //reload messages every 10 seconds
  64. return messageSource;
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76. package spring.config;
  77. import org.springframework.context.annotation.Bean;
  78. import org.springframework.context.annotation.Configuration;
  79. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  80.  
  81.  
  82. import java.util.Properties;
  83. import javax.annotation.Resource;
  84. import org.hibernate.dialect.MySQL5Dialect;
  85. import org.hibernate.ejb.HibernatePersistence;
  86. import org.springframework.core.env.Environment;
  87. import org.springframework.orm.jpa.JpaTransactionManager;
  88. import org.springframework.orm.jpa.JpaVendorAdapter;
  89. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  90. import org.springframework.orm.jpa.vendor.Database;
  91. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  92.  
  93.  
  94. @Configuration
  95. public class AppConfigurationData {
  96.  
  97.  
  98. @Resource
  99. private Environment env;
  100.  
  101. @Bean
  102. public DriverManagerDataSource dataSource() {
  103.  
  104. DriverManagerDataSource source = new DriverManagerDataSource();
  105. source.setDriverClassName("com.mysql.jdbc.Driver");
  106.  
  107. source.setUrl("jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
  108. source.setUsername("root");
  109. source.setPassword("");
  110.  
  111.  
  112. return source;
  113. }
  114.  
  115.  
  116.  
  117. private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
  118. private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
  119.  
  120. @Bean
  121. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  122. LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  123. entityManagerFactoryBean.setDataSource(dataSource());
  124. entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
  125. entityManagerFactoryBean.setPackagesToScan("spring.domain");
  126. entityManagerFactoryBean.setJpaProperties(hibProperties());
  127. return entityManagerFactoryBean;
  128. }
  129. @Bean
  130. public JpaVendorAdapter jpaVendorAdapter() {
  131. HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
  132. jpaVendorAdapter.setShowSql(true);
  133. jpaVendorAdapter.setDatabase(Database.MYSQL);
  134. jpaVendorAdapter.setDatabasePlatform(MySQL5Dialect.class.getName());
  135. jpaVendorAdapter.setGenerateDdl(false);
  136. return jpaVendorAdapter;
  137. }
  138. private Properties hibProperties() {
  139. Properties properties = new Properties();
  140. properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, "org.hibernate.dialect.MySQL5Dialect");
  141. properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, true);
  142. return properties;
  143. }
  144.  
  145. @Bean
  146. public JpaTransactionManager transactionManager() {
  147. JpaTransactionManager transactionManager = new JpaTransactionManager();
  148. transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
  149. return transactionManager;
  150. }
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. package spring.config;
  172.  
  173. import org.springframework.beans.factory.annotation.Autowired;
  174. import org.springframework.context.annotation.Bean;
  175. import org.springframework.context.annotation.Configuration;
  176. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  177. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  178. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  179. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  180. import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
  181. import org.springframework.security.core.GrantedAuthority;
  182. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  183. import org.springframework.security.core.userdetails.User;
  184. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  185. import org.springframework.security.crypto.password.PasswordEncoder;
  186. import org.springframework.security.provisioning.JdbcUserDetailsManager;
  187. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  188.  
  189. import spring.service.UserServiceImp;
  190.  
  191. import javax.sql.DataSource;
  192. import java.util.ArrayList;
  193. import java.util.List;
  194.  
  195. @Configuration
  196. @EnableWebMvcSecurity
  197. public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  198.  
  199. @Autowired
  200. private UserServiceImp userServiceImp;
  201.  
  202.  
  203. protected void configure(HttpSecurity http) throws Exception {
  204.  
  205.  
  206.  
  207. http.authorizeRequests().anyRequest().authenticated();
  208.  
  209. http
  210. // Authorise everyone for logins and logouts
  211. .authorizeRequests()
  212. .antMatchers("/register", "/register-new-account", "/login",
  213. "/authorise-account")
  214. .anonymous()
  215. .antMatchers("/edit")
  216. .hasAnyAuthority("ADMIN")
  217. .and()
  218.  
  219.  
  220. .formLogin().failureUrl("/login?error")
  221. .defaultSuccessUrl("/displayTree")
  222. .loginPage("/login")
  223. .permitAll()
  224. .and()
  225. .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
  226. .permitAll();
  227. }
  228.  
  229. @Autowired
  230. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  231.  
  232. BCryptPasswordEncoder pe = new BCryptPasswordEncoder();
  233. auth.userDetailsService(userServiceImp).passwordEncoder(encoder());
  234.  
  235. }
  236. @Bean
  237. public BCryptPasswordEncoder encoder() {
  238. return new BCryptPasswordEncoder(11);
  239. }
  240.  
  241. }
  242.  
  243. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  244. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  245.  
  246. Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  247. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  248.  
  249.  
  250. Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  251. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  252.  
  253.  
  254. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  255. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  256.  
  257. Caused by: java.lang.Error: Unresolved compilation problem:
  258. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  259.  
  260. at spring.config.SecurityConfiguration.configure(SecurityConfiguration.java:67)
  261. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  262. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  263. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  264. at java.lang.reflect.Method.invoke(Unknown Source)
  265. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:652)
  266. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
  267. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
  268. ... 62 more
  269. Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
  270. at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
  271.  
  272. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  273. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  274.  
  275. Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  276. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  277.  
  278.  
  279. Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  280. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  281.  
  282.  
  283. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is java.lang.Error: Unresolved compilation problem:
  284. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  285.  
  286.  
  287. Caused by: java.lang.Error: Unresolved compilation problem:
  288. Bound mismatch: The generic method userDetailsService(T) of type AuthenticationManagerBuilder is not applicable for the arguments (UserServiceImp). The inferred type UserServiceImp is not a valid substitute for the bounded parameter <T extends UserDetailsService>
  289.  
  290. at uk.ac.le.cs.CO3098.spring.config.SecurityConfiguration.configure(SecurityConfiguration.java:67)
  291. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  292. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  293. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  294. at java.lang.reflect.Method.invoke(Unknown Source)
  295. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:652)
  296. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
  297. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
  298. ... 62 more
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement