I am using Spring 4, the annotation configuration. I tried different combinations of adding css and js to the jsp page, but none of them worked. Project structure:
Configuration:
one
@Configuration public class NewConfigure extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } } 2
public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[] { AppConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{NewConfigure.class}; } @Override protected String[] getServletMappings() { return new String[] { "/" }; } } 3
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired @Qualifier("userDetailsService") UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("**/style/**"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .antMatchers("/user/**").access("hasRole('ROLE_USER')") .antMatchers("/basket/**").access("hasRole('ROLE_USER')") .antMatchers("/resources/**", "/**").permitAll() .and().formLogin() .loginPage("/login").defaultSuccessUrl("/index").failureUrl("/login?error") .usernameParameter("username") .passwordParameter("password") .and().logout().logoutSuccessUrl("/login?logout") .and().exceptionHandling().accessDeniedPage("/403"); http.csrf().disable(); } AppConfig // beans for work:
@EnableWebMvc @Configuration @ComponentScan({"com.vdp.*"}) @EnableTransactionManagement @Import({ SecurityConfig.class }) public class AppConfig { Last connection method:
<link href="/resources/style/style.css" rel="stylesheet" type="text/css"/ > 