如何在春季使用OAuth2中的刷新令牌更新访问令牌? [英] How to renew access token with the refresh token in oauth2 in spring?
问题描述
我对Spring非常陌生,这是我第一次尝试使用OAuth2来实现Spring安全。我已经使用Spring安全实现了OAuth2,并且我确实获得了访问令牌和刷新令牌。但是,在发送刷新令牌以获取新的访问令牌时,我得到了"o.s.s.o.Provider.endpoint.TokenEndpoint-IlLegalStateException,需要UserDetailsService。"
其他用户类似问题的解决方案似乎是将UserDetailsService与终结点连接。
因此我执行了同样的操作,现在,当我尝试使用GRANT_TYPE:REFRESH_TOKEN和REFRESH_TOKEN:令牌以及客户端ID和密码发送请求时,我收到了找不到用户的错误。
请参考下面的WebSecurityConfigurity类:
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private UserDetailsService customUserDetailsService;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean ();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailsService)
.passwordEncoder(encoder());
}
@Override
protected void configure (HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/login**")
.permitAll()
.anyRequest()
.authenticated();
}
public PasswordEncoder encoder() {
return NoOpPasswordEncoder.getInstance();
}
}
请参考下面的AuthorizationServerConfiguration类:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private DataSource dataSource;
@Autowired
private CustomUserDetailsService userDetailsService;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.tokenStore(tokenStore());
.userDetailsService(userDetailsService);
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
}
请参考下面的ResourceServerConfiguration类:
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter{
@Autowired
DataSource dataSource;
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId("scout").tokenStore(tokenStore());
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests (). antMatchers ("/oauth/token", "/oauth/authorize **").permitAll();
// .anyRequest (). authenticated ();
http.requestMatchers (). antMatchers ("/api/patients/**") // Deny access to "/ private"
.and (). authorizeRequests ()
.antMatchers ("/api/patients/**"). access ("hasRole ('PATIENT')")
.and (). requestMatchers (). antMatchers ("/api/doctors/**") // Deny access to "/ admin"
.and (). authorizeRequests ()
.antMatchers ("/api/doctors/**"). access ("hasRole ('DOCTOR')");
}
}
CustomUserDetailsService类以供参考(如果需要):
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private UsersRepository userRepository;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Optional<Users> usersOptional = userRepository.findByEmail(email);
Users user = null;
if(usersOptional.isPresent()) {
System.out.println(usersOptional.isPresent());
user = usersOptional.get();
}else {
throw new RuntimeException("Email is not registered!");
}
return new CustomUserDetails(user);
}
}
我认为,服务器应该只检查刷新令牌的有效性,因为我们不会将用户详细信息与刷新令牌一起传递。所以我不知道为什么它一开始就需要用户详细信息。
如果我遗漏了什么,请帮助和引导! 提前谢谢。
推荐答案
我不确定。但是,正如我在WebSecurityConfiguration中看到的,您的代码可以连接默认的InMemoyUserDetailsManager UserDetailsService。这可能是您有两个不同的提供程序的原因。其中一个是你写的,另一个是读用户的。请尝试更改您的代码,如下所示,并让我知道它是否有帮助:
是:
@Autowired
private UserDetailsService customUserDetailsService;
我的愿景应该是:
@Autowired
private CustomUserDetailsService customUserDetailsService;
这篇关于如何在春季使用OAuth2中的刷新令牌更新访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!