【问题标题】:Spring Security authenticated() doesn't work with AngularJSSpring Security authenticated() 不适用于 AngularJS
【发布时间】:2017-08-07 17:22:55
【问题描述】:

目前我在使用 Spring Security 和连接的 Angular 时遇到了一些问题。我在一个类中配置了 Spring Security,如下所示:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
            .antMatchers(HttpMethod.OPTIONS, "/**")
            .antMatchers("/app/**/*.{js,html}")
            .antMatchers("/static/node_modules/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/auth").permitAll()
            .antMatchers("/", "/login").permitAll()
            .antMatchers("/resources/templates/index.html").permitAll()
            .antMatchers("/api/client, /api/reservation", "/api/reservationreminder", "/api/review", "/api/user", "/api/vehicle", "/api/wash", "/api/washlocation", "/api/washtype", "/api/worker").hasAuthority(AuthoritiesConstants.CLIENT)
            .antMatchers("/static/app/styles", "/static/app/js").permitAll()
            .anyRequest().authenticated()
            .and()
        .logout()
            .logoutSuccessUrl("/static/views/pages/login.html")
            .permitAll()
            .and()
        .formLogin()
            .loginProcessingUrl("/static/views/pages/login.html")
            .permitAll()
}

每次我尝试运行我的应用程序时,我都会在浏览器中失败:

但是当我扔掉.anyRequest().authenticated() 时,一切似乎都正常了——这样我就可以在浏览器中看到我的应用程序了。在该示例中不起作用的事情是 Spring Security 不起作用 - 我的意思是尽管我每个站点都有角色,但我可以从角色/用户访问这些站点,理论上我不能......

【问题讨论】:

    标签: angularjs spring spring-boot spring-security


    【解决方案1】:

    您的 HTML 文件中静态资源的 URL 错误,请参阅 Spring Boot Reference

    默认情况下,资源映射到 /**,但您可以通过 spring.mvc.static-path-pattern 进行调整。

    从您的 HTML 文件和 Spring Security 配置中的 URL 中删除 /static

    【讨论】:

      【解决方案2】:

      我使用带有 Angular 的 Spring Boot(在 resource/static 文件夹中),配置:

      .antMatchers("/css/**", "/js/**", "/img/**", "/fonts/**").permitAll()
      .antMatchers("/index.html", "/")
      

      我怀疑你不需要static 前缀。

      【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2015-11-03
      • 2015-10-02
      • 1970-01-01
      • 2014-11-29
      • 2020-12-02
      • 2016-04-16
      • 2016-03-13
      • 2020-08-21
      相关资源
      最近更新 更多