【问题标题】:Why I am getting error while doing spring-security jdbc authentication?为什么在进行 spring-security jdbc 身份验证时出现错误?
【发布时间】:2020-08-06 00:53:09
【问题描述】:

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;

    }
  }

我认为我从 @Configuration 注释中得到了错误

每次我运行服务器时,它都会加载一段时间并最后给出异常

演示安全配置

@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");

}

}

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

【问题讨论】:

    标签: java spring maven spring-security spring-jdbc


    【解决方案1】:

    您在 DemoAppConfig 中返回了函数而不是变量

    更改返回 securityDatasoruce();返回 securityDataSource

    现在应该可以正常工作了

    【讨论】:

      猜你喜欢
      • 2014-11-23
      • 1970-01-01
      • 2019-06-24
      • 2015-01-03
      • 2014-02-01
      • 2016-09-13
      • 2016-04-25
      • 2014-08-02
      • 2018-10-04
      相关资源
      最近更新 更多