有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将spring安全性更改为使用jdbc而不是ldap?

我完成了Spring指南“使用LDAP验证用户”,并试图删除LDAP部分,并将其替换为与MySQL数据库的连接。"

我不知道如何注入这个数据源。我目前在Eclipse中得到一个红十字会,它说“数据源无法解析或不是字段”

我已经在本地机器上建立并运行了一个数据库,但我不确定在哪里告诉Spring连接的凭据

  1. 我需要添加什么才能正确注入此数据源
  2. 如何告诉Spring使用什么凭据连接到我的数据库
  3. 我是否错过了使身份验证工作正常进行的任何其他关键步骤

    package hello;
    
    import javax.inject.Inject;
    import javax.sql.DataSource;
    
    import hello.UserAccountDAO;
    
    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.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.crypto.bcrypt.BCryptPasswordEncoder;
    
    @Configuration
    @EnableWebMvcSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Inject
        DataSource dataSource;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest().fullyAuthenticated().and().formLogin();
        }
    
        @Configuration
        protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
    
            @Override
            public void init(AuthenticationManagerBuilder auth) throws Exception {
    
                // auth
                // .ldapAuthentication()
                // .userDnPatterns("uid={0},ou=people")
                // .groupSearchBase("ou=groups")
                // .contextSource().ldif("classpath:test-server.ldif");
    
                auth.jdbcAuthentication().dataSource(this.dataSource)
                        .usersByUsernameQuery("SELECT Username, Password, Enabled FROM User WHERE Username = ?")
                        .authoritiesByUsernameQuery("SELECT Username, Permission FROM UserPermission WHERE Username = ?")
                        .passwordEncoder(new BCryptPasswordEncoder());
            }
        }
    }
    

共 (0) 个答案