大佬教程收集整理的这篇文章主要介绍了使用 HTTP Basic 和 OIDC Bearer Token 的 Spring Security 身份验证,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个使用 Spring Boot 2.4.1 和 Spring Security 5.4.2 的 Web 应用程序,我需要提供 http 基本身份验证和不记名令牌身份验证(JWT 访问令牌从 SPA 发送给每个 API称呼)。 所有 API URL 都以路径 /API 开头,并且必须使用 Bearer Token 进行身份验证,除了两个 URL(/API/func1 和 /API/func2 ) 是使用 http Basic 所必需的。
问题是,如果我为 http Basic 激活扩展 WebSecurityConfigurerAdapter 的类,则会跳过承载令牌身份验证。
@Configuration
@EnableWebSecurity
@ConditionalOnProperty(name = "auth.enable",matchIfMissing = truE)
@Order(1)
public class httpBasicSecurityConfiguration extends WebSecurityConfigurerAdapter {
private final RestBasicAuthEntryPoint authenticationEntryPoint;
private final Datasource datasource;
@autowired
public httpBasicSecurityConfiguration(final RestBasicAuthEntryPoint authenticationEntryPoint,final Datasource datasourcE) {
super();
this.authenticationEntryPoint = authenticationEntryPoint;
this.datasource = datasource;
}
@OverrIDe
protected voID configure(final httpSecurity http) throws Exception {
http.csrf().disable()
.authorizerequests().antMatchers({"/API/func1/**","/API/func2/**"}).authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint);
}
@OverrIDe
protected voID configure(final AuthenticationManagerBuilder auth) throws Exception {
// Usernames,passwords and roles are stored into USERS and AUTHORITIES tables
auth.jdbcAuthentication()
.passwordEncoder(passwordEncoder())
.datasource(datasourcE);
}
private passwordEncoder passwordEncoder() {
return passwordEncoderFactorIEs.createDelegaTingpasswordEncoder();
}
}
@Configuration
@EnableWebSecurity(deBUG=truE)
@ConditionalOnProperty(name = "auth.enable",matchIfMissing = truE)
@Order(2)
public class Oauth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
@autowired
public Oauth2SecurityConfiguration(CactusSystemConfiguration systemConfiguration) {
super();
this.systemConfiguration = systemConfiguration;
}
@OverrIDe
protected voID configure(final httpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizerequests()
.antMatchers(httpR_157_11845@ethod.oPTIONS,"/**").permitAll()
.antMatchers({"/ws/**","/API/mock/**"}).permitAll()
.and()
.authorizerequests().antMatchers("/API/**").authenticated()
.and()
.oauth2resourceServer().jwt();
}
}
当调用需要使用承载令牌进行身份验证的方法时,Spring Security 调试会打印以下信息:
request received for get '/API/genericFunc':
org.apache.catalina.connector.requestFacade@517df0fb
servletPath:/API/genericFunc
pathInfo:null
headers:
host: devBox:8080
user-agent: Mozilla/5.0 (windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 firefox/84.0
accept: */*
accept-language: en-US,en;q=0.5
accept-enCoding: gzip,deflate
authorization: Bearer eyJ0eXAiOiJ.....
connection: keep-alive
cookie: JsESSIONID=D3E8CED6AB13EFF0D15BFA6C69AFeed4; JsESSIONID=C923091B9B9E38A19106BC4F529F7D13
Security filter chain: [
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
headerWriterFilter
logoutFilter
BasicAuthenticationFilter
requestCacheAwareFilter
SecurityContextHolderAwarerequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
]
显然 @H_502_21@BearerTokenAuthenticationFilter 未加载。
如果我从运行中排除 httpBasicSecurityConfiguration 那么我得到:
Security filter chain: [
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
headerWriterFilter
logoutFilter
BearerTokenAuthenticationFilter
requestCacheAwareFilter
SecurityContextHolderAwarerequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
]
知道为什么会这样吗? 也许不可能在 Spring Security 中使用具有相同祖先路径(即 /API)的 API 使用两种不同的身份验证方法
发生这种情况是因为 httpBasicSecurityConfiguration
正在匹配所有请求,因此只有 /api/func1/**
和 /api/func2/**
会根据 http 基本身份验证进行检查,而其他人则不需要进行身份验证。此时跳过 Spring 安全过滤器链,并且永远不会触发另一个过滤器。
您需要限制应用第一个过滤器的请求。只需将 configure
中的 httpBasicSecurityConfiguration
方法更改为:
@Override
protected void configure(final httpSecurity http) throws Exception {
http.csrf().disable()
.requestMatchers().antMatchers("/api/func1/**","/api/func2/**")
.and()
.authorizerequests().anyrequest().authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint);
}
注意 .requestMatchers().antMatchers("/api/func1/**","/api/func2/**")
是在 .authorizerequests().anyrequest()
之前应用的,然后你可以使用任何请求,不需要再次匹配 url。
以上是大佬教程为你收集整理的使用 HTTP Basic 和 OIDC Bearer Token 的 Spring Security 身份验证全部内容,希望文章能够帮你解决使用 HTTP Basic 和 OIDC Bearer Token 的 Spring Security 身份验证所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。