|
@@ -1,205 +0,0 @@
|
|
|
-//package com.backendsys.configs;
|
|
|
-//
|
|
|
-//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 com.nimbusds.jose.proc.SecurityContext;
|
|
|
-//import org.springframework.context.aspect.Bean;
|
|
|
-//import org.springframework.context.aspect.Configuration;
|
|
|
-//import org.springframework.core.Ordered;
|
|
|
-//import org.springframework.core.aspect.Order;
|
|
|
-//import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
-//import org.springframework.security.config.Customizer;
|
|
|
-//import org.springframework.security.config.aspect.web.builders.HttpSecurity;
|
|
|
-//import org.springframework.security.config.aspect.web.configuration.EnableWebSecurity;
|
|
|
-//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.crypto.factory.PasswordEncoderFactories;
|
|
|
-//import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
-//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.aspect.web.configuration.OAuth2AuthorizationServerConfiguration;
|
|
|
-//import org.springframework.security.oauth2.server.authorization.config.aspect.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.authentication.LoginUrlAuthenticationEntryPoint;
|
|
|
-//
|
|
|
-//import java.security.KeyPairDTO;
|
|
|
-//import java.security.KeyPairGenerator;
|
|
|
-//import java.security.interfaces.RSAPrivateKey;
|
|
|
-//import java.security.interfaces.RSAPublicKey;
|
|
|
-//import java.util.UUID;
|
|
|
-//
|
|
|
-//@Configuration
|
|
|
-//@EnableWebSecurity
|
|
|
-//public class SecurityConfig {
|
|
|
-//
|
|
|
-// // https://docs.spring.io/spring-authorization-server/docs/current/reference/html/getting-started.html
|
|
|
-// // https://github.com/spring-projects/spring-authorization-server/blob/main/samples/default-authorizationserver/src/main/java/sample/config/AuthorizationServerConfig.java
|
|
|
-//
|
|
|
-// // 协议端点的Spring安全过滤器链
|
|
|
-// @Bean
|
|
|
-// @Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
-// public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
|
|
|
-//
|
|
|
-// OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
|
|
|
-// http.getConfigurer(OAuth2AuthorizationServerConfigurer.class).oidc(Customizer.withDefaults());
|
|
|
-// // Enable OpenID Connect 1.0
|
|
|
-// http
|
|
|
-// // Redirect to the login page when not authenticated from the
|
|
|
-// // authorization endpoint
|
|
|
-// .exceptionHandling((exceptions) -> exceptions
|
|
|
-// .authenticationEntryPoint(
|
|
|
-// new LoginUrlAuthenticationEntryPoint("/login"))
|
|
|
-// )
|
|
|
-// // Accept access tokens for User Info and/or Client Registration
|
|
|
-// .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
|
|
|
-//
|
|
|
-// return http.build();
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// // 用于身份验证的Spring Security过滤器链。
|
|
|
-// @Bean
|
|
|
-// @Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
-// public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
|
|
|
-// http.authorizeRequests(authorizeRequests ->
|
|
|
-// // 所有请求均放过, spring security 就没有什么用了
|
|
|
-// // anyRequest() 限定任意的请求
|
|
|
-// // permitAll() 无条件允许访问
|
|
|
-// authorizeRequests.anyRequest().permitAll()
|
|
|
-// );
|
|
|
-//
|
|
|
-// //http
|
|
|
-// // // .csrf().disable()
|
|
|
-// // .authorizeHttpRequests(
|
|
|
-// // (requests) -> requests
|
|
|
-// // // .requestMatchers(new AntPathRequestMatcher("/actuator/**"), ..)
|
|
|
-// // .requestMatchers("/", "/api/**").permitAll()
|
|
|
-// // .anyRequest().authenticated()
|
|
|
-// //).formLogin((form) -> form
|
|
|
-// // .loginPage("/login")
|
|
|
-// // .permitAll()
|
|
|
-// //).logout((logout) -> logout.permitAll());
|
|
|
-// return http.build();
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// // UserDetailsService的实例,用于检索要进行身份验证的用户。
|
|
|
-// @Bean
|
|
|
-// public UserDetailsService userDetailsService() {
|
|
|
-// // 密码加密
|
|
|
-// PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
|
|
-// String pwd = encoder.encode("123456");
|
|
|
-// // 用户验证
|
|
|
-// UserDetails admin = User.withUsername("admin")
|
|
|
-// .password(pwd)
|
|
|
-// .roles("ADMIN")
|
|
|
-// .build();
|
|
|
-// return new InMemoryUserDetailsManager(admin);
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-//
|
|
|
-//
|
|
|
-// // RegisteredClientRepository 的实例,用于管理客户端。
|
|
|
-// @Bean
|
|
|
-// public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) {
|
|
|
-// RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
|
|
|
-// .clientId("messaging-client")
|
|
|
-// .clientSecret("{noop}secret")
|
|
|
-// .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
|
|
|
-// .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
|
|
|
-// .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
|
|
|
-// .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
|
|
|
-// .redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc")
|
|
|
-// .redirectUri("http://127.0.0.1:8080/authorized")
|
|
|
-// .scope(OidcScopes.OPENID)
|
|
|
-// .scope(OidcScopes.PROFILE)
|
|
|
-// .scope("message.read")
|
|
|
-// .scope("message.write")
|
|
|
-// .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
|
|
|
-// .build();
|
|
|
-//
|
|
|
-// // Save registered client in db as if in-memory
|
|
|
-// // JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository(jdbcTemplate);
|
|
|
-// // registeredClientRepository.save(registeredClient);
|
|
|
-//
|
|
|
-// return new InMemoryRegisteredClientRepository(registeredClient);
|
|
|
-// }
|
|
|
-//
|
|
|
-// /*
|
|
|
-// @Bean
|
|
|
-// public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) {
|
|
|
-// return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository);
|
|
|
-// }
|
|
|
-// @Bean
|
|
|
-// public OAuth2AuthorizationConsentService authorizationConsentService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) {
|
|
|
-// return new JdbcOAuth2AuthorizationConsentService(jdbcTemplate, registeredClientRepository);
|
|
|
-// }
|
|
|
-// */
|
|
|
-//
|
|
|
-// // 用于签署访问令牌的com. nimbuss .jo .jwk.source. jwksource实例。
|
|
|
-// @Bean
|
|
|
-// public JWKSource<SecurityContext> jwkSource() {
|
|
|
-// KeyPairDTO 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);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 一个java.security.KeyPair的实例,它带有在启动时生成的密钥,用于创建上面的JWKSource
|
|
|
-// private static KeyPairDTO generateRsaKey() {
|
|
|
-// KeyPairDTO keyPair;
|
|
|
-// try {
|
|
|
-// KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
|
|
-// keyPairGenerator.initialize(2048);
|
|
|
-// keyPair = keyPairGenerator.generateKeyPair();
|
|
|
-// }
|
|
|
-// catch (Exception ex) {
|
|
|
-// throw new IllegalStateException(ex);
|
|
|
-// }
|
|
|
-// return keyPair;
|
|
|
-// }
|
|
|
-//
|
|
|
-// // JwtDecoder的实例,用于解码已签名的访问令牌。
|
|
|
-// @Bean
|
|
|
-// public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
|
|
|
-// return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 配置Spring授权服务器的AuthorizationServerSettings实例。
|
|
|
-// @Bean
|
|
|
-// public AuthorizationServerSettings authorizationServerSettings() {
|
|
|
-// return AuthorizationServerSettings.builder().build();
|
|
|
-// }
|
|
|
-//
|
|
|
-// /*
|
|
|
-// @Bean
|
|
|
-// public EmbeddedDatabase embeddedDatabase() {
|
|
|
-// // @formatter:off
|
|
|
-// return new EmbeddedDatabaseBuilder()
|
|
|
-// .generateUniqueName(true)
|
|
|
-// .setType(EmbeddedDatabaseType.H2)
|
|
|
-// .setScriptEncoding("UTF-8")
|
|
|
-// .addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql")
|
|
|
-// .addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql")
|
|
|
-// .addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql")
|
|
|
-// .build();
|
|
|
-// */
|
|
|
-//
|
|
|
-//}
|