【问题标题】:Oauth "An Authentication object was not found in the SecurityContext"Oauth“在 SecurityContext 中找不到身份验证对象”
【发布时间】:2014-12-20 18:33:41
【问题描述】:

我正在使用 this code 作为指南,使用 Spring Server 设置 Oauth 安全配置。我已将 ResourceServerConfigurationAdapter 修改为如下所示,基本上是添加一个类以允许匿名获取我的 API 路径。

protected static class ResourceServer extends
        ResourceServerConfigurerAdapter {

    // This method configures the OAuth scopes required by clients to access
    // all of the paths in the video service.
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();

        http.authorizeRequests()
            .antMatchers("/oauth/token").anonymous();

        http.authorizeRequests()
            .antMatchers(HttpMethod.GET,"/gift/**").anonymous();

        //Other additions will be made that require username/password combinations, but I want to start simple first
    }

我通过网络浏览器访问服务器的/gift 文件夹,并看到此错误:

<oauth>
    <error_description>
        An Authentication object was not found in the SecurityContext
    </error_description>
    <error>unauthorized</error>
</oauth>

我的服务器控制台日志显示:

2014-10-24 16:48:32.895  WARN 8908 --- [io-8443-exec-10] o.s.c.s.ResourceBundleMessageSource      : ResourceBundle [messages] not found for MessageSource: Can't find bundle for base name messages, locale en_US
2014-10-24 16:48:32.895  INFO 8908 --- [io-8443-exec-10] o.s.b.a.audit.listener.AuditListener     : AuditEvent [timestamp=Fri Oct 24 16:48:32 EDT 2014, principal=<unknown>, type=AUTHENTICATION_FAILURE, data={message=An Authentication object was not found in the SecurityContext, type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException}]

谷歌搜索错误导致this site,上面写着

这是第一次出现的另一个调试级别消息 匿名用户试图访问受保护的资源,但是当您 过滤器链中没有 AnonymousAuthenticationFilter 配置。

但是,由于我永远无法建立连接,因此似乎存在根本性错误。此外,我没有丝毫线索可以将AnonymousAuthenticationFilter 放在我的过滤器链配置中,因为我什至不知道在哪里包含过滤器链配置。我该怎么办?

【问题讨论】:

    标签: java spring oauth spring-security


    【解决方案1】:

    您需要在 web.xml 文件中添加以下过滤器并尝试使用新的 tomcat

    <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>        
    <listener>
      <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
      </listener>
    

    【讨论】:

      【解决方案2】:

      你可以试试这个代码:

          @Override
      public void configure(HttpSecurity http) throws Exception {
          http.csrf().disable()
              .anonymous().and()
              .authorizeRequests()
              .antMatchers("/oauth/token").permitAll()
              .antMatchers(HttpMethod.GET,"/gift/**").permitAll();
      }
      

      【讨论】:

        猜你喜欢
        • 2017-09-01
        • 2017-08-03
        • 2020-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-17
        • 2015-02-02
        • 2013-05-09
        相关资源
        最近更新 更多