Spring Ldap用户未找到异常

时间:2017-08-12 21:50:54

标签: spring authentication spring-security ldap

如果用户输入的用户名不正确,则会显示服务器内部错误500,但它应该在登录页面上显示为错误。

Server error 500

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/resources/**").permitAll()

                .antMatchers("/admin/**").hasRole("ADMIN")

                .antMatchers("/dozent/**").hasRole("USER")

                .anyRequest().authenticated().and()

                .formLogin().loginPage("/login").permitAll().and().logout().permitAll().and();
    }

    @Autowired
    DataSource dataSource;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception, UsernameNotFoundException {
        auth.jdbcAuthentication().
            dataSource(dataSource)
            .usersByUsernameQuery("select username,password, "
                    + "enabled from users where username=?")
            .authoritiesByUsernameQuery("select username, "
                    + "role from user_roles where username=?");

        auth.ldapAuthentication().
        ldapAuthoritiesPopulator
        (new CustomAuthoritiesPopulator())
            .userSearchFilter("(uid={0})").
            contextSource(contextSource());


    }


    @Bean // for LDAP users
    public LdapContextSource contextSource() {
        System.out.println("Ldap contextsource");
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl("ldap://ldapc.xxx.com:389");
        contextSource.setUrl("ldap://ldapm.yyy.com:389");
        contextSource.setBase("ou=people,dc=example,dc=com");
        contextSource.afterPropertiesSet(); // needed otherwise you will have a
                                            // NullPointerException in spring
        return contextSource;
    }

    @Bean // für Admin
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setUsername("xy");
        ds.setPassword("12345");
        ds.setUrl("jdbc:postgresql://xxx.de:5432/xyDB");
        System.out.println(ds);
        return ds;
    }
public class CustomAuthoritiesPopulator implements LdapAuthoritiesPopulator {

    @Override
    public Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData,
            String username) {
        System.out.println("UserRolle aus DB: " + PostgreSQLLassy.getInstance().getRolle(username));
        Collection<GrantedAuthority> gas = new HashSet<GrantedAuthority>();

        if ("User".equals(PostgreSQLLassy.getInstance().getRolle(username))) {
            gas.add(new SimpleGrantedAuthority("ROLE_USER"));

        } else if ("AnotherUserRole".equals(PostgreSQLLassy.getInstance().getRolle(username))) {
            gas.add(new SimpleGrantedAuthority("ROLE_USERR"));
        }

        return gas;
    }
}

0 个答案:

没有答案