Guest User

Untitled

a guest
Jan 12th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 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 configurer) throws Exception {
  27.  
  28. configurer
  29. .inMemory()
  30. .withClient("my-client")
  31. .secret("$2a$10$jfAHmk4szDU/t1qLGlFTLukuBZL0ZHZGUJQICePjjyq6IrLOS934.") //my-secret
  32. .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit" )
  33. .scopes("read", "write", "trust");
  34. }
  35.  
  36. @Override
  37. public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
  38.  
  39. endpoints
  40. .authenticationManager(authenticationManager)
  41. .accessTokenConverter(accessTokenConverter());
  42. }
  43.  
  44. }
  45.  
  46. @Configuration
  47. @EnableResourceServer
  48. @Order(2)
  49. public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  50.  
  51. private static final String RESOURCE_ID = "resource_id";
  52.  
  53. @Override
  54. public void configure(ResourceServerSecurityConfigurer resources) {
  55. resources.resourceId(RESOURCE_ID).stateless(false);
  56. }
  57.  
  58. @Override
  59. public void configure(HttpSecurity http) throws Exception {
  60. http.
  61. anonymous().disable()
  62. .authorizeRequests()
  63. .antMatchers("/admin/**").access("hasRole('ADMIN')")
  64. .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
  65. }
  66. }
  67.  
  68. @Configuration
  69. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  70.  
  71. @Resource(name = "userDetailService")
  72. private UserDetailService userDetailsService;
  73.  
  74. @Bean
  75. public BCryptPasswordEncoder encoder() {
  76. return new BCryptPasswordEncoder();
  77. }
  78.  
  79. @Override
  80. @Bean
  81. public AuthenticationManager authenticationManagerBean() throws Exception {
  82. return super.authenticationManagerBean();
  83. }
  84.  
  85. @Autowired
  86. public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
  87. auth.userDetailsService(userDetailsService).passwordEncoder(encoder());
  88. }
  89.  
  90. @Override
  91. protected void configure(HttpSecurity http) throws Exception {
  92. http
  93. .csrf().disable()
  94. .anonymous().disable()
  95. .authorizeRequests()
  96. .antMatchers("/api-docs/**").permitAll()
  97. .and()
  98. .formLogin();
  99. }
  100.  
  101. @Override
  102. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  103. auth
  104. .userDetailsService(userDetailsService)
  105. .passwordEncoder(encoder());
  106. }
  107. }
  108.  
  109. curl --request POST
  110. --url http://localhost:8080/oauth/token
  111. --header 'authorization: Basic bXktY2xpZW50Om15LXNlY3JldA=='
  112. --header 'content-type: application/x-www-form-urlencoded'
  113. --data 'grant_type=password&username=admin&password=test'
Add Comment
Please, Sign In to add comment