html - 使用自定义 CSS 登录页面

标签 html css spring-boot thymeleaf

我正在使用 spring boot 制作网站。我想使用我自己的登录页面并使用 CSS 对其进行样式设置。但是,CSS 不会链接到 HTML 页面(由 thymleaf 提供支持)属性。

根据定义的 CSS,我希望为登录页面设置红色背景色,但这并没有发生。此外,当登录成功进行时,它不会重定向到 index.html,而是显示 style.css

如果有人能帮我弄清楚如何将 css 文件正确链接到 html 文件,我将不胜感激

Controller 类:

@Controller
public class ControllerManager {

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping(value = {"/",""} )
    public String homePage(){
        return "index";
    }
}

配置类:

@Configuration
public class ConfigManager extends WebSecurityConfigurerAdapter {


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .anyRequest()
                    .fullyAuthenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll();
    }
}

登录.html 文件

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Please Login</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
      integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/style.css}"/>
</head>
<body>
<div>
    <div class="container">
        <form name="f" th:action="@{/login}" method="post">
            <fieldset>
                <legend>Please Login</legend>
                <div th:if="${param.error}" class="alert alert-error">
                Invalid username and password.
                </div>
                <div th:if="${param.logout}" class="alert alert-success">
                You have been logged out.
                </div>
                <label for="username">Username</label>
                <input type="text" id="username" name="username"/>
                <label for="password">Password</label>
                <input type="password" id="password" name="password"/>
                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">Log in</button>
                </div>
            </fieldset>
        </form>
    </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"
    integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
    crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
    crossorigin="anonymous"></script>

</body>
</html>

模板/index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Please Login</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
      integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

</head>
<body>

<p th:text="Hello World">Greeting</p>
</body>
</html>

静态/css/style.css

body{
    background-color: red;
}

登录前: enter image description here

登录后:

enter image description here

当我点击返回 (<--) 按钮时,样式被应用: enter image description here

调试器当我想登录时: enter image description here

登录后的调试器: enter image description here

点击返回按钮后的调试器:

enter image description here

最佳答案

您应该如下忽略静态资源。

配置类:

@Configuration
public class ConfigManager extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/css/**", "/js/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .anyRequest()
                    .fullyAuthenticated()
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .permitAll();
    }
}

希望对您有所帮助!

关于html - 使用自定义 CSS 登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56836176/

相关文章:

jquery - 正确使用 jQuery Effects

html - 仅在 Wordpress 中更改一页的填充

spring-boot - Spring Boot 中的 AWS SQS、DLQ

java - 当@Autowired 时,SftpRemoteFileTemplate 不适用于 RemoteDirectoryExpression

html - anchor 元素的ping属性如何跨浏览器?

html - 为什么我们使用相对方案//而不是 http ://or https://?

css - 如何对齐父标签中的子标签

java - 在 Spring Boot Java 应用程序中调用自定义 Rest 模板

html - CSS伪:element hover effect is not working correctly

html - 具有固定页眉和页脚的聊天框布局