0

This is My Code that I have written and i used my Custom User Detail Service here but every time im trying to run this code bean not found expection is comming i Want to know is there any way to implement it because i saw auth object has one method auth.jdbcAuthentication();

package org.springframework.cloud.dataflow.server.config.security;

/**
 * Created by smajumder on 16-May-17.
 */

import org.apache.tomcat.jdbc.pool.DataSource;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
@EnableWebSecurity
@ConditionalOnProperty("security.authentication.jdbc.enabled")
@ConfigurationProperties(prefix = 
JDBCAuthenticationConfiguration.CONFIGURATION_PROPERTIES_PREFIX)
public class JDBCAuthenticationConfiguration extends 
GlobalAuthenticationConfigurerAdapter {

public static final String CONFIGURATION_PROPERTIES_PREFIX ="security"
        + ".authentication.jdbc";
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(JDBCAuthenticationConfiguration.class);

@Autowired
private CustomUserDetailsService userDetailsService ;

@Autowired
private DataSource dataSource;

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
    logger.info("JDBC authentication Starting .....");
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
    logger.info("JDBC authentication Enabled .....");


}

@Bean(name="passwordEncoder")
public PasswordEncoder passwordencoder(){
    return new BCryptPasswordEncoder();
}



}

This is the info i got from shell

This is the stack trace after i did this inside init()

 securityStateBean.setAuthenticationEnabled(true);
    securityStateBean.setAuthorizationEnabled(false);

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer@629f066f to already built object at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.cloud.dataflow.server.local.LocalDataFlowServer.main(LocalDataFlowServer.java:38) [classes!/:1.2.0.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [spring-cloud-dataflow-server-local-1.2.0.RELEASE.jar:1.2.0.RELEASE] at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [spring-cloud-dataflow-server-local-1.2.0.RELEASE.jar:1.2.0.RELEASE] at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [spring-cloud-dataflow-server-local-1.2.0.RELEASE.jar:1.2.0.RELEASE] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [spring-cloud-dataflow-server-local-1.2.0.RELEASE.jar:1.2.0.RELEASE] Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer@629f066f to already built object at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] ... 28 common frames omitted Caused by: java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer@629f066f to already built object at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.add(AbstractConfiguredSecurityBuilder.java:196) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.apply(AbstractConfiguredSecurityBuilder.java:133) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder.apply(AuthenticationManagerBuilder.java:290) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder.jdbcAuthentication(AuthenticationManagerBuilder.java:164) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.cloud.dataflow.server.config.security.JDBCAuthenticationConfiguration.configure(JDBCAuthenticationConfiguration.java:72) ~[spring-cloud-dataflow-server-core-1.2.1.RELEASE.jar!/:1.2.1.RELEASE] at org.springframework.cloud.dataflow.server.config.security.JDBCAuthenticationConfiguration.configure(JDBCAuthenticationConfiguration.java:37) ~[spring-cloud-dataflow-server-core-1.2.1.RELEASE.jar!/:1.2.1.RELEASE] at org.springframework.cloud.dataflow.server.config.security.JDBCAuthenticationConfiguration$$EnhancerBySpringCGLIB$$1a0e187f.configure() ~[spring-cloud-dataflow-server-core-1.2.1.RELEASE.jar!/:1.2.1.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.configure(AbstractConfiguredSecurityBuilder.java:384) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:330) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:41) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration.getAuthenticationManager(AuthenticationConfiguration.java:105) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.authenticationManager(WebSecurityConfigurerAdapter.java:262) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:195) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:315) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:86) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration$ApplicationNoWebSecurityConfigurerAdapter$$EnhancerBySpringCGLIB$$36139df2.init() ~[spring-boot-autoconfigure-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:371) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:325) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:41) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:104) ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration$$EnhancerBySpringCGLIB$$5141ef61.CGLIB$springSecurityFilterChain$6() ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration$$EnhancerBySpringCGLIB$$5141ef61$$FastClassBySpringCGLIB$$cbf558e0.invoke() ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration$$EnhancerBySpringCGLIB$$5141ef61.springSecurityFilterChain() ~[spring-security-config-4.2.2.RELEASE.jar!/:4.2.2.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] ... 29 common frames omitted

This is the error im getting

Subhadip Majumder
  • 323
  • 1
  • 3
  • 11

1 Answers1

1

I guess this doc might be helpful.

Also, there is a sample as well.

Ilayaperumal Gopinathan
  • 4,099
  • 1
  • 13
  • 12
  • Thanks for the documentation ill try to implement it. Actually im just a beginner with spring boot and security and im trying to modify this spring-cloud-dataflows user authentication part. Thank you so much – Subhadip Majumder May 26 '17 at 09:14
  • Hi i have tried this using `code` auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username,password, enabled from users where username=?") .authoritiesByUsernameQuery("select username, role from user_roles where username=?"); `code`but in ui i am not able to fetch details regarding to whether authentication enabled or not i saw in main.js it was comming from /feature controller. but i have no clue what is the issue. – Subhadip Majumder May 29 '17 at 11:18
  • what happens when you connect to the server via Shell. A stack trace or something would help understand the issue better. – Ilayaperumal Gopinathan May 29 '17 at 11:39
  • @IIayaperumal i have added screenshot from shell – Subhadip Majumder May 30 '17 at 06:30
  • That means you are able to authenticate via shell but not through the web UI? – Ilayaperumal Gopinathan May 30 '17 at 09:15
  • Actually that login didnt apeared at all it was authenticated via basic authentication. – Subhadip Majumder May 30 '17 at 10:05
  • Hi i am able to get authenticated if i use the default login of spring boot but from springcloud dataflow ui it is giving 404 not found error on this link localhost:9393/authenticate – Subhadip Majumder May 31 '17 at 10:17
  • Both the shell and the UI should be using the same authentication mechanism. From the web UI, you need to hit http://localhost:9393/dashboard` which is expected to redirect to login page. – Ilayaperumal Gopinathan May 31 '17 at 10:26
  • Yes Sir i did that only but in browser console i got 404 not flound erron on that link – Subhadip Majumder May 31 '17 at 10:31
  • ok, not sure how to debug unless we look at the code. Could you please create Github issue on using custom authentication and the issue you see on this. We'll look into it. Thank you! – Ilayaperumal Gopinathan May 31 '17 at 11:27
  • sure ill create should i create it inside of spring-cloud-dataflow-server-core ??? – Subhadip Majumder Jun 01 '17 at 05:23
  • Hi @ilayaperumal i have posted the issue and issue number is #1522 – Subhadip Majumder Jun 01 '17 at 12:33