在 Spring 引导 OAuth2 授权服务器中使用 Active Directory 身份验证

Use Active Directory Authentication in Spring Boot OAuth2 Authorization Server

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

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

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

与我的主要区别在于我省略了所有 AngularJS 内容。

我有以下服务:

我想要发生的事情是这样的:

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

我可以让这一切在 auth 服务器上使用基本身份验证正常工作。但是我希望能够用 Active Directory 中的基本身份验证替换基本身份验证。

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

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

这是我的身份验证服务器应用程序(添加了 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

看起来登录端点现在需要不记名令牌?我不确定现在如何进行...

任何 help/advice 将不胜感激...

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