I am running a Spring Boot app wherein it runs a vuejs front end deployed as static resources and an API managed by Spring. I am setting up authentication and the scenario is as such.
The front end app sits at /app but / is redirected to /app by Spring anyway. I want a form based login for my app at '/login'. My app uses the API served by Spring and the API sits at /api. As such, I want the API to recognise the logged in session by the front end. But, I also want the API to be Basic authenticated. At the same time I don't want any route except /api to be basic authenticated, i.e. even if I am supplying an authentication header, it should still redirect me to /login. So,
/apiBasic and Session based authentication/**Only Session based authentication through a form
I am using the current code:
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${ldap.urls}")
private String ldapUrls;
@Value("${ldap.base.dn}")
private String ldapBaseDn;
@Value("${ldap.username}")
private String ldapSecurityPrincipal;
@Value("${ldap.password}")
private String ldapPrincipalPassword;
@Value("${ldap.user.dn.pattern}")
private String ldapUserDnPattern;
private AuthenticationEntryPoint authenticationEntryPoint;
@Autowired
public SecurityConfig(AuthenticationEntryPoint authenticationEntryPoint) {
super();
this.authenticationEntryPoint = authenticationEntryPoint;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/**").authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint)
.and()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login?logout")
.invalidateHttpSession(true);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.contextSource()
.url(ldapUrls + ldapBaseDn)
.managerDn(ldapSecurityPrincipal)
.managerPassword(ldapPrincipalPassword)
.and()
.userDnPatterns(ldapUserDnPattern);
}
}
This is not working exactly as expected. My API is authenticated via Basic and Session tokens but so is my app. i.e. I can make a GET request in Postman along with the Basic Authentication headers and the HTML to my homepage is returned.
Along with that I don't think I have a good understanding of how the configuration setup is done especially with using and(). It would be great if someone could direct me to some resources which explains the nitty gritties of configuration.