Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. package com.dicardistry.store.configs;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.core.io.ClassPathResource;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  10. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  11. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  12. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  13. import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
  14. import org.springframework.security.oauth2.provider.token.TokenStore;
  15. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  16. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  17.  
  18. import java.io.IOException;
  19. import java.nio.charset.Charset;
  20.  
  21. /**
  22. * Created by phuongtran on 5/21/17.
  23. */
  24. @Configuration
  25. @EnableResourceServer
  26. public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
  27.  
  28. @Override
  29. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  30. resources.tokenServices(tokenService());
  31. resources.resourceId("frontend");
  32. }
  33.  
  34. @Bean
  35. public ResourceServerTokenServices tokenService() {
  36. DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
  37. defaultTokenServices.setTokenStore(tokenStore());
  38. return defaultTokenServices;
  39. }
  40.  
  41. @Bean
  42. public TokenStore tokenStore() {
  43. return new JwtTokenStore(accessTokenConvert());
  44. }
  45.  
  46. @Bean
  47. public JwtAccessTokenConverter accessTokenConvert() {
  48. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  49. Resource resource = new ClassPathResource("public.txt");
  50. String publicKey = null;
  51. try {
  52. publicKey = IOUtils.toString(resource.getInputStream(), Charset.defaultCharset());
  53. } catch (final IOException e) {
  54. throw new RuntimeException(e);
  55. }
  56. converter.setVerifierKey(publicKey);
  57. return converter;
  58. }
  59.  
  60. @Override
  61. public void configure(HttpSecurity http) throws Exception {
  62. http
  63. .authorizeRequests()
  64. .antMatchers("/api/public/v1/**").permitAll()
  65. .antMatchers("/download/**").permitAll()
  66. .anyRequest().authenticated();
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement