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

如何在Spring Boot中设置基于头部的预身份验证?

曾珂
2023-03-14

我的应用程序从Oracle Access Manager SSO获取一个带有用户名的AUTH_用户请求标头。Spring Security“附加主题”2.2。1有一个“PreAuth”的示例,这似乎是我需要的,但不是一个完整的工作示例。

下面的片段来自文档/示例,而不是基于注释的配置。

Siteminder示例配置-使用带有RequestHeaderAuthenticationFilter和PreAuthenticationDauthenticationProvider的XML以及UserDetails服务来查找用户。

这如何映射到基于Java的配置?

<security:http>
  <!-- Additional http configuration omitted -->
  <security:custom-filter position="PRE_AUTH_FILTER" ref="siteminderFilter" />
</security:http>

<bean id="siteminderFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter">
  <property name="principalRequestHeader" value="AUTH_USER"/>
  <property name="authenticationManager" ref="authenticationManager" />
</bean>

<bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.    PreAuthenticatedAuthenticationProvider">
  <property name="preAuthenticatedUserDetailsService">
    <bean id="userDetailsServiceWrapper"
          class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
      <property name="userDetailsService" ref="userDetailsService"/>
    </bean>
  </property>
</bean>

<security:authentication-manager alias="authenticationManager">
   <security:authentication-provider ref="preauthAuthProvider" />
</security:authentication-manager>

SpringSecurityPreauth示例有一个完全不同的设置(XML配置甚至更吓人)。未提及预验证过滤器或如何设置标题名称。

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/login","/resources/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .jee()
                .mappableRoles("USER","ADMIN");
    }
}

spring boot示例web secure扩展了WebMVCConfigureAdapter而不是WebSecurityConfigureAdapter,只进行基于表单的基本登录,没有关于如何从预授权用户头获取用户ID的信息。

public class SampleWebSecureApplication extends WebMvcConfigurerAdapter {

... omitted...

    @Bean
    public ApplicationSecurity applicationSecurity() {
        return new ApplicationSecurity();
    }

    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

        @Autowired
        private SecurityProperties security;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin()
                    .loginPage("/login").failureUrl("/login?error").permitAll();
        }
    }

}

我读过很多参考文献/文章,但它们似乎与当前代码和Spring boot无关,因此我一直在努力理解如何配置应用程序预验证安全性。

共有1个答案

太叔志文
2023-03-14

这是我基于注入的userService配置预验证安全性的方法:

@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true, securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    private static final Logger LOG = Logger.getLogger(ApplicationSecurity.class.getName());

    @Autowired
    private UserService userService; // implements AuthenticationUserDetailsService...

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        LOG.info("configure autentication provider with custom userService");
        PreAuthenticatedAuthenticationProvider paaProvider = new PreAuthenticatedAuthenticationProvider();
        paaProvider.setPreAuthenticatedUserDetailsService(userService);
        auth.authenticationProvider(paaProvider);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        LOG.info("configure autentication filter");
        // ...
    }

}
 类似资料:
  • 在ASP.NET Core 1.x中,我可以在Configure中使用身份验证方法,但现在在ASP.NET Core 2.0中,我必须在ConfigureSreservices中设置所有内容,而不能在ConfigureMethod中配置。例如:

  • 我是角的新手。我正在尝试使用基本的身份验证标头向服务器发送GET请求,但它不起作用。这是我的代码, 当我从控制器调用服务时,浏览器的控制台中显示了2个错误, 选项http://xxx.xxx.xxx.xxx/api/getusers XMLHttpRequest无法加载http://xxx.xxx.xxx.xxx/api/getusers.请求的资源上不存在“访问控制允许源”标头。起源http:/

  • 在斯威格中有一个.BasicAuth(“基本”)。我取消注释的描述(“基本HTTP授权”)行。 但是,当尝试使用其中一个endpoint时,单击“试用!”按钮时,系统会提示我登录。无论我在这个框中放了什么(用户名和密码框),我都无法运行或测试endpoint,因为我未经授权。我需要在 SwaggerConfig.cs 文件或其他位置执行哪些操作才能进行测试? 我使用的是.Net Framework

  • 问题内容: 我不确定如何发送HTTP Auth标头。 我有以下HttpClient来获取请求,但不确定如何发送请求? 问题答案: HttpClient 文档及其示例代码中对此进行了介绍。

  • 所以我正忙着为twitter开发一个应用程序,我希望人们使用PIN进行身份验证。(https://dev.twitter.com/oauth/pin-based). 要向人们显示PIN码,我需要使用以下命令:https://dev.twitter.com/oauth/reference/get/oauth/authorize 那里的例子说:https://api.twitter.com/oauth

  • 我在本地机器上设置了一个独立的CAS5实例进行修补/黑客攻击,并试图将其设置为部署到Tomcat的独立WAR文件,并使用简单的基于文件的身份验证。 从WAR覆盖模板(https://github.com/apereo/cas-overlay-template)开始,我添加了以下依赖项,如https://apereo.github.io/cas/5.0.x/installation/whitelis