当前位置: 首页 > 知识库问答 >
问题:

使用Spring Security和Angular时出现未经授权的错误

牛凌
2023-03-14

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

我在/src/main/resources中创建了一个静态/登陆文件夹,然后我将生成角度文件的网页包放在该文件夹中。

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 Apis中运行良好。

共有1个答案

谭宏盛
2023-03-14

<代码>。antMatchers(/“login/**”)。permitAll()看起来有误,

试试这个:

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

}

如果仍然不起作用,请添加到应用程序中。属性

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

和post输出

 类似资料:
  • 我们使用Spring Security OAuth2保护我们的REST服务(用于服务器到服务器通信,不涉及用户)。但是,当您尝试访问浏览器中的受保护资源时,它将显示: 我们希望这是我们自己选择的自定义页面。有办法吗? 设置“拒绝访问”页面不起作用。首先,它需要定义一个登录页面,我们没有,因为这是一个纯服务器到服务器的通信。另一个原因是,这个属性自Spring 3.0..或类似的版本以来就被弃用了。

  • 在jHipster中是否可以实现这一特定功能,以使身份验证能够从浏览器或应用程序(不在jHipster服务器上运行)工作?

  • 问题内容: 我从Nexus存储库中检出了代码。我更改了帐户密码,并在文件中正确设置了密码。在执行时,我收到错误消息,说明它尝试从该存储库下载文件。 任何想法如何解决此错误?我在Maven 3.04中使用Windows 7 问题答案: 这里的问题是所使用的密码出现错字错误,由于密码中使用了字符/字母,因此很难识别。

  • 我的rest服务中没有Spring Security实现,当我试图调用rest中的资源时,我面临CORS 401未授权问题。 我对此感到愤怒: https://www.baeldung.com/spring-security-cors-preflight https://docs.spring.io/spring-security/site/docs/5.0.7.RELEASE/reference

  • 我试图将SonarCloud与Travis-CI一起使用,但遇到以下错误: *出了什么问题: 任务“:SonarQube”的执行失败。 您只被授权执行本地(预览)SonarQube分析,而不将结果推送到SonarQube服务器。请联系您的SonarQube管理员。 下面是我试图使用Travis设置配置项的Github项目。 下面是Travis构建的链接,该构建在执行SonarQube步骤时失败。

  • 问题内容: 尝试使用Jenkins运行Sonarqube MSBuild Runner时出现以下错误。我正在使用SonarQube 4.5和MSBuild SonarQube Runner 1.1。有关如何解决的任何建议? 问题答案: 您需要查看配置文件SonarQube.Analysis.xml。确认属性sonar.host.url是正确的,并且您已经配置了有效的sonar.login和sona