OauthAuthorizationConfig.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //package com.lqkj.link.config.auth;
  2. //
  3. //
  4. //import com.nimbusds.jose.jwk.JWKSet;
  5. //import com.nimbusds.jose.jwk.RSAKey;
  6. //import com.nimbusds.jose.jwk.source.ImmutableJWKSet;
  7. //import com.nimbusds.jose.jwk.source.JWKSource;
  8. //import org.springframework.context.annotation.Bean;
  9. //import org.springframework.context.annotation.Configuration;
  10. //import org.springframework.core.annotation.Order;
  11. //import org.springframework.security.config.Customizer;
  12. //import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  13. //import org.springframework.security.core.userdetails.User;
  14. //import org.springframework.security.core.userdetails.UserDetails;
  15. //import org.springframework.security.core.userdetails.UserDetailsService;
  16. //import org.springframework.security.oauth2.core.AuthorizationGrantType;
  17. //import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
  18. //import org.springframework.security.oauth2.core.oidc.OidcScopes;
  19. //import org.springframework.security.oauth2.jwt.JwtDecoder;
  20. //import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
  21. //import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
  22. //import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
  23. //import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
  24. //import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
  25. //import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings;
  26. //import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
  27. //import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  28. //import org.springframework.security.web.SecurityFilterChain;
  29. //import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  30. //
  31. //import java.security.KeyPair;
  32. //import java.security.KeyPairGenerator;
  33. //import java.security.interfaces.RSAPrivateKey;
  34. //import java.security.interfaces.RSAPublicKey;
  35. //import java.util.UUID;
  36. //
  37. //@Configuration
  38. //public class OauthAuthorizationConfig {
  39. // @Bean
  40. // @Order(1)
  41. // public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
  42. // OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
  43. // http
  44. // .getConfigurer(OAuth2AuthorizationServerConfigurer.class)
  45. // .oidc(Customizer.withDefaults());
  46. //
  47. // http
  48. // .oauth2ResourceServer(Customizer.withDefaults());
  49. // return http.build();
  50. // }
  51. //
  52. // @Bean
  53. // @Order(2)
  54. // public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
  55. // http
  56. // .authorizeHttpRequests((authorize) ->
  57. // authorize
  58. // .requestMatchers(
  59. // new AntPathRequestMatcher("/oauth2/**"),
  60. // new AntPathRequestMatcher("/**/*.html"))
  61. // .permitAll()
  62. // .anyRequest()
  63. // .authenticated());
  64. //
  65. // return http.build();
  66. // }
  67. //
  68. // @Bean
  69. // public UserDetailsService userDetailsService() {
  70. // UserDetails userDetails = User.withDefaultPasswordEncoder()
  71. // .username("test")
  72. // .password("test")
  73. // .roles("USER")
  74. // .build();
  75. //
  76. // return new InMemoryUserDetailsManager(userDetails);
  77. // }
  78. //
  79. // @Bean
  80. // public RegisteredClientRepository registeredClientRepository() {
  81. // RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
  82. // .clientId("link_server")
  83. // .clientSecret("{noop}demo-client-secret")
  84. // .authorizationGrantType(AuthorizationGrantType.PASSWORD)
  85. // .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
  86. // .scope("all")
  87. // // 登录成功后对scope进行确认授权
  88. // .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
  89. // .build();
  90. //
  91. // return new InMemoryRegisteredClientRepository(registeredClient);
  92. // }
  93. //
  94. // @Bean
  95. // public JWKSource jwkSource() {
  96. // KeyPair keyPair = generateRsaKey();
  97. // RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  98. // RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  99. // RSAKey rsaKey = new RSAKey
  100. // .Builder(publicKey)
  101. // .privateKey(privateKey)
  102. // .keyID(UUID.randomUUID().toString())
  103. // .build();
  104. // JWKSet jwkSet = new JWKSet(rsaKey);
  105. // return new ImmutableJWKSet<>(jwkSet);
  106. // }
  107. //
  108. // private static KeyPair generateRsaKey() {
  109. // KeyPair keyPair;
  110. // try {
  111. // KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  112. // keyPairGenerator.initialize(2048);
  113. // keyPair = keyPairGenerator.generateKeyPair();
  114. // }
  115. // catch (Exception ex) {
  116. // throw new IllegalStateException(ex);
  117. // }
  118. // return keyPair;
  119. // }
  120. //
  121. // @Bean
  122. // public JwtDecoder jwtDecoder(JWKSource jwkSource) {
  123. // return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
  124. // }
  125. //
  126. // @Bean
  127. // public AuthorizationServerSettings authorizationServerSettings() {
  128. // return AuthorizationServerSettings.builder().build();
  129. // }
  130. //}