I've setup a spring-boot + spring-mvc + spring-security project.
Everything work as expected right now except for the invalid urls.
If I issue:
http://siteaddr.com/invalid/url
I expect to see my 404 not found page, but spring-security redirects to login page first and after authentication shows 404 not found!
I don't think this is how it should work!
Here is my Websecurity config:
package com.webitalkie.webapp.config;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.crypto.password.PasswordEncoder;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
import com.webitalkie.webapp.security.DatabaseUserDetailsServic;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DatabaseUserDetailsServic userDetailsService;
@Autowired
private ServletContext servletContext;
@Override
protected void configure(HttpSecurity http) throws Exception {
servletContext.getFilterRegistration(AbstractSecurityWebApplicationInitializer
.DEFAULT_FILTER_NAME).addMappingForUrlPatterns(EnumSet
.allOf(DispatcherType.class), false, "/*");
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/home/", "/home", "/home/index", "/", "/favicon.ico").permitAll()
.antMatchers("/contents/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/home/loginsec").failureUrl("/loginsecerror").permitAll()
.and()
.logout()
.logoutUrl("/home/logout")
.logoutSuccessUrl("/home/index")
.invalidateHttpSession(true);
}
@Override
@org.springframework.beans.factory.annotation.Autowired
protected void configure(
org.springframework.security.config.annotation
.authentication.builders.AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(getPasswordEncoder());
}
public PasswordEncoder getPasswordEncoder() {
return new BcryptPasswordEncoder(11);
}
}
Do you guys have any idea?