Home > Blockchain >  Spring security don't show images befor loging in. How to fix?
Spring security don't show images befor loging in. How to fix?

Time:01-15

I can't see the image on my site before logging in. How can I solve this problem? Below is the class of my spring security config. I think problem is inside this class.

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/", "/home" , "/about", "/blog/**", "/blog", "/blog/add", "/registration").permitAll()
                    .anyRequest().authenticated()
                .and()
                    .formLogin()
                    .loginPage("/sign-in")
                    .permitAll()
                .and()
                    .logout()
                    .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .passwordEncoder(NoOpPasswordEncoder.getInstance())
                .usersByUsernameQuery("select username, password, active from usr where username=?")
                .authoritiesByUsernameQuery("select u.username, ur.roles from usr u inner join user_role ur on u.id = ur.user_id where u.username=?");
    }
}

CodePudding user response:

try adding this method to WebSecurityConfig

e.g for images located in static resource directory at: src/main/resources/static/images

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
      .antMatchers("/images/**");
}

CodePudding user response:

As i understood you have a problem to pass the static resources from spring security filter. for this problem you need to change your configuration to exclude the address of your static resources which in your case it is picture. in resources folder on your project create the new folder called image and than add your picture into it and add these lines of code in your spring security config

.antMatchers("/","/public/**", "/resources/**","/resources/image/**")
                .permitAll()
  •  Tags:  
  • Related