Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. package com.proj.spring;
  2.  
  3. import javax.sql.DataSource;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.PropertySource;
  10. import org.springframework.core.env.Environment;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.jdbc.datasource.init.DataSourceInitializer;
  13. import org.springframework.jdbc.datasource.init.DatabasePopulator;
  14. import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
  15. import org.springframework.security.authentication.AuthenticationManager;
  16. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  17. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  18. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  19. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  20. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  21. import org.springframework.security.oauth2.provider.token.TokenStore;
  22. import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
  23.  
  24. @Configuration
  25. @PropertySource({ "classpath:persistence.properties" })
  26. @EnableAuthorizationServer
  27. public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  28.  
  29. @Autowired
  30. private Environment env;
  31.  
  32. @Autowired
  33. private AuthenticationManager authenticationManager;
  34.  
  35. @Autowired
  36. DataSource dataSource;
  37.  
  38. @Value("classpath:schema.sql")
  39. private Resource schemaScript;
  40.  
  41. @Override
  42. public void configure(AuthorizationServerSecurityConfigurer oauthServer)
  43. throws Exception {
  44. oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
  45. }
  46.  
  47. @Override
  48. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  49. clients.jdbc(dataSource)
  50. .withClient("sampleClientId")
  51. .authorizedGrantTypes("implicit")
  52. .scopes("read")
  53. .autoApprove(true)
  54. .and()
  55. .withClient("clientIdPassword")
  56. .secret("secret")
  57. .authorizedGrantTypes("password","authorization_code", "refresh_token")
  58. .scopes("read");
  59. }
  60.  
  61. @Override
  62. public void configure(AuthorizationServerEndpointsConfigurer endpoints)
  63. throws Exception {
  64. endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
  65. }
  66.  
  67. @Bean
  68. public TokenStore tokenStore() {
  69. return new JdbcTokenStore(dataSource);
  70. }
  71.  
  72. @Bean
  73. public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
  74. DataSourceInitializer initializer = new DataSourceInitializer();
  75. initializer.setDataSource(dataSource);
  76. initializer.setDatabasePopulator(databasePopulator());
  77. return initializer;
  78. }
  79.  
  80. private DatabasePopulator databasePopulator() {
  81. ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
  82. populator.addScript(schemaScript);
  83. return populator;
  84. }
  85. }
  86.  
  87. package com.proj.spring;
  88.  
  89. import org.springframework.beans.factory.annotation.Autowired;
  90. import org.springframework.context.annotation.Bean;
  91. import org.springframework.context.annotation.ComponentScan;
  92. import org.springframework.context.annotation.Configuration;
  93. import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  94. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  95. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  96. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  97. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  98. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  99. import org.springframework.security.config.http.SessionCreationPolicy;
  100. import org.springframework.security.core.userdetails.UserDetailsService;
  101. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  102. import org.springframework.security.crypto.password.PasswordEncoder;
  103. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  104. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  105. import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
  106. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  107.  
  108. import com.proj.security.CustomAuthenticationEntryPoint;
  109.  
  110. @Configuration
  111. @ComponentScan(basePackages = { "com.orangelabs.smp.security" })
  112. // @ImportResource({ "classpath:webSecurityConfig.xml" })
  113. @EnableWebSecurity
  114. public class SecSecurityConfig extends WebSecurityConfigurerAdapter {
  115.  
  116. @Autowired
  117. private UserDetailsService userDetailsService;
  118.  
  119. @Autowired
  120. private AuthenticationSuccessHandler myAuthenticationSuccessHandler;
  121.  
  122. @Autowired
  123. private LogoutSuccessHandler myLogoutSuccessHandler;
  124.  
  125. @Autowired
  126. private AuthenticationFailureHandler authenticationFailureHandler;
  127. @Autowired
  128. private CustomAuthenticationEntryPoint customAuthenticationEntryPoint;
  129.  
  130. public SecSecurityConfig() {
  131. super();
  132. }
  133. @Autowired
  134.  
  135. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  136.  
  137. auth
  138.  
  139. .userDetailsService(userDetailsService)
  140.  
  141. .passwordEncoder(encoder());
  142.  
  143. }
  144.  
  145. @Override
  146. public void configure(final WebSecurity web) throws Exception {
  147. web.ignoring().antMatchers("/resources/**");
  148. }
  149.  
  150. @Override
  151. protected void configure(final HttpSecurity http) throws Exception {
  152. // @formatter:off
  153. http
  154. .csrf().disable()
  155. .authorizeRequests()
  156. .antMatchers("/login*","/login*", "/logout*", "/signin/**", "/signup/**",
  157. "/user/registration*", "/regitrationConfirm*", "/expiredAccount*", "/registration*",
  158. "/badUser*", "/user/resendRegistrationToken*" ,"/forgetPassword*", "/user/resetPassword*",
  159. "/user/changePassword*", "/emailError*", "/resources/**","/old/user/registration*","/successRegister*").permitAll()
  160. .antMatchers("/invalidSession*").anonymous()
  161. .anyRequest().authenticated()
  162. .and()
  163. .formLogin()
  164. .loginPage("/login")
  165. .defaultSuccessUrl("/homepage.html")
  166. .failureUrl("/login?error=true")
  167. .successHandler(myAuthenticationSuccessHandler)
  168. .failureHandler(authenticationFailureHandler)
  169. .permitAll()
  170. .and()
  171. .sessionManagement()
  172. .invalidSessionUrl("/invalidSession.html")
  173. .sessionFixation().none()
  174. .and()
  175. .logout()
  176. .logoutSuccessHandler(myLogoutSuccessHandler)
  177. .invalidateHttpSession(false)
  178. .logoutSuccessUrl("/logout.html?logSucc=true")
  179. .deleteCookies("JSESSIONID")
  180. .permitAll();
  181.  
  182. // @formatter:on
  183. }
  184.  
  185. // beans
  186.  
  187. @Bean
  188. public DaoAuthenticationProvider authProvider() {
  189. final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
  190. authProvider.setUserDetailsService(userDetailsService);
  191. authProvider.setPasswordEncoder(encoder());
  192. return authProvider;
  193. }
  194.  
  195. @Bean
  196. public PasswordEncoder encoder() {
  197. return new BCryptPasswordEncoder(11);
  198. }
  199.  
  200. }
  201.  
  202. curl -vu clientIdPassword:secret -X POST 'http://localhost:8080/proj/oauth/token?grant_type=password&username=test@test.com&password=test'
  203.  
  204. curl -vu clientIdPassword:secret -X POST 'http://localhost:8080/proj/oauth/token?grant_type=refresh_token&refresh_token=7ec31746-6253-4f25-b825-be45a6239257'
  205.  
  206. curl -i -H "Authorization: Bearer 41adf8cf-bb92-4552-a5d6-5ab49ca6c7d7" http://localhost:8080/dsn-smp/api/test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement