1
我正在嘗試實現OAuth 2.0的授權代碼授權流程。但卡住了令牌請求中的驗證彈出問題。彈簧安全令牌請求需要身份驗證
這是我的代碼。
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
@Configuration 公共類SecurityConfig 擴展WebSecurityConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin").password("abc").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and().csrf().disable();
}
}
@Configuration
@EnableAuthorizationServer
public class AuthServerOAuth2Config
extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test")
.secret("test_secret")
.authorizedGrantTypes("authorization_code")
.scopes("write");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints)
throws Exception {
endpoints
.authorizationCodeServices(authorizationCodeServices())
.authenticationManager(authenticationManager)
.tokenStore(tokenStore())
.approvalStoreDisabled();
}
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Bean
protected AuthorizationCodeServices authorizationCodeServices() {
return new InMemoryAuthorizationCodeServices();
}
}
要獲得令牌我做以下步驟:
首先,它重定向我到一個登錄形式,在這裏我輸入用戶名和passord:管理ABC
- 然後問我是否允許提供的許可,我的「測試「客戶。
- 它重定向我「重定向URI」:http://localhost:8080?code=XXX
- 然後我複製代碼,並使用谷歌高級REST客戶端發送令牌請求:對http://localhost:9000/oauth/token?client_id=test&grant_type=authorization_code&code=XXX POST沒有任何標題。據我瞭解海報應該使用瀏覽器cookie。
- 由於令牌請求的結果,我看到一個彈出式窗口要求填寫用戶名和密碼,同時期望獲得訪問令牌作爲響應。
請幫我解決這個問題。我應該爲我的令牌請求添加一些標頭嗎?或者我的授權服務器配置不正確?