Spring Webjars定位器和上下文路径

时间:2016-03-03 09:26:32

标签: java spring spring-mvc spring-security spring-boot

我有Spring Boot应用程序,启用的Spring Security配置如下:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home", "/webjars/**", "/css/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

在我更改上下文路径之前,一切正常。例如,如果我这样设置它:

server:
   context-path: /myapp

我无法访问我的webjars,css ......他们仍在查看:http://localhost:8080/webjars/ ...

如何配置Spring Webjars以使其无论上下文路径如何都能正常工作?

1 个答案:

答案 0 :(得分:5)

好的,我解决了这个问题。

我忘记做的是请求Thymeleaf引擎处理我的CSS和js文件的相对路径。所以,基本上我在模板中更改了这个:

int i;
        int loginid=AL.size();
        for(i=0;i<loginid;i++){
        System.out.println(AL.get(i));
            DriverObj.findElement(By.id("Username ID")).clear();
    DriverObj.findElement((By.id("Username ID"))).sendKeys(AL.get(i));
        DriverObj.findElement(By.id("password")).clear();
        DriverObj.findElement((By.id("password"))).sendKeys("123456");
    DriverObj.findElement(By.name("Button")).click();

到此:

<link rel="stylesheet" type="text/css" href="/webjars/bootstrap/css/bootstrap.min.css" />