Java Spring Tool Suite安全性

时间:2018-04-09 20:35:33

标签: java spring-security

我有一个简单的java应用程序,我希望我的所有页面都可以访问样式表文件夹和文件,即使用户尚未经过身份验证。我在WebSecurityConfig.java文件中有以下代码:

package com.security;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

// Add WebSecurityConfig class to configure security
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    private static final String USER = "USER"; 

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/security").permitAll()
                .antMatchers("/css/**.css").permitAll()
                .antMatchers("/hands_on").hasAnyRole(USER)
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }

}

我在hands_on.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>
<title>hands on Demo</title>
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css" />
</head>
<body>
    <h1>hands on Demo</h1>
</body>
</html>

我的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>
<title>Spring Security Demo</title>
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css" />
</head>
    <body>
        <div th:if="${param.error}">
            Invalid username and password.
        </div>
        <div th:if="${param.logout}">
            You have been logged out.
        </div>
        <form th:action="@{/login}" method="post">
            <div><label> Enter your User Name : <input type="text" name="username"/> </label></div>
            <div><label> Enter your Password: <input type="password" name="password"/> </label></div>
            <div><input type="submit" value="Sign In"/></div>
        </form>
    </body>
</html>

如果我启动我的java应用程序并启动浏览器并转到localhost:8080 / hands_on我希望显示登录页面并显示我的样式表。会发生什么是登录页面显示但没有应用样式。当我查看我的javascript控制台时,我看到了这一点:

  

拒绝申请来自&#39; http://localhost:8080/css/style.css&#39;因为它的MIME类型(&#39; application / json&#39;)不是受支持的样式表MIME类型,并且启用了严格的MIME检查。

我的style.css文件位于security [boot] / src / main / resources / static / css文件夹中。

我以为我可以在我的WebSecurityConfig.java文件中访问基于.antMatchers(&#34; / css / **。css&#34;)。permitAll()的style.css文件,但我猜我我错过了什么。想法?

0 个答案:

没有答案