3

我正在尝试使用 Spring Boot 和 OAuth2 设置概念证明。我已经建立了一些与此处概述的项目非常相似的项目:

https://spring.io/guides/tutorials/spring-boot-oauth2/

在这里:https ://spring.io/guides/tutorials/spring-security-and-angular-js/

与我的主要区别是我遗漏了所有 AngularJS 的东西。

我有以下服务:

  • 授权服务器
  • 资源服务器(作为受保护的 OAuth2 客户端)
  • UI 服务器(作为受保护的 OAuth2 客户端)

我想要发生的只是:

  1. 访问 UI 服务器
  2. 重定向到身份验证服务器并提示输入凭据
  3. 然后 UI 服务器将从资源服务器获取一些文本并显示它们

我可以通过身份验证服务器上的基本身份验证使这一切正常工作。但是,我希望能够将基本身份验证替换为 Active Directory 中的身份验证。

我有几个其他的 Spring Boot 项目可以进行 AD 身份验证,并且可以正常工作,但是每当我尝试将其放入其中时,都会出错。我认为这与身份验证服务器端点周围的安全性有关,但我不确定是什么。

另外,我不清楚在生产环境中哪些端点应该通过什么协议(OAuth2 v. Basic)来保护?文档建议应使用 Basic 保护某些端点。所有“OAuth2 客户端”都应该以某种方式在他们的请求中包含这些凭据吗?

这是我的身份验证服务器应用程序(添加了 Active Directory 位):

@EnableResourceServer
@EnableAuthorizationServer
@SpringBootApplication
@RestController
public class AuthServerLdapApplication {

public static void main(String[] args) {
    SpringApplication.run(AuthServerLdapApplication.class, args);
}

@RequestMapping("/user")
public Principal user(Principal user) {
    return user;
}

@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
@Configuration
protected static class ActiveDirectoryConfig extends WebSecurityConfigurerAdapter {

    @Value("${activedirectory.url}")
    private String activeDirectoryUrl;

    @Value("${activedirectory.domain}")
    private String getActiveDirectoryDomain;

    @Autowired private AuthenticationManager authenticationManager;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(getActiveDirectoryDomain,
                activeDirectoryUrl);
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);

        auth.authenticationProvider(provider);
        auth.parentAuthenticationManager(authenticationManager);
    }
}

}

基本上然后发生的事情是当我点击 UI 服务器时我得到了这个:

<oauth>
  <error_description>
    Full authentication is required to access this resource
  </error_description>
  <error>unauthorized</error>
</oauth>

如果我对授权服务器执行此操作:

curl -v http://localhost:9004/uaa/login
*   Trying ::1...
* Connected to localhost (::1) port 9004 (#0)
> GET /uaa/login HTTP/1.1
> Host: localhost:9004
> User-Agent: curl/7.44.0
> Accept: */*
>
< HTTP/1.1 401 Unauthorized
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Cache-Control: no-store
< Pragma: no-cache
< WWW-Authenticate: Bearer realm="null", error="unauthorized",    error_description="Full authentication is required to access this resource"
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 01 Dec 2015 12:38:53 GMT
<
{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}* Connection #0 to host localhost left intact

看起来登录端点现在需要一个不记名令牌?我不知道现在如何进行...

任何帮助/建议将不胜感激...

4

1 回答 1

2

但是您@EnableResourceServer没有为要保护的资源提供任何规则,因此它试图保护一切。您需要添加一个@Bean类型ResourceServerConfigurerAdapter并设置请求匹配器(以及您想要的任何其他规则)。

于 2015-12-02T19:26:25.087 回答