Guest User

Untitled

a guest
Dec 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. @Configuration
  2. public class OAuth2ServerConfig {
  3.  
  4. @Configuration
  5. @EnableResourceServer
  6. protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
  7.  
  8. @Inject
  9. private Http401UnauthorizedEntryPoint authenticationEntryPoint;
  10.  
  11. @Inject
  12. private AjaxLogoutSuccessHandler ajaxLogoutSuccessHandler;
  13.  
  14. @Override
  15. public void configure(HttpSecurity http) throws Exception {
  16. http
  17. .exceptionHandling()
  18. .authenticationEntryPoint(authenticationEntryPoint)
  19. .and()
  20. .logout()
  21. .logoutUrl("/logout")
  22. .logoutSuccessHandler(ajaxLogoutSuccessHandler)
  23. .and()
  24. .csrf()
  25. .requireCsrfProtectionMatcher(new AntPathRequestMatcher("oauth/token"))
  26. .disable()
  27. .headers()
  28. .frameOptions().disable()
  29. .and()
  30. .sessionManagement()
  31. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  32. .and()
  33. .authorizeRequests()
  34. .antMatchers("/admin").hasAnyAuthority("ADMIN");
  35. }
  36. }
  37.  
  38. @Configuration
  39. @EnableAuthorizationServer
  40. protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
  41.  
  42. private static final String CLIENTID = "app";
  43. private static final String PROP_SECRET = "secret";
  44. private static final Integer TOKEN_VALIDITY_SECONDS = -1;
  45. @Inject
  46. private UserDetailsService userDetailsService;
  47. @Inject
  48. private OAuth2AccessTokenRepository oAuth2AccessTokenRepository;
  49.  
  50. @Inject
  51. private OAuth2RefreshTokenRepository oAuth2RefreshTokenRepository;
  52.  
  53. @Bean
  54. public TokenStore tokenStore() {
  55. return new MongoDBTokenStore(oAuth2AccessTokenRepository, oAuth2RefreshTokenRepository);
  56. }
  57.  
  58. @Inject
  59. @Qualifier("authenticationManagerBean")
  60. private AuthenticationManager authenticationManager;
  61.  
  62. @Override
  63. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  64. throws Exception {
  65.  
  66. endpoints
  67. .tokenStore(tokenStore())
  68. .authenticationManager(authenticationManager).userDetailsService(userDetailsService);
  69. }
  70.  
  71. @Override
  72. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  73. clients
  74. .inMemory()
  75. .withClient(CLIENTID)
  76. .authorizedGrantTypes("authorization_code","refresh_token","password")
  77. .authorities("USER", "ADMIN")
  78. .secret(PROP_SECRET)
  79. .accessTokenValiditySeconds(TOKEN_VALIDITY_SECONDS);
  80. }
  81. }
  82. }
  83.  
  84. curl app:secret@localhost:8080/oauth/token -d grant_type=password -d client_id=app -d username=user -d password=password
Add Comment
Please, Sign In to add comment