1

在我的Spring Boot應用程序中,我需要以編程方式創建新用戶並從內部(此應用程序的一部分)OAuth2授權服務器爲他獲取OAuth2訪問/刷新標記。Spring Security OAuth2 accessToken

然後,我打算將這些access/refresh tokens發送到一些外部(客戶端)應用程序,該應用程序將代表此用戶與我的第一個應用程序進行交互。

是否有可能在沒有提供密碼的情況下以編程方式獲取此用戶的OAuth2訪問/刷新標記(在編程創建此用戶期間,我不想處理password,僅限username)。

回答

1

是的,你可以看看下面的代碼

@Autowired 
private TokenEndpoint tokenEndpoint; 

public ResponseEntity<?> createToken(User user) { 

     Principal principal = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassword(), user.getAuthorities()); 

     HashMap<String, String> parameters = new HashMap<String, String>(); 
     parameters.put("client_id", "XXX"); 
     parameters.put("client_secret", "XXX"); 
     parameters.put("grant_type", "password"); 
     parameters.put("password", user.getPassword()); 
     parameters.put("scope", "XXX"); 
     parameters.put("username", user.getUserName()); 

     return tokenEndpoint.getAccessToken(principal, parameters); 
} 

但你違反了OAuth2規範。授權應由資源所有者執行。

相關問題