2016-11-29 146 views
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); 
        } 
       } 
      } 
     } 
    }; 
} 
} 

如何與訪問令牌中的資源服務器通信? 任何示例都會有所幫助。

回答

相關問題