Spring mvc - Css不能工作



我正在做一个Spring MVC和Spring Security项目。我已经尝试了很多方法来CSS工作,但我没有任何成功。在我的页面上。这里有一些我尝试过的例子。此外,我尝试直接在html代码中找到正确的URL,但一切都不起作用。

这是我的代码。

SecurityConfiguration.java
@Override
protected void configure( HttpSecurity http )throws Exception 
{
    http.authorizeRequests().antMatchers         ( "/resources/**" ).permitAll()
                            .antMatchers         ( "/admin"        ).hasRole( "ADMIN" )
                            .antMatchers         ( "/topics/**"    ).permitAll()
                            .antMatchers         ( "/login"        ).hasRole( "USER"  )
                            .anyRequest          ()
                            .authenticated       ()
                            .and                 ()
                            .formLogin           ()
                            .loginPage           ( "/hello-world" ).permitAll()
                            .and                 ()
                            .logout              ()
                            .logoutRequestMatcher( new AntPathRequestMatcher( "/logout" ) );
}
AppWebConfiguration.java
@Override
public void addResourceHandlers( ResourceHandlerRegistry registry ) 
{
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

page.tag
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!--<spring:url value = "/resources/css/main.css"         var = "main" />
<spring:url value = "/resources/css/home_nocache.css" var = "home_nocache" />
<link href="${main}"         rel="stylesheet" type="text/css">
<link href="${home_nocache}" rel="stylesheet" type="text/css">-->
<!--<link href="${homeLess}" rel="stylesheet" /> -->
<link href= "<c:url value='/resources/css/main.css' />"         rel="stylesheet" ></link>
<link href= "<c:url value='/resources/css/home_nocache.css' />" rel="stylesheet" ></link>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style type="text/css"></style>
</head>

我的申请有什么问题,有人能帮我吗?

我认为你也需要允许css文件夹在应用程序上下文xml类似于

如果CSS在资源文件夹

<intercept-url pattern="/resources/css/**" access="permitAll()" requires-channel="https" />

你有这个/资源/* *

失踪/css/* *

 protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterAfter(new CSRFTokenGeneratorFilter(), CsrfFilter.class)
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers( "/**/" ).permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/wizard").permitAll()
                .antMatchers("/menu").permitAll()
                .antMatchers("/error").permitAll()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/fonts/**").permitAll()
                .antMatchers("/libs/**").permitAll();
}

我已经更新了这个,它是相同的:

@Override
    protected void configure( HttpSecurity http )throws Exception 
    {
        http.authorizeRequests().antMatchers         ( "/resources/**"        ).permitAll()
                                .antMatchers         ( "/resources/css/**"    ).permitAll()
                                .antMatchers         ( "/resources/images/**" ).permitAll()
                                .antMatchers         ( "/admin"               ).hasRole( "ADMIN" )
                                .antMatchers         ( "/topics/**"           ).permitAll()
                                .antMatchers         ( "/login"               ).hasRole( "USER"  )
                                .anyRequest          ()
                                .authenticated       ()
                                .and                 ()
                                .formLogin           ()
                                .loginPage           ( "/hello-world" ).permitAll()
                                .and                 ()
                                .logout              ()
                                .logoutRequestMatcher( new AntPathRequestMatcher( "/logout" ) );
    }

最新更新