簡體   English   中英

配置 Spring 安全性以顯示 styles

[英]Configuring Spring Security for showing styles

我有個問題。 我的 styles 不適用於未經身份驗證的用戶。

這是我的安全配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/registration").permitAll()
                .anyRequest()
                    .authenticated()
                .and()
                    .formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/hello")
                    .permitAll()
                .and()
                    .logout()
                    .logoutSuccessUrl("/hello")
                    .permitAll();
    }

這也是我對 styles 的配置:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/styles/**")
                .addResourceLocations("/WEB-INF/resources/css/");
    }

這是我的簡單樣式頁面:

<!DOCTYPE html>
<html xmlns:sec="http://www.w3.org/1999/xhtml" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8"/>
    <link rel="stylesheet" type="text/css"
          href="/styles/demo.css">
    <title>Welcome</title>
</head>

<body>
    <div class="red-text">
        Red text
    </div>
    <br>
    <div class="green-text">
        Green text
    </div>
    <span sec:authorize="isAuthenticated()">
        <h1>Welcome, <span sec:authentication="name">Username</span></h1>
        <form th:action="@{/logout}" method="post">
            <input type="submit" value="Log out"/>
        </form>
    </span>
<br>

<input type="button" class="button" onclick="sayHello();"
       value="Click me!">

</body>
</html>

我注意到在我將這一行添加到安全配置之后發生了這種情況:

.anyRequest().authenticated()
.antMatchers("/styles/**").permitAll()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM