简体   繁体   中英

Why I am getting error while doing spring-security jdbc authentication?

springframework-version 5.0.2.RELEASE

springsecurity-version 5.0.0.RELEASE

DemoAppConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.luv2code.springsecurity.demo")
@PropertySource("classpath:persistence-mysql.properties")
public class DemoAppConfig {

@Autowired
private Environment env;


@Bean
public ViewResolver viewResolver(){
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

    viewResolver.setPrefix("/WEB-INF/view/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
}


@Bean
public DataSource securityDatasoruce(){

    ComboPooledDataSource securityDataSource
                            = new ComboPooledDataSource();


     try {

         securityDataSource.setDriverClass(env.getProperty("jdbc.driver"));

     } catch (PropertyVetoException exc) {

         exc.printStackTrace();
     }


     securityDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
     securityDataSource.setUser(env.getProperty("jdbc.user"));
     securityDataSource.setPassword(env.getProperty("jdbc.password"));


     securityDataSource.setInitialPoolSize(
             getIntProperty("connection.pool.initialPoolSize"));
     securityDataSource.setMinPoolSize(
             getIntProperty("connection.pool.minPoolSize"));
     securityDataSource.setMaxPoolSize(
             getIntProperty("connection.pool.maxPoolSize"));
     securityDataSource.setMaxIdleTime(
             getIntProperty("connection.pool.maxIdleTime"));

    return securityDatasoruce();

}

private int getIntProperty(String propName){

        String propVal=env.getProperty(propName);

        int intPropVal=Integer.parseInt(propVal);

        return intPropVal;

    }
  }

I think I am getting error from @Configuration annotation

Every time I run server it gets into loading for some time and gives exception at last

DemoSecurityConfig

@Configuration
@EnableWebSecurity
public class DemoSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource securityDataSource;


@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {


    auth.jdbcAuthentication().dataSource(securityDataSource);

}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests()
    .antMatchers("/").hasRole("EMPLOYEE")
    .antMatchers("/leaders").hasRole("MANAGER")
    .antMatchers("/systems").hasRole("ADMIN")
    .and()
        .formLogin()
            .loginPage("/showMyLoginPage")
            .loginProcessingUrl("/authenticateTheUser")
            .permitAll()
        .and()
        .logout().permitAll()
        .and()
        .exceptionHandling()
        .accessDeniedPage("/access-denied");

}

}

at com.luv2code.springsecurity.demo.config.DemoAppConfig$$EnhancerBySpringCGLIB$$b4328af6.securityDatasoruce() at com.luv2code.springsecurity.demo.config.DemoAppConfig.securityDatasoruce(DemoAppConfig.java:81) at com.luv2code.springsecurity.demo.config.DemoAppConfig$$EnhancerBySpringCGLIB$$b4328af6.CGLIB$securityDatasoruce$0() at com.luv2code.springsecurity.demo.config.DemoAppConfig$$EnhancerBySpringCGLIB$$b4328af6$$FastClassBySpringCGLIB$$5fac1996.invoke() at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) at com.luv2code.springsecurity.demo.config.DemoAppConfig$$EnhancerBySpring CGLIB$$b4328af6.securityDatasoruce() at com.luv2code.springsecurity.demo.config.DemoAppConfig.securityDatasoruce(DemoAppConfig.java:81) at com.luv2code.springsecurity.demo.config.DemoAppConfig$$EnhancerBySpringCGLIB$$b4328af6.CGLIB$securityDatasoruce$0() at com.luv2code.springsecurity.demo.config.DemoAppConfig$$EnhancerBySpringCGLIB$$b4328af6$$FastClassBySpringCGLIB$$5fac1996.invoke() at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)

you have returned function instead of variable in DemoAppConfig

change return securityDatasoruce(); to return securityDataSource

now it should work fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM