//package com.lqkj.link.config.auth; // // //import com.nimbusds.jose.jwk.JWKSet; //import com.nimbusds.jose.jwk.RSAKey; //import com.nimbusds.jose.jwk.source.ImmutableJWKSet; //import com.nimbusds.jose.jwk.source.JWKSource; //import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Configuration; //import org.springframework.core.annotation.Order; //import org.springframework.security.config.Customizer; //import org.springframework.security.config.annotation.web.builders.HttpSecurity; //import org.springframework.security.core.userdetails.User; //import org.springframework.security.core.userdetails.UserDetails; //import org.springframework.security.core.userdetails.UserDetailsService; //import org.springframework.security.oauth2.core.AuthorizationGrantType; //import org.springframework.security.oauth2.core.ClientAuthenticationMethod; //import org.springframework.security.oauth2.core.oidc.OidcScopes; //import org.springframework.security.oauth2.jwt.JwtDecoder; //import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository; //import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; //import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; //import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration; //import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer; //import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings; //import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; //import org.springframework.security.provisioning.InMemoryUserDetailsManager; //import org.springframework.security.web.SecurityFilterChain; //import org.springframework.security.web.util.matcher.AntPathRequestMatcher; // //import java.security.KeyPair; //import java.security.KeyPairGenerator; //import java.security.interfaces.RSAPrivateKey; //import java.security.interfaces.RSAPublicKey; //import java.util.UUID; // //@Configuration //public class OauthAuthorizationConfig { // @Bean // @Order(1) // public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception { // OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http); // http // .getConfigurer(OAuth2AuthorizationServerConfigurer.class) // .oidc(Customizer.withDefaults()); // // http // .oauth2ResourceServer(Customizer.withDefaults()); // return http.build(); // } // // @Bean // @Order(2) // public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { // http // .authorizeHttpRequests((authorize) -> // authorize // .requestMatchers( // new AntPathRequestMatcher("/oauth2/**"), // new AntPathRequestMatcher("/**/*.html")) // .permitAll() // .anyRequest() // .authenticated()); // // return http.build(); // } // // @Bean // public UserDetailsService userDetailsService() { // UserDetails userDetails = User.withDefaultPasswordEncoder() // .username("test") // .password("test") // .roles("USER") // .build(); // // return new InMemoryUserDetailsManager(userDetails); // } // // @Bean // public RegisteredClientRepository registeredClientRepository() { // RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString()) // .clientId("link_server") // .clientSecret("{noop}demo-client-secret") // .authorizationGrantType(AuthorizationGrantType.PASSWORD) // .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) // .scope("all") // // 登录成功后对scope进行确认授权 // .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build()) // .build(); // // return new InMemoryRegisteredClientRepository(registeredClient); // } // // @Bean // public JWKSource jwkSource() { // KeyPair keyPair = generateRsaKey(); // RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // RSAKey rsaKey = new RSAKey // .Builder(publicKey) // .privateKey(privateKey) // .keyID(UUID.randomUUID().toString()) // .build(); // JWKSet jwkSet = new JWKSet(rsaKey); // return new ImmutableJWKSet<>(jwkSet); // } // // private static KeyPair generateRsaKey() { // KeyPair keyPair; // try { // KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); // keyPairGenerator.initialize(2048); // keyPair = keyPairGenerator.generateKeyPair(); // } // catch (Exception ex) { // throw new IllegalStateException(ex); // } // return keyPair; // } // // @Bean // public JwtDecoder jwtDecoder(JWKSource jwkSource) { // return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource); // } // // @Bean // public AuthorizationServerSettings authorizationServerSettings() { // return AuthorizationServerSettings.builder().build(); // } //}