-1

I have implemented an application with combination Spring Boot and Angular 4. I put all Angular files under /resources/static directory:

static directory

Then I added to Spring Security classes:

@Configuration
@EnableWebMvc
@ComponentScan("com.inventory")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/static/**")
                .addResourceLocations("classpath:/resources/static/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }
}

and:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/").permitAll()
                .anyRequest().authenticated();
    }

and it seem that everythings should work. Unfortunately whenever I run my app it throws exception:

There was an unexpected error (type=Internal Server Error, status=500).
Could not resolve view with name 'index.html' in servlet with name 'dispatcherServlet'.

Of course I tried diffrent solution like adding this:

@Controller
public class ViewController {

    @RequestMapping(value = "/#/")
    public String index() {
        return "forward:/index.html";
    }

    @RequestMapping(value = "/")
    public String home() {
        return "forward:/index.html";
    }
}

But nothing works. Does anyone has a clue what else can I do?

Jan Testowy
  • 649
  • 3
  • 13
  • 32

1 Answers1

0

When using Spring boot normally you don't have to configure resource handler manually, spring boot will automatically load contents from the following loc:

  • /static
  • /public
  • /resources
  • /META-INF/resources

see spring boot guides on loading static contents

Community
  • 1
  • 1
joseph
  • 127
  • 4
  • And if you want to provide custom location as in your case, then include this in your handler: .addResourceHandler("/resources/**") – joseph Nov 15 '17 at 11:57
  • So I changed to: `@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**", "/", "/#/").addResourceLocations("/resources/static/index.html","/resources/static/index.html","/resources/static/index.html"); }` and it throws `There was an unexpected error (type=Not Found, status=404). No message available` – Jan Testowy Nov 15 '17 at 12:29
  • I don't think you can use # sign in your url, its not meant to be pass in the server. – joseph Nov 20 '17 at 16:18