Guest User

Untitled

a guest
Jan 12th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. @Configuration
  2. @EnableAuthorizationServer
  3. public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  4.  
  5. private final AuthenticationManager authenticationManager;
  6.  
  7. @Autowired
  8. public AuthorizationServerConfig(AuthenticationManager authenticationManager) {
  9. this.authenticationManager = authenticationManager;
  10. }
  11.  
  12. @Bean
  13. public JwtAccessTokenConverter accessTokenConverter() {
  14. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  15. converter.setSigningKey("as466gf");
  16. return converter;
  17. }
  18.  
  19. @Bean
  20. public TokenStore tokenStore() {
  21. return new JwtTokenStore(accessTokenConverter());
  22. }
  23.  
  24.  
  25. @Override
  26. public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
  27. clients
  28. .inMemory()
  29. .withClient("my-client-id")
  30. .authorizedGrantTypes("authorization_code", "implicit", "refresh_token", "password")
  31. .authorities("ADMIN")
  32. .scopes("all")
  33. .resourceIds("product_api")
  34. .secret("$2a$10$jfAHmk4szDU/t1qLGlFTLukuBZL0ZHZGUJQICePjjyq6IrLOS934.")
  35. .redirectUris("https://example.com")
  36. .accessTokenValiditySeconds(7200)
  37. .refreshTokenValiditySeconds(7200);
  38. }
  39.  
  40. @Override
  41. public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
  42. oauthServer
  43. .tokenKeyAccess("permitAll()")
  44. .checkTokenAccess("permitAll()");
  45. }
  46.  
  47. @Override
  48. public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
  49.  
  50. endpoints
  51. .authenticationManager(authenticationManager)
  52. .accessTokenConverter(accessTokenConverter());
  53. }
  54.  
  55. }
  56.  
  57. @Configuration
  58. @EnableResourceServer
  59. @Order(2)
  60. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  61.  
  62.  
  63. @Override
  64. public void configure(ResourceServerSecurityConfigurer resources) {
  65. resources.resourceId("product_api");
  66. }
  67.  
  68. @Override
  69. public void configure(HttpSecurity http) throws Exception {
  70. http
  71. .requestMatchers()
  72. .antMatchers("/**")
  73. .and().authorizeRequests()
  74. .antMatchers("/**").permitAll()
  75. .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
  76. }
  77.  
  78.  
  79. }
  80.  
  81. @Configuration
  82. @Order(1)
  83. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  84.  
  85. @Resource(name = "userDetailService")
  86. private UserDetailService userDetailsService;
  87.  
  88. @Bean
  89. public BCryptPasswordEncoder encoder() {
  90. return new BCryptPasswordEncoder();
  91. }
  92.  
  93. @Override
  94. @Bean
  95. public AuthenticationManager authenticationManagerBean() throws Exception {
  96. return super.authenticationManagerBean();
  97. }
  98.  
  99. @Autowired
  100. public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
  101. auth.userDetailsService(userDetailsService).passwordEncoder(encoder());
  102. }
  103.  
  104. @Override
  105. protected void configure(HttpSecurity http) throws Exception {
  106.  
  107. http.authorizeRequests()
  108. .antMatchers("/api/v1/**")
  109. .hasAnyRole("ADMIN", "USER").and()
  110. .httpBasic().and().formLogin().and().authorizeRequests().anyRequest().authenticated();
  111. }
  112.  
  113. @Override
  114. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  115. auth
  116. .userDetailsService(userDetailsService)
  117. .passwordEncoder(encoder());
  118. }
  119. }
  120.  
  121. curl --request POST
  122. --url http://localhost:8080/oauth/token
  123. --header 'authorization: Basic bXktY2xpZW50Om15LXNlY3JldA=='
  124. --header 'content-type: application/x-www-form-urlencoded'
  125. --data 'grant_type=password&username=admin&password=test'
Add Comment
Please, Sign In to add comment