【问题标题】:Unauthorized error when using Spring Security and Angular使用 Spring Security 和 Angular 时出现未经授权的错误
【发布时间】:2017-05-24 17:52:21
【问题描述】:

我的前端基于 Angular 4,而我的后端基于带有 Spring Security 的 Spring Boot。 我将所有内容部署在一个 WAR 文件中。

我在 /src/main/resources 中创建了一个 static/landing 文件夹,然后将 Webpack 构建的 Angular 文件放入该文件夹中。

Angular 负责登录过程,因此我在 Spring Security 中创建了以下规则:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Bean
public WebMvcConfigurerAdapter mappingIndex() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
         registry.addViewController("/login").setViewName("forward:/landing/index.html");
        }
    };
   }

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    .addFilterBefore(new CORSFilter(),ChannelProcessingFilter.class)
    .antMatchers(/"login/**").permitAll()
    .anyRequest().authenticated();

不幸的是,当我尝试使用网络浏览器访问 /login 页面进行登录时,我总是收到 HTTP 状态代码 401(未经授权)。

如何实现以这种方式集成 Angular 应用程序?因为我的安全规则适用于 REST API。

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    .antMatchers(/"login/**").permitAll() 看起来不对,

    试试这个:

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .authorizeRequests()
                .antMatchers("/login/**").permitAll()
                .anyRequest().authenticated();
    
    }
    

    如果还是不行,添加到你的application.properties

    logging.level.org.springframework.security=trace
    logging.level.org.springframework.web=trace
    

    并发布输出

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 2019-08-09
      • 2013-02-13
      • 1970-01-01
      • 2022-11-12
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多