我的應用程序中出現錯誤“未經授權”的問題。我正在使用 Spring Security 和 oauth2。我的客戶和用戶存儲在數據庫中。當我開始使用數據庫中的客戶端時,PostMan 中出現錯誤 401??蛻舳苏诒4嬖跀祿熘?,但是當我想從 localhost:8080/oauth/token 獲取令牌訪問權限時仍然出現錯誤。以下是我的來源:授權服務器配置:公共類 AuthorizationServerConfig 擴展 AuthorizationServerConfigurerAdapter {@Autowiredprivate AuthenticationManager authenticationManager;@Autowiredprivate TokenStore tokenStore;private CustomClientDetailsService customClientDetailsService;@BeanPasswordEncoder passwordEncoder() { return PasswordEncoderFactories.createDelegatingPasswordEncoder();}@Overridepublic void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()");}@Overridepublic void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.withClientDetails(customClientDetailsService);}@Overridepublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints .tokenStore(tokenStore) .authenticationManager(authenticationManager);}}這是我的 CustomClientDetails :公共類 CustomClientDetails 實現 ClientDetails {final static Logger log = LoggerFactory.getLogger(CustomClientDetailsService.class);private static final long serialVersionUID = 6602529451366778198L;private Clients clients;public CustomClientDetails(final Clients clients){ this.clients = clients;}@Overridepublic String getClientId() { return clients.getClientId();}@Overridepublic Set<String> getResourceIds() { final Set<String> resourcesIds = new HashSet<String>(); resourcesIds.add(clients.getResourceIds()); return resourcesIds;}@Overridepublic boolean isSecretRequired() { return true;}@Overridepublic String getClientSecret() { return clients.getClientSecret();}@Overridepublic boolean isScoped() { return true;}@Overridepublic Set<String> getScope() { final Set<String> scopes = new HashSet<String>(); scopes.add(clients.getScope()); return scopes;}
2 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
您應該在 postman 中提供 aclient_id
和 a client_secret
,在授權部分,您可以設置 Basic Auth。
在username
字段中,輸入您的client_id
和 在 中password
,輸入您的client_secret
.

搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
“/oauth/token”中的“未經授權”可能意味著您沒有HTTP Basic Auth
在請求標頭中提供憑據。據我所知,該端點默認情況下使用存儲在oauth_client_details
實體中的登錄名和密碼進行保護。查找client_id
+client_secret
對并將其提供給 Postman 并使用 Authorization->Basic Auth 設置。
添加回答
舉報
0/150
提交
取消