gpt4 book ai didi

java - 无法通过 BasicAuth 登录

转载 作者:行者123 更新时间:2023-12-02 10:20:21 24 4
gpt4 key购买 nike

我正在尝试使用 token 实现oauth。一切看起来都很好,但是在 POST 之后

http://localhost:8080/oauth/token?grant_type=password

设置BasicAuth admin/admin(我在数据库中有用户,登录名admin,密码admin)

我得到了带有基本身份验证的窗口,当我写下登录名和密码时,我一次又一次地得到了带有基本身份验证的窗口。

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").authenticated();
}
}

.

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;

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

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("android-client")
.authorizedGrantTypes("client-credentials", "password","refresh_token")
.authorities("ROLE_CLIENT", "ROLE_ANDROID_CLIENT")
.scopes("read", "write", "trust")
.resourceIds("oauth2-resource")
.accessTokenValiditySeconds(5000)
.secret("android-secret").refreshTokenValiditySeconds(50000);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
}
}

.

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private UserRepository userRepository;

@Bean
public AuthenticationManager customAuthenticationManager() throws Exception {
return authenticationManager();
}

private PasswordEncoder encoder =
PasswordEncoderFactories.createDelegatingPasswordEncoder();

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(username -> {
Optional<User> user = userRepository.findById(username);
if (user.isPresent()) {
return new org.springframework.security.core.userdetails.User(user.get().getUsername(), encoder.encode(user.get().getPassword()),
true, true, true, true, AuthorityUtils.createAuthorityList("USER"));
} else {
throw new UsernameNotFoundException("Could not find the user '" + username + "'");
}
});
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic().and().csrf().disable();
}
}

非常重要:
如果我删除 ResourceServerConfig.java 我可以通过 BasicAuth 登录,在写入 admin/admin 之后,我从 localhost:8080 获得 JSON,但我想通过 token 访问。

这是我的第一个 RESTful API。
有人可以帮助我吗?有谁知道我哪里出错了?
网上的信息很少。我该如何修复它?

最佳答案

您需要发送您的 clientId:“android-client”和 secret “android-secret”作为基本身份验证凭据,而不是需要作为 http 参数发送的用户(admin/admin)凭据(用户名=管理员密码= admin) 就像“grant_type”参数

所以你的请求应该是这样的

http://localhost:8080/oauth/token?grant_type=password&username=admin&password=admin

将您的 clientId 和 key 添加到基本身份验证凭据中

关于java - 无法通过 BasicAuth 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54387548/

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