Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的Spring Boot项目没有';t从静态文件夹正确加载css_Java_Html_Css_Spring Boot - Fatal编程技术网

Java 我的Spring Boot项目没有';t从静态文件夹正确加载css

Java 我的Spring Boot项目没有';t从静态文件夹正确加载css,java,html,css,spring-boot,Java,Html,Css,Spring Boot,我在Heroku上部署了我的项目,并注意到当我打开网站时,该项目没有正确获取css。一旦我再次登录并打开网站,css就在那里,即使我再次进入登录页面,css也已经在那里了。我不明白: 所以,我登录了,我得到了这个: 最后,我再次打开网站,所有内容(css、js)都正确加载。 我检查了每一个相关的主题,尝试了每一个组合,结果总是一样的。我的配置应该没有问题。我希望有人能帮我修一下。我复制了我的配置,非常感谢您的帮助。 login.html <!doctype html> <h

我在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>

您需要在
websecurityConfigureAdapter
实现类中重写下面的方法,以指定包含文件的文件夹,这些文件即使在尚未通过身份验证的情况下仍应由Spring Boot提供服务

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

这很有道理,我甚至都没想过。但我刚刚创建了建议的方法,同样的问题仍然存在。我做了一些更改,但基本上你的答案就是解决方案。这是否回答了你的问题?不,没用。由于某些原因,我无法使用自己创建的登录页面。只有当我删除了我不想删除的
.anyRequest().authenticated()
时,它才起作用:)
@Override
public void configure(WebSecurity webSecurity) throws Exception {
    webSecurity
        .ignoring()
        .antMatchers("/static/**");
}