【问题标题】:My Spring Boot project doesn't load css properly from the static folder [duplicate]我的 Spring Boot 项目无法从静态文件夹中正确加载 css [重复]
【发布时间】:2020-03-23 17:36:00
【问题描述】:

我在 Heroku 上部署了我的项目,并注意到当我打开网站时该项目没有正确获取 css。一旦我登录并再次打开网站,css就在那里,即使我再次进入登录页面,css也已经正确存在。我不明白:

所以,我登录了,我得到了这个:

最后,我再次打开网站,所有内容(css、js)都已正确加载。 我检查了每个相关主题并尝试了每种组合,并且总是得到相同的结果。我的配置应该没问题,没有这个问题。我希望有人可以帮助我解决它。我复制了我的配置,每一个帮助将不胜感激。

login.html

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">

<head> 
<!-- Required meta tags --> 
<meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 

<!--CSS-->
<!--We are choosing these two to let the browser to load faster--> 


<link rel="stylesheet" href="../static/css/style.css" th:href="@{/css/style.css}"/>

<!--this is the last one so that we can override previous boostrap styles if we want--> 


</head>

    <body class="signin-body">
      <!-- <div class="container signin-container" layout:fragment="loginContent"> -->  
            <div class="row">
                <div class="col"></div>
                <div class="col-sm-12 col-md-8">
                    <div class="card signin-card">
                        <div class="card-block">
                            <img th:src="@{/imgages/logindetails.png}" width="50%" height="50%" class="img-fluid signin-img"/> 
                            <form name="login" th:action="@{/login}" method="post" class="form-signin">
                                <div class="form-group">
                                <h2 class="form-signin-heading">Please sign in</h2>
                                    <div th:if="${param.error}" class="alert alert-danger">Wrong username and password</div>
                                    <div th:if="${param.logout}" class="alert alert-success">You successfully logged out</div>
                                    <label for="username" class="sr-only">Username</label>
                                    <input type="text" id="username" name="username" class="form-control" placeholder="Username" required="true">
                                </div>
                                    <label for="password" class="sr-only">Password</label>
                                <div class="form-group">
                                <input type="password" id="password" name="password" class="form-control" placeholder="Password" required="true">
                                </div>
                                    <div class="checkbox">
                                    <label>
                                       <input id="remember-me" name="remember-me" type="checkbox">Remember me
                                    </label>                                
                                <button class="btn btn-lg btn-primary btn-block signin-btn" type="submit">Login</button>
                                    </div>
                            </form>
                        </div>
                </div>
                <a class= "new-account" href="/registration">Create New Account</a>
            </div>
        <div class="col"></div>
    </div>

        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
        <script src="css/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
    </body>

</html>

SecurityConf.java

@EnableGlobalMethodSecurity(securedEnabled=true)
@Configuration
public class SecurityConf extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();  
    }
    private UserDetailsService userService;

    @Autowired
    public void setUserService(UserDetailsService userService) {
        this.userService = userService;
    }
    @Autowired
    public void configureAuth(AuthenticationManagerBuilder auth) {

        try {
            auth
            .inMemoryAuthentication()
                .withUser("user")
                .password("{noop}user") 
                .roles("USER")
            .and()
                .withUser("admin")
                .password("{noop}admin")
                .roles("ADMIN");
        } catch (Exception e) {
             System.out.println(" " + e.getMessage());
        }
    }
    @Override
    protected void configure(HttpSecurity httpSec) {

        try {
            httpSec.authorizeRequests()
            //  .antMatchers(HttpMethod.GET, "/").permitAll()
            //  .antMatchers("/delete").hasRole("ADMIN)")
                .antMatchers("/login", "/*.css", "/*.js").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/registration").permitAll()
                .antMatchers("/reg").permitAll()
                .anyRequest().authenticated()
            .and()
                .formLogin()
                .loginPage("/login")
                .permitAll()
            .and()
                .logout()
                .logoutSuccessUrl("/login?logout")
                .permitAll();
        } catch (Exception ex) {
            System.out.println(" " + ex.getMessage());
        }
    }
}

【问题讨论】:

标签: java html css spring-boot


【解决方案1】:

您需要在 WebSecurityConfigurerAdapter 实现类中覆盖以下方法,以指定包含 Spring Boot 仍应提供服务的文件的文件夹,即使尚未经过身份验证。

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

【讨论】:

  • 这很有意义,我什至没有考虑过。但我刚刚创建了建议的方法,同样的问题仍然存在。
  • 我做了一些更改,但基本上你的答案就是解决方案。
猜你喜欢
  • 2017-04-25
  • 2018-08-31
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
相关资源
最近更新 更多