Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

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()
question from:https://stackoverflow.com/questions/65643429/configuring-spring-security-for-showing-styles

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
.antMatchers("/styles/**").permitAll()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...