Guest User

Untitled

a guest
Mar 20th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  4. private static String REALM="ALTEGIX_REALM";
  5. private static final int ONE_DAY = 60 * 60 * 24;
  6. private static final int THIRTY_DAYS = 60 * 60 * 24 * 30;
  7.  
  8. @Autowired
  9. private TokenStore tokenStore;
  10.  
  11. @Autowired
  12. private AltegixUserDetailsService altegixUsersDetailsService;
  13.  
  14.  
  15. @Autowired
  16. private UserApprovalHandler userApprovalHandler;
  17.  
  18. @Autowired
  19. @Qualifier("authenticationManagerBean")
  20. private AuthenticationManager authenticationManager;
  21.  
  22. @Override
  23. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  24. clients.inMemory()
  25. .withClient("xxxx")
  26. .secret("xxxxx")
  27. .authorizedGrantTypes("password", "refresh_token")
  28. .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
  29. .scopes("read", "write", "trust")
  30. //.accessTokenValiditySeconds(ONE_DAY)
  31. .accessTokenValiditySeconds(300)
  32. .refreshTokenValiditySeconds(THIRTY_DAYS);
  33. }
  34.  
  35. @Override
  36. public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
  37. endpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)
  38. .authenticationManager(authenticationManager)
  39. .userDetailsService(altegixUsersDetailsService);
  40. }
  41.  
  42. @Override
  43. public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  44. oauthServer.realm(REALM);
  45. }
  46. }
  47.  
  48. @Configuration
  49. @EnableGlobalMethodSecurity(prePostEnabled = true)
  50. public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
  51.  
  52. @Override
  53. protected MethodSecurityExpressionHandler createExpressionHandler() {
  54. return new OAuth2MethodSecurityExpressionHandler();
  55. }
  56. }
  57.  
  58. @Configuration
  59. @EnableResourceServer
  60. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  61.  
  62. @Override
  63. public void configure(HttpSecurity http) throws Exception {
  64. http.
  65. anonymous().disable()
  66. .requestMatchers().antMatchers("/**")
  67. .and().authorizeRequests().anyRequest().authenticated()
  68. .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
  69. }
  70.  
  71. }
  72.  
  73. @Configuration
  74. @EnableWebSecurity
  75. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  76.  
  77. @Autowired
  78. private ClientDetailsService clientDetailsService;
  79.  
  80. @Autowired
  81. private AltegixUserDetailsService altegixUsersDetailsService;
  82.  
  83. @Override
  84. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  85. auth.userDetailsService(altegixUsersDetailsService)
  86. .passwordEncoder(passwordEncoder());
  87. }
  88.  
  89. @Bean
  90. public PasswordEncoder passwordEncoder() {
  91. return new BCryptPasswordEncoder();
  92. }
  93.  
  94. /*@Autowired
  95. public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
  96. auth.inMemoryAuthentication()
  97. .withUser("admin").password("incarta165").roles("ADMIN","USER").and()
  98. .withUser("user").password("pegasus").roles("USER");
  99. }
  100. */
  101.  
  102. @Override
  103. @Order(Ordered.HIGHEST_PRECEDENCE)
  104. protected void configure(HttpSecurity http) throws Exception {
  105. http
  106. .sessionManagement()
  107. .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  108. .and()
  109. .csrf().disable()
  110. .authorizeRequests()
  111. .antMatchers("/about").permitAll()
  112. .antMatchers("/signup").permitAll()
  113. .antMatchers("/oauth/token").permitAll()
  114. //.antMatchers("/api/**").authenticated()
  115. //.antMatchers("/api/**").hasRole("USER")
  116. .anyRequest().authenticated()
  117. .and()
  118. .httpBasic()
  119. .realmName("ALTEGIX_REALM");
  120. }
  121.  
  122.  
  123. @Override
  124. @Bean
  125. public AuthenticationManager authenticationManagerBean() throws Exception {
  126. return super.authenticationManagerBean();
  127. }
  128.  
  129. @Bean
  130. public TokenStore tokenStore() {
  131. return new InMemoryTokenStore();
  132. }
  133.  
  134. @Bean
  135. @Autowired
  136. public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){
  137. TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
  138. handler.setTokenStore(tokenStore);
  139. handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
  140. handler.setClientDetailsService(clientDetailsService);
  141. return handler;
  142. }
  143.  
  144. @Bean
  145. @Autowired
  146. public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
  147. TokenApprovalStore store = new TokenApprovalStore();
  148. store.setTokenStore(tokenStore);
  149. return store;
  150. }
  151.  
  152. }
Add Comment
Please, Sign In to add comment