gpt4 book ai didi

java - Spring Boot OAuth2 具有基本身份验证和自定义 UserDetailsS​​ervice

转载 作者:行者123 更新时间:2023-12-01 19:59:29 26 4
gpt4 key购买 nike

我正在尝试配置一个能够完成基本 OAuth2 流程的 OAuth2 服务器(有关示例,请参阅 here)。

对长问题表示歉意

我的第一次尝试是能够执行authorization_code流程。

我有以下配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig
extends WebSecurityConfigurerAdapter {

....

@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}

...

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login").permitAll()
.and()
.requestMatchers().antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests().anyRequest().authenticated();
}

...

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}

我的服务器配置

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorServerConfig
extends AuthorizationServerConfigurerAdapter {

@Inject
private TokenStore tokenStore;

@Inject
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.tokenStore(tokenStore)
.authenticationManager(authenticationManager);
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.allowFormAuthenticationForClients()
.checkTokenAccess("authenticated()");
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
.inMemory()
.withClient("foo")
.secret("foo")
.authorizedGrantTypes("authorization_code","password", "refresh_token")
.scopes(new String[] { "read", "write" })
}

事实上,我的授权代码流程工作正常!当我尝试执行如下密码流程时出现问题:

POST localhost:8200/oauth/token
Content-Type: application/x-www-form-urlencoded
Accept:application/json
Authorization:Basic Zm9vOmZvbw=="
username=admin&password=admin&grant_type=password&scope=read%20write&client_secret=foo&client_id=foo&

我的问题是授权 header 被忽略,无论它是否存在结果都是相同的,它给我返回了access_token和refresh_token

如果我尝试按如下方式启用基本身份验证,请启用 ClientUserDetailsS​​ervice,从数据库 JDBC 读取客户端:

public class SecurityConfig
extends WebSecurityConfigurerAdapter {

....

@Bean
public ClientDetailsUserDetailsService clientDetailsUserDetailsService(ClientDetailsService clientDetailsService){
// JDBC clientDetailsService
return new ClientDetailsUserDetailsService(clientDetailsService);
}


@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(clientDetailsUserDetailsService());
}

...

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
DaoAuthenticationProvider p = new DaoAuthenticationProvider();
p.setUserDetailsService(userDetailsService);
p.setPasswordEncoder(passwordEncoder());
return new ProviderManager(Lists.newArrayList(p));
// return super.authenticationManagerBean();
}

}

现在我所实现的是让基本身份验证正常工作,但我失去了authorization_code流程,因为基本身份验证现在是针对clientid和 secret 而不是用户的实际凭据完成的

我错过了什么吗?我怎样才能同时拥有这两种流量?请帮忙,我已经挣扎了好几天了。

一些类似的问题,但没有任何运气:

最佳答案

我认为这是因为您已经为客户端打开了表单例份验证,您正在使用它而不是基本 header 。

public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.allowFormAuthenticationForClients()
.checkTokenAccess("authenticated()");
}

取出.allowFormAuthenticationForClients()

关于java - Spring Boot OAuth2 具有基本身份验证和自定义 UserDetailsS​​ervice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com