简体   繁体   中英

Configuring Spring Security for showing styles

I have a problem. My styles don't work for unauthenticated users.

Here is my Security config:

@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();
    }

Also here is my config for styles:

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

And here is my simple style page:

<!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>

I noticed that this happened after I added this line to the security config:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM