2016-11-30 79 views
-1

我創建了一個Spring Boot應用程序,並在/resources/static文件夾中有我的前端應用程序。用Spring Security保護Angular JS應用程序

對於路由,我使用Angular JS UI路由器庫。 我已經定義了一個路由,我只想訪問管理員,現在我試圖使用Spring Security來保護它。

這裏是我WebSecurity配置類:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { 

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

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity 
     .authorizeRequests() 
     .antMatchers(HttpMethod.GET, "/#/admin").hasRole("ADMIN") 
     .and() 
     .formLogin() 
     .and() 
     .authorizeRequests() 
     .antMatchers(HttpMethod.POST, "/member", "/member/**").permitAll() 
     .antMatchers(
       HttpMethod.GET, 
       "/", 
       "/*.html", 
       "/favicon.ico", 
       "/**/*.html", 
       "/**/*.css", 
       "/**/*.js", 
       "/**/**/*.css", 
       "/**/**/*.js", 
       "/**/**/*.html", 
       "/**/**/**/*.css", 
       "/**/**/**/*.js", 
       "/**/**/**/*.html", 
       "/**/**/**/**/*.css", 
       "/**/**/**/**/*.js"   
    ).permitAll() 
     .antMatchers("/auth/**", "/member/**", "/account/**").permitAll() 

     .and() 
     .csrf().disable(); 

    } 
} 

我想,以確保這條路線可以通過http://localhost:8080/#/admin訪問。
但是,每當我訪問該路由時,都不會請求登錄,任何人都可以查看該頁面。
我應該遵循另一種方法嗎?

回答

1

的網址:http://localhost:8080/#/admin映射到/permitAll列表,而不是/#/admin規則,因爲#/admin部分只是URL片段,和平時不服務器端的業務。

您必須在您的前端和後端之間定義一個API。通常以RESTful Web服務形式提供,並在/api/*路徑上提供服務。確保路徑,並讓前端只通過這些API與後端進行通話。

0

這是eaiser解決您的問題,

更新

.antMatchers(HttpMethod.GET, "/#/admin").hasRole("ADMIN") 

.antMatchers(HttpMethod.GET, "/#/admin").hasRole("ADMIN").anyRequest().authenticated() 

對於每一個匹配器,你總是需要permitAll()authenticated()它。

+0

這工作,但現在我應該登錄訪問所有的端點,即使他們之前可用 –

相關問題