【问题标题】:Spring boot / Java - Add basic auth with sessionSpring boot / Java - 使用会话添加基本身份验证
【发布时间】:2021-11-23 16:58:16
【问题描述】:

我的应用程序有 2 个现有的访问级别:

  1. 对于 String[] ALLOWED_WITHOUT_AUTHENTICATION 中存在的 URL,它是 permitAll()
  2. 其他所有内容均由oauth2Login() 进行身份验证。

应用程序也在使用SecurityContextRepository 类型的securityContext - CookieSecurityContextRepository,它存储CustomOidcUser 类型的用户。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and().authorizeRequests().antMatchers(Constants.ALLOWED_WITHOUT_AUTHENTICATION).permitAll()
        .and().securityContext().securityContextRepository(new CookieSecurityContextRepository(authCookieHelper))
        .and().authorizeRequests().antMatchers("/**").authenticated()
        .and().oauth2Login().userInfoEndpoint().oidcUserService(oidcUserService).customUserType(CustomOidcUser.class, "customUser")
        .and().defaultSuccessUrl(authSuccessURL, true).failureUrl(userNotAuthenticatedURL)
        .and().exceptionHandling().authenticationEntryPoint(getAuthenticationEntryPoint());
}

我想将 basicAuth 添加到一些 url String[] AUTHENTICATED_WITH_BASIC_AUTH。所以我添加了以下代码:

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and().authorizeRequests().antMatchers(Constants.ALLOWED_WITHOUT_AUTHENTICATION).permitAll()
        .and().securityContext().securityContextRepository(new CookieSecurityContextRepository(authCookieHelper))

        .and().authorizeRequests().antMatchers(Constants.AUTHENTICATED_WITH_BASIC_AUTH).authenticated() // Line added #1 
        .and().httpBasic() // Line added #2

        .and().authorizeRequests().antMatchers("/**").authenticated()
        .and().oauth2Login().userInfoEndpoint().oidcUserService(oidcUserService).customUserType(CustomOidcUser.class, "customUser")
        .and().defaultSuccessUrl(authSuccessURL, true).failureUrl(userNotAuthenticatedURL)
        .and().exceptionHandling().authenticationEntryPoint(getAuthenticationEntryPoint());
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("myUser")
            .password("{noop}1234")
            .roles("USER");
}

但是每当我尝试访问一个基本的身份验证保护 url 时,我都会收到错误:

java.lang.ClassCastException: org.springframework.security.core.userdetails.User cannot be cast to com.myapp.user.CustomOidcUser

我认为这是因为 session 正在尝试查找类型为 CustomOidcUser 的用户。

如何将 http basicAuth 从我现有的配置中分离出来,以使它们都能正常工作?

【问题讨论】:

    标签: java spring spring-boot basic-authentication


    【解决方案1】:

    一种解决方案是在您的 SecurityConfiguration 类中使用两种不同的安全配置

    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Configuration
        @Order(1)
        public class BasicAuthSecurity extends WebSecurityConfigurerAdapter {
           @Override 
           public void configure(HttpSecurity http) throws Exception {
              [basic HTTP auth configuration here]
           }
           [...]
        }
    
    
        @Configuration
        @Order(2)
        public class OAuthSecurity extends WebSecurityConfigurerAdapter {
           @Override 
           public void configure(HttpSecurity http) throws Exception {
              [OAuth configuration here]
           }
           [...]
        }
    }
    

    这样Spring 将与two different authentication system 一起使用。

    另一种解决方案是使用两个角色,如下所示https://stackoverflow.com/a/50062043/10691359

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2019-04-25
      • 2014-09-22
      • 2017-07-31
      • 2018-01-22
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多