2016-11-30 41 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

这工作,但现在我应该登录访问所有的端点,即使他们之前可用 –