gpt4 book ai didi

java - Spring 启动 : error ="invalid_grant", error_description ="Bad credentials"

转载 作者:行者123 更新时间:2023-11-30 06:06:46 26 4
gpt4 key购买 nike

我写了一个非常简单的 Spring boot 应用程序。

为了获取身份验证 token ,我使用了以下 curl 命令。但是我在服务器日志(eclipse 控制台)中看到以下错误:error="invalid_grant", error_description="Bad credentials"

curl -v -u greetings:123456 -X POST http://localhost:8080/oauth/token -H "Accept: application/json" -d "username=username&password=password&grant_type=password&scope=write&client_secret=12345&client_id=greetings"

我想知道:

我做错了什么,它不让我得到auth code

OAuth2ServerConfiguration.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;


@Configuration
@EnableResourceServer
@EnableAuthorizationServer
class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {

String applicationName = "greetings";

// This is required for password grants, which we specify below as one of the
// {@literal authorizedGrantTypes()}.
@Autowired
AuthenticationManagerBuilder authenticationManager;

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
// Workaround for https://github.com/spring-projects/spring-boot/issues/1801
endpoints.authenticationManager(new AuthenticationManager() {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
// TODO Auto-generated method stub
return authenticationManager.getOrBuild().authenticate(authentication);
}
});
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

clients.inMemory()
.withClient(applicationName)
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.authorities("ROLE_USER")
.scopes("write")
.resourceIds(applicationName)
.secret("123456");
}
}

WebSecurityConfiguration.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.User;

import com.boot.myproj.repository.AccountRepository;

class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {

@Autowired
AccountRepository userRepository;

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService());
}

@Bean
UserDetailsService userDetailsService() {
return (username) -> userRepository
.findByUsername(username)
.map(a -> new User(a.username, a.password, true, true, true, true,
AuthorityUtils.createAuthorityList("USER", "write")))
.orElseThrow(
() -> new UsernameNotFoundException("could not find the user '"
+ username + "'"));
}
}

Account.java

import javax.persistence.*;


import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
public class Account {

@Id
@GeneratedValue
private Long id;

public Long getId() {
return id;
}

public String getPassword() {
return password;
}

public String getUsername() {
return username;
}

@JsonIgnore
public String password;
public String username;

public Account(String name, String password) {
this.username = name;
this.password = password;
}

Account() { // jpa only
}
}

AccountRepository.java

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.boot.myproj.config.security.Account;

public interface AccountRepository extends JpaRepository<Account, String>{
public Optional<Account> findByUsername(String username);
}

App.java

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.boot.cut_costs.config.security.Account;
import com.boot.cut_costs.repository.AccountRepository;

@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}

@Bean
CommandLineRunner init(AccountRepository accountRepository) {
return (arg) -> {
accountRepository.save(new Account("username", "password"));
};
}
}

最佳答案

@Configuration 添加到WebSecurityConfiguration 类。这样你的 bean 就会被注入(inject)到 spring 上下文中。

附注您在这里尝试使用 OAuth2 password grant 执行的操作不会返回 auth code ,而是直接返回给您 accessToken

关于java - Spring 启动 : error ="invalid_grant", error_description ="Bad credentials",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43423932/

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