I'm a noob to Spring framework.
Trying to configure security options for the app. I have following as my (xml-less) security config:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("accountService")
UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests()
.antMatchers("/admin/**" ).hasRole( "Admin" )
.and()
.formLogin()
.loginPage("/")
.loginProcessingUrl( "/j_spring_security_check" )
.failureUrl( "/loginfailed" )
.permitAll()
.and().logout().logoutSuccessUrl("/logout")
.and().exceptionHandling().accessDeniedPage("/403");
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
}
It displays the login page but when I submit it throws me a /j_spring_security_check Not Found
exception. Any help is much appreciated.
My web config is thus:
public class WebConfig implements WebApplicationInitializer {
public void onStartup( ServletContext servletContext ) throws ServletException {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register( MvcServletConfig.class );
applicationContext.register( SecurityConfig.class );
//Add the servlet mapping manually and make it initialize automatically
ServletRegistration.Dynamic servlet = servletContext.addServlet( "dispatcher", new DispatcherServlet( applicationContext ) );
servletContext.addListener(new ContextLoaderListener(applicationContext));
servlet.addMapping( "/" );
servlet.setLoadOnStartup( 1 );
}
}
Changed my WebConfig as thus
@Configuration
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected String[] getServletMappings() {
return new String[ ] { "/" };
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[ ] { AppConfig.class, SecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[ ]{ MvcServletConfig.class };
}
}
Added a AbstractSecurityWebApplicationInitializer
as following:
public class SecurityInitialiser extends AbstractSecurityWebApplicationInitializer {
}
My configure()
method override in SecurityConfig
now looks like this:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage( "/" )
.loginProcessingUrl( "/j_spring_security_check" )
.defaultSuccessUrl( "/hello" )
.failureUrl( "/loginfailed" )
.permitAll()
.and()
.logout()
.logoutUrl( "/j_spring_security_logout" )
.logoutSuccessUrl( "/" )
.invalidateHttpSession( true )
.and()
.exceptionHandling().accessDeniedPage( "/WEB-INF/pages/403.jsp" )
.and()
.csrf()
.and()
.httpBasic();
}
Hope this helps someone! Thanks to all who chipped in their suggestions. :-)