Vaadin和Spring,通过LDAP对Active Directory进行身份验证

时间:2014-06-02 14:09:56

标签: java spring active-directory ldap vaadin7

我正在尝试使用Vaadin创建的应用程序中对AD进行身份验证,Vaadin也使用Spring(SpringVaadinIntegration)。

我无法找到有关如何实现此目的的任何信息以及使用Spring安全性连接到Active Directory的许多令人困惑,不同和部分的方法。 由于Vaadin表单字段没有名称,我不知道我是否可以使用普通表单或者我必须编写自己的JSP。我的印象是,要将表单中输入的用户名和密码映射到xml,这些字段必须具有名称。

有没有人实现这个目标,或者有人知道如何做到这一点?

如果某人可以提供一个逐步解释的链接,对于傻瓜,也会很棒。我只能找到部分解决方案,在这里您无法全面了解系统以及应该如何配置。

2 个答案:

答案 0 :(得分:1)

我们在TextField上有PasswordField(用户名),Button(密码)和UI

public class MyUI extends UI {
    @Override
    protected void init( VaadinRequest request ) {
        setContent( VaadinSession.getCurrent().getAttribute("userId") == null ? getNewLoginLayout() : getNewMainLayout() );
    }
    private VerticalLayout getNewLoginLayout() {
        TextField username = ...
        TextField password = ...
        Button login = ...
        return new VerticalLayout(username, password, login);
    }
}

当按下按钮时,我们在服务器端执行简单的LDAP搜索,如this(例如,将这些参数传递给Spring bean)。如果成功,我们设置VaadinSession属性(userId)并将UI内容更改为主布局。不一定需要Spring安全性。

答案 1 :(得分:0)

即使这个问题已经回答了,我想向您展示我的解决方案。

我们使用Spring Security进行LDAP身份验证,因此我们有两个配置类:

@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, proxyTargetClass = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        // @formatter:off
        http
            .authorizeRequests()
                .anyRequest().authenticated() // Alle Requests erfordern einen Login...
                .and()
            .formLogin().loginPage("/login").defaultSuccessUrl("/#!").permitAll() // http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-form
                .and()
            .logout().permitAll() // http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-logout
                .and()
            .csrf().disable(); // CSRF (https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html) wird von Vaadin selbst gehandhabt!
        // @formatter:on
    }

    /**
     * @see http://stackoverflow.com/questions/34944617/java-config-for-spring-security-with-vaadin/35212403#35212403
     */
    @Override
    public void configure(WebSecurity web) throws Exception
    {
        // @formatter:off
        web
            .ignoring()
                .antMatchers("/resources/**", "/VAADIN/**");
        // @formatter:on
    }
}

@Configuration
public class SecurityConfigActiveDirectory
{
    @Value("${ldap.url}")
    String ldapUrl;

    @Value("${ldap.domain}")
    String ldapDomain;

    @Bean
    public AuthenticationManager authenticationManager()
    {
        ActiveDirectoryLdapAuthenticationProvider adProvider = new ActiveDirectoryLdapAuthenticationProvider(ldapDomain, ldapUrl);
        adProvider.setConvertSubErrorCodesToExceptions(true);
        adProvider.setUseAuthenticationRequestCredentials(true);
        adProvider.setAuthoritiesMapper(getAuthorityMapper());
        return new ProviderManager(Arrays.asList(adProvider));
    }

    private static SimpleAuthorityMapper getAuthorityMapper()
    {
        SimpleAuthorityMapper mapper = new SimpleAuthorityMapper();
        mapper.setConvertToUpperCase(true);
        return mapper;
    }
}

SecurityConfig类定义应在我们的Web应用程序中保护哪些页面,SecurityConfigActiveDirectory定义LDAP身份验证提供程序。

ldap.domain可以是 private.myTest.de ldap.url之类的内容,例如 ldap://myLdapHost.private.myTest.de:389

干杯!