0
我有一个Spring Boot应用程序。我试图为该应用程序实现OAuth2授权。我遵循本教程https://spring.io/guides/tutorials/spring-boot-oauth2/启用授权服务器部分。虽然我成功地从auth-server获得访问令牌,但当我试图发送这些令牌来请求我的资源服务器时,它在控制台中出现错误未授权访问。OAuth2认证服务器和资源服务器使用Spring启动
org.springframework.security.access.AccessDeniedException: Access is denied
虽然我会&资源服务器分离后的两个授权服务器,为最初目的,对于单个应用程序会工作。
@Configuration
@EnableAuthorizationServer
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(Application.baseURL + "/user/register");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.and().exceptionHandling()
.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
.and().csrf().disable();
}
}
和用户authetication
@Configuration
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Loggable
private static Logger logger;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Session session = Hibernate.sessionFactory.openSession();
try {
UserPasswordDTO userPasswordDTO = new UserPasswordModel().getByEmailId(session, username);
return new SimsmisUser(username, userPasswordDTO.hashedPassword, true, true, true, true,
AuthorityUtils.createAuthorityList("USER"), userPasswordDTO.userId);
}
catch (InvalidIdException e) {
throw new UsernameNotFoundException(e.getMessage());
}
finally {
if (session != null) {
try {
session.close();
}
catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
}
};
}
}
如何与访问令牌中的资源服务器通信? 任何示例都会有所帮助。