Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://hellokoding.com/registration-and-login-example-with-spring-xml-configuration-maven-jsp-and-mysql/
- package spring.config;
- import javax.annotation.Resource;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Import;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.context.support.ReloadableResourceBundleMessageSource;
- import org.springframework.core.env.Environment;
- import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- import org.springframework.security.authentication.AuthenticationManager;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
- import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
- @Configuration
- @EnableWebMvc
- @Import({AppConfigurationData.class})
- @ComponentScan("com.mobapp")//change
- @PropertySource("classpath:application.properties")
- public class AppConfiguration extends WebMvcConfigurerAdapter{
- @Resource
- private Environment env;
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
- }
- @Override
- public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
- configurer.enable();
- }
- @Bean
- public InternalResourceViewResolver viewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
- resolver.setPrefix("/WEB-INF/views/");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- @Bean
- public ReloadableResourceBundleMessageSource messageSource() {
- ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
- messageSource.setBasename("classpath:validation");
- // messageSource.setCacheSeconds(10); //reload messages every 10 seconds
- return messageSource;
- }
- }
- package spring.config;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import java.util.Properties;
- import javax.annotation.Resource;
- import org.hibernate.dialect.MySQL5Dialect;
- import org.hibernate.ejb.HibernatePersistence;
- import org.springframework.core.env.Environment;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.Database;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- @Configuration
- public class AppConfigurationData {
- @Resource
- private Environment env;
- @Bean
- public DriverManagerDataSource dataSource() {
- DriverManagerDataSource source = new DriverManagerDataSource();
- source.setDriverClassName("com.mysql.jdbc.Driver");
- source.setUrl("jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull");
- source.setUsername("root");
- source.setPassword("");
- return source;
- }
- private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
- private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
- LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
- entityManagerFactoryBean.setDataSource(dataSource());
- entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
- entityManagerFactoryBean.setPackagesToScan("spring.domain");
- entityManagerFactoryBean.setJpaProperties(hibProperties());
- return entityManagerFactoryBean;
- }
- @Bean
- public JpaVendorAdapter jpaVendorAdapter() {
- HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
- jpaVendorAdapter.setShowSql(true);
- jpaVendorAdapter.setDatabase(Database.MYSQL);
- jpaVendorAdapter.setDatabasePlatform(MySQL5Dialect.class.getName());
- jpaVendorAdapter.setGenerateDdl(false);
- return jpaVendorAdapter;
- }
- private Properties hibProperties() {
- Properties properties = new Properties();
- properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, "org.hibernate.dialect.MySQL5Dialect");
- properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, true);
- return properties;
- }
- @Bean
- public JpaTransactionManager transactionManager() {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
- return transactionManager;
- }
- }
- package spring.config;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.User;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import org.springframework.security.provisioning.JdbcUserDetailsManager;
- import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
- import spring.service.UserServiceImp;
- import javax.sql.DataSource;
- import java.util.ArrayList;
- import java.util.List;
- @Configuration
- @EnableWebMvcSecurity
- public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
- @Autowired
- private UserServiceImp userServiceImp;
- protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests().anyRequest().authenticated();
- http
- // Authorise everyone for logins and logouts
- .authorizeRequests()
- .antMatchers("/register", "/register-new-account", "/login",
- "/authorise-account")
- .anonymous()
- .antMatchers("/edit")
- .hasAnyAuthority("ADMIN")
- .and()
- .formLogin().failureUrl("/login?error")
- .defaultSuccessUrl("/displayTree")
- .loginPage("/login")
- .permitAll()
- .and()
- .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
- .permitAll();
- }
- @Autowired
- protected void configure(AuthenticationManagerBuilder auth) throws Exception {
- BCryptPasswordEncoder pe = new BCryptPasswordEncoder();
- auth.userDetailsService(userServiceImp).passwordEncoder(encoder());
- }
- @Bean
- public BCryptPasswordEncoder encoder() {
- return new BCryptPasswordEncoder(11);
- }
- }
- 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:
- 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>
- 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:
- 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>
- 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:
- 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>
- 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:
- 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>
- Caused by: java.lang.Error: Unresolved compilation problem:
- 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>
- at spring.config.SecurityConfiguration.configure(SecurityConfiguration.java:67)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- at java.lang.reflect.Method.invoke(Unknown Source)
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:652)
- at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
- ... 62 more
- 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
- at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
- 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:
- 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>
- 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:
- 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>
- 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:
- 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>
- 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:
- 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>
- Caused by: java.lang.Error: Unresolved compilation problem:
- 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>
- at uk.ac.le.cs.CO3098.spring.config.SecurityConfiguration.configure(SecurityConfiguration.java:67)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
- at java.lang.reflect.Method.invoke(Unknown Source)
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:652)
- at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
- at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
- ... 62 more
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement