Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Use input parameter with mock

@SpringBean
MyClass myClass = Stub {
    method(_) >> {
        Integer id -> map.get(id)
    }
}

-------- or --------

@SpringBean
MyClass myClass = Mock()

given:
myClass.method(_) >> {
        Integer id -> map.get(id)
}

Validate with java & Spring

Posted on by Kim

import javax.validation.constraints.NotNull;

public class Car {

    @NotNull
    private String name;
}
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;

@Service
@Validated
public class CarService {

    public save(@Valid Car car) {
        // TODO
    }
}

@Slf4j
@RequiredArgsConstructor
@Configuration
public class SslConfig {

    private final SslSettings sslSettings;

    @PostConstruct
    void init() {
        String truststore = sslSettings.getTruststore();
        if (truststore != null) {
            log.info("Setting truststore: {}", truststore);
            System.setProperty("javax.net.ssl.trustStore", truststore);
        } else if (System.getProperty("javax.net.ssl.trustStore") != null) {
            log.info("Using provided truststore: {}", System.getProperty("javax.net.ssl.trustStore"));
        } else {
            throw new IllegalStateException("No truststore found!");
        }
    }
}

With problems with handlers

Posted on by Kim

Debug

protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception

in

DispatcherServlet


validate against AD with Spring

Posted on by Kim

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;

import java.util.Collections;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsContextMapperImpl userDetailsContextMapper;

    @Value("${ldap.disable:false}")
    private String disable;

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

    @Value("${ldap.url}")
    private String url;

    @Override
    protected void configure(final HttpSecurity http) throws Exception {

        if (!"true".equals(disable)) {
            http
                    .formLogin()
                    .permitAll()
                    .and()
                    .authorizeRequests()
                    .antMatchers("/ErrorViewer/*")
                    .authenticated()
                    .and().csrf().disable();
        } else {
            http.csrf().disable();
        }
    }

    @Override
    protected void configure(final AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        authManagerBuilder.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailsService());
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Collections.singletonList(activeDirectoryLdapAuthenticationProvider()));
    }

    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        final ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(domain, url);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        provider.setUserDetailsContextMapper(userDetailsContextMapper);
        return provider;
    }
}