使用 Spring Security 时 Bootstrap 不起作用

el *_*obo 3 java spring-mvc spring-security twitter-bootstrap-3 spring-boot

我目前正在研究 Spring Boot 并致力于一个小型示例项目。但是由于将 Bootstrap 与 Spring-Boot-Security 包一起使用,我面临着一个非常令人困惑的问题。当我使用以下代码时,页面不会与 Bootstrap 一起显示。

我的 SecurityConfiguration.java 看起来像这样

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .authorizeRequests().antMatchers("/","/products","/product/show/*","/console/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll()
            .and()
            .logout().permitAll();

        httpSecurity.csrf().disable();
        httpSecurity.headers().frameOptions().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
            .withUser("admin").password("{noop}admin").roles("ADMIN")
            .and().withUser("user").password("{noop}user").roles("USER");
    }
}
Run Code Online (Sandbox Code Playgroud)

开发控制台网络

我认为有点令人困惑的是我得到了 301/ 未修改,但是当我尝试这样做以防止我缓存问题时,我完全重新打开了浏览器并使用了一个私人窗口。

当我禁用几乎所有安全功能时,我的页面会使用 Bootstrap 正确呈现。

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
            .authorizeRequests().antMatchers("/","/products","/product/show/*","/console/**").permitAll();


        httpSecurity.csrf().disable();
        httpSecurity.headers().frameOptions().disable();
    }
}
Run Code Online (Sandbox Code Playgroud)

我包括引导使用 Webjars

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>bootstrap</artifactId>
    <version>3.3.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在我的前端代码中,使用名为Thymeleaf. 我将引导程序包含在以下代码中:

 <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
          th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
          rel="stylesheet" media="screen" />
Run Code Online (Sandbox Code Playgroud)

上面的包含在一个名为 的文件中headerinc.html,它包含在像这样的真实页面中:

<!DOCTYPE html>
<html>
<head lang="en">

    <title>Spring Framework Guru</title>

    <!--/*/ <th:block th:include="fragments/headerinc :: head"></th:block> /*/-->
</head>
<body>

<div class="container">
    <!--/*/ <th:block th:include="fragments/header :: header"></th:block> /*/-->
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

什么不可能是问题:例如不使用 mvn clean / mvn install。我这样做了。

有人可以在这里指出我正确的方向吗?提前致谢。

Shu*_*ubh 6

使用它来授权 src/main/resources/static 文件夹中所有可用的资源文件,您可以相应地添加文件夹。

//this method allows static resources to be neglected by spring security
    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
            .antMatchers("/resources/**", "/static/**","/webjars/**");
    }
Run Code Online (Sandbox Code Playgroud)