2016-03-09 72 views
0

我試着第一次使用Spring Boot,並且出現403錯誤,我無法弄清楚如何解決問題使用Spring Boot + Spring Security時圖像上的錯誤403

I 「已經使用thymeleaf創建了一個管理頁面:

<!DOCTYPE HTML> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <title>The Link Application</title> 
    <link rel="stylesheet" href="css/bootstrap.min.css"/> 
</head> 
<body> 

<nav class="navbar navbar-default"> 
    <div class="container-fluid"> 

     <div class="navbar-header"> 
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
       <span class="sr-only">Toggle Navigation</span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
      </button> 
      <a class="navbar-brand" href="#"> 
       <img src="img/Company-logo-sm-header.png" /> 
      </a> 
     </div> 
    </div> 
</nav> 

... 

的CSS加載完美,位於src/main/resources/static/css,這是給我的錯誤403位於src/main/resources/static/img

這是我Application類形象:

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 
public class Application { 

    public static void main(String[] args) throws Exception { 
    SpringApplication.run(Application.class, args); 
    } 

} 

我有一個MVC配置類:

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
    registry.addViewController("/home").setViewName("home"); 
    registry.addViewController("/").setViewName("home"); 
    registry.addViewController("/hello").setViewName("hello"); 
    registry.addViewController("/login").setViewName("login"); 
    } 

} 

和安全配置,我不知道如果我正確地使用它,antMatchers(...).permitAll()我似乎應該允許圖片:

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

    http 
     .authorizeRequests() 
      .antMatchers("/public/**", "/resources/**","/resources/public/**").permitAll() 
      .antMatchers("/", "/home", "/link").permitAll() 
      .antMatchers("/css/**", "/js/**", "/img/**", "**/favicon.ico").anonymous() 
      .antMatchers("/admin").hasRole("ADMIN") 
     .anyRequest().authenticated().and() 
     .formLogin().loginPage("/login").permitAll().and() 
     .logout().permitAll(); 
    } 

    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
     .inMemoryAuthentication() 
     .withUser("admin").password("admin").roles("USER", "ADMIN").and() 
     .withUser("user").password("user").roles("USER"); 
    } 

} 

我使用Spring 1.3.3引導我 中沒有/src/main/resources公共目錄,我所有的靜態內容進入/src/main/resources/static,CSS是進入的CSS subfol DER,JS是進入js的子文件夾和做<link rel="stylesheet" href="css/bootstrap.min.css"/><script src="js/jquery-2.2.1.min.js"></script>

任何想法時,爲什麼我在/src/main/resouces/static/img圖像是給我一個錯誤403和/src/main/resouces/static/css CSS和JS在/src/main/resouces/static/js不是他們都工作得很好?

回答

1

我認爲這只是您需要工作的安全配置。

您不需要這條線,因爲那正是靜態資產的服務對象。這不是一條可以訪問的路徑。

.antMatchers("/public/**", "/resources/**","/resources/public/**").permitAll() 

至於這條線,嘗試改變.anonymous().permitAll(),你應該能夠訪問圖像。

.antMatchers("/css/**", "/js/**", "/img/**", "**/favicon.ico").anonymous() 
+0

偉大的東西,工程! 'anonymous()'做了什麼? permitAll()我可以看到的是允許所有請求,匿名我不清楚。 –

+1

我從來沒有在我編寫的任何應用程序中使用過它,但是您可以將匿名身份驗證視爲具有某些限制的實際用戶。 –

相關問題