【发布时间】:2019-06-11 23:44:45
【问题描述】:
为了尝试为我正在从事的项目开发通用身份服务,我需要在单个 spring-boot 应用程序中支持 UI 页面的基于 Azure ADFS 的 SAML 安全性和我的 REST API 的基于 Oauth2 的安全性
我有一个 Spring Boot 应用程序,它试图为我的一些 UI 资源提供基于 SAML 的保护,并为我的 REST API 提供基于 Oauth2 的保护。 UI 页面和 REST API 托管在同一个基于 Spring Boot 的应用程序中。我有一个扩展 WebSecurityConfigurerAdapter 并包含我的 SAML 配置的 SecurityConfiguration 类。我还有一个 ResourceServerConfig 类,它扩展了 ResourceServerConfigurerAdapter,我尝试在其中为我的 REST API 配置 Oauth2 身份验证。
这是我的代码的样子
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
/////// Other methods //////
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().anyRequest().authenticated();
http.httpBasic().authenticationEntryPoint(samlEntryPoint());
http.csrf().disable();
http.addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class).addFilterAfter(samlFilter(), BasicAuthenticationFilter.class);
http.authorizeRequests().antMatchers("/oauth/token").permitAll().anyRequest().authenticated();
}
ResourceServerConfig 类如下所示
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter
{
@Override
public void configure(HttpSecurity http) throws Exception
{
http
.csrf().disable()
.anonymous().and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers("/api/**").authenticated();
}
我的问题是,通过上述配置,我可以在我的 UI 页面上获得 SAML 保护或在我的 API 上获得 Oauth2 保护,但不能同时获得两者。如果我从 ResourceServerConfig 类中删除 @EnableResourceServer 并尝试访问我的 UI 页面之一,我会可靠地重定向到 microsoft Azure 登录页面,该页面会在成功验证后将我重定向回我的 UI 页面。但是有了这个,任何访问我的 api(使用有效的不记名令牌)的尝试都会导致重定向到 microsoft Azure 登录页面。如果我重新启用 @EnableResourceServer,我的 API 会受到保护并按预期运行,但所有 UI 页面的 SAML 保护都会完全禁用,spring 允许不受阻碍地访问所有 UI 资源。
我不知道如何告诉 spring boot 为我拥有的两种 URL 模式使用哪个身份验证框架。这样的事情有可能吗?
对此的任何帮助将不胜感激。
问候,
迪帕克贾
【问题讨论】:
标签: spring-boot authentication