Tuesday, November 21, 2017

Spring security lessons

  • css and image files were not getting loaded because i had overridden the configure method of
 WebSecurityConfigurerAdapter class as follows:
  •  @Override
        public void configure(final WebSecurity web) throws Exception {
          
           web.ignoring().antMatchers("/");
                       
        }
  • The above was not loading the css and images. the reason is yet to be known.
  • So when i entered the authentication password css and image files are getting loaded as desired.
  • @Override
        protected void configure(final HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/","/css/**","/images/**").permitAll();
  • }   
  • the above solved the problem to bypass the security for home page,css and image.
  • we also need to override the following method of webconfigurerAdapter class to serve the static resources ie we need to add the resource locations through the following commands:
  • @Override
        public void addResourceHandlers(final ResourceHandlerRegistry registry) {
          //registry.addResourceHandler("/resources/**").addResourceLocations("/", "/resources/");
           
      registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
      registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
      registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
     
           
         }

No comments:

Post a Comment