【发布时间】:2021-11-23 16:58:16
【问题描述】:
我的应用程序有 2 个现有的访问级别:
- 对于 String[]
ALLOWED_WITHOUT_AUTHENTICATION中存在的 URL,它是permitAll()。 - 其他所有内容均由
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