From ccdb57fa3932785811faf9dd42d5998e4f1ea14b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 8 Dec 2022 10:37:23 +0300 Subject: Spring beans should not be public --- .../java/com/juick/config/ActivityPubConfig.java | 16 ++++-- .../java/com/juick/config/HttpClientConfig.java | 22 ++++---- src/main/java/com/juick/config/MailConfig.java | 2 +- src/main/java/com/juick/config/SapeConfig.java | 2 +- src/main/java/com/juick/config/SecurityConfig.java | 16 +++--- .../com/juick/config/SignInWithAppleConfig.java | 2 +- src/main/java/com/juick/config/TelegramConfig.java | 2 +- src/main/java/com/juick/config/TwitterConfig.java | 2 +- src/main/java/com/juick/config/WebConfig.java | 63 +++++++++++++++------- src/main/java/com/juick/config/XMPPConfig.java | 5 +- 10 files changed, 83 insertions(+), 49 deletions(-) (limited to 'src/main/java/com/juick/config') diff --git a/src/main/java/com/juick/config/ActivityPubConfig.java b/src/main/java/com/juick/config/ActivityPubConfig.java index fb204aaa..c93c035b 100644 --- a/src/main/java/com/juick/config/ActivityPubConfig.java +++ b/src/main/java/com/juick/config/ActivityPubConfig.java @@ -30,6 +30,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert import org.springframework.web.client.RestTemplate; import javax.inject.Inject; + import java.nio.charset.StandardCharsets; @Configuration @@ -38,26 +39,31 @@ public class ActivityPubConfig { ActivityPubClientErrorHandler activityPubClientErrorHandler; @Inject ObjectMapper jsonMapper; + @Bean - public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() { + MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(jsonMapper); return converter; } + @Bean - public ActivityPubManager activityPubManager() { + ActivityPubManager activityPubManager() { return new ActivityPubManager(); } + @Inject CloseableHttpClient httpClient; + @Bean - public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() { + HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() { HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); clientHttpRequestFactory.setHttpClient(httpClient); return clientHttpRequestFactory; } + @Bean - public RestTemplate apClient() { + RestTemplate apClient() { RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory()); restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter()); restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); @@ -65,4 +71,4 @@ public class ActivityPubConfig { restTemplate.getInterceptors().add(new ActivityPubRequestInterceptor()); return restTemplate; } -} \ No newline at end of file +} diff --git a/src/main/java/com/juick/config/HttpClientConfig.java b/src/main/java/com/juick/config/HttpClientConfig.java index 99351828..b59a8e1a 100644 --- a/src/main/java/com/juick/config/HttpClientConfig.java +++ b/src/main/java/com/juick/config/HttpClientConfig.java @@ -42,16 +42,16 @@ public class HttpClientConfig { private static final int MAX_TOTAL_CONNECTIONS = 50; private static final int DEFAULT_KEEP_ALIVE_TIME_MILLIS = 20 * 1000; private static final int CLOSE_IDLE_CONNECTION_WAIT_TIME_SECS = 30; - + @Bean - public PoolingHttpClientConnectionManager poolingConnectionManager() { + PoolingHttpClientConnectionManager poolingConnectionManager() { PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(); poolingConnectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS); return poolingConnectionManager; } - + @Bean - public ConnectionKeepAliveStrategy connectionKeepAliveStrategy() { + ConnectionKeepAliveStrategy connectionKeepAliveStrategy() { return new ConnectionKeepAliveStrategy() { @Override public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) { @@ -61,7 +61,7 @@ public class HttpClientConfig { HeaderElement he = it.next(); String param = he.getName(); String value = he.getValue(); - + if (value != null && param.equalsIgnoreCase("timeout")) { return TimeValue.of(Long.parseLong(value) * 1000, TimeUnit.MILLISECONDS); } @@ -70,24 +70,24 @@ public class HttpClientConfig { } }; } - + @Bean - public CloseableHttpClient httpClient() { + CloseableHttpClient httpClient() { RequestConfig requestConfig = RequestConfig.custom() .setCookieSpec(StandardCookieSpec.IGNORE) .setConnectionRequestTimeout(Timeout.of(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS)) .setConnectTimeout(Timeout.of(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)) .setResponseTimeout(Timeout.of(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS)).build(); - + return HttpClients.custom() .setDefaultRequestConfig(requestConfig) .setConnectionManager(poolingConnectionManager()) .setKeepAliveStrategy(connectionKeepAliveStrategy()) .build(); } - + @Bean - public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) { + Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) { return new Runnable() { @Override @Scheduled(fixedDelay = 10000) @@ -106,4 +106,4 @@ public class HttpClientConfig { } }; } -} \ No newline at end of file +} diff --git a/src/main/java/com/juick/config/MailConfig.java b/src/main/java/com/juick/config/MailConfig.java index 209796be..24875f80 100644 --- a/src/main/java/com/juick/config/MailConfig.java +++ b/src/main/java/com/juick/config/MailConfig.java @@ -26,7 +26,7 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnProperty("service_email") public class MailConfig { @Bean - public EmailManager emailManager() { + EmailManager emailManager() { return new EmailManager(); } } diff --git a/src/main/java/com/juick/config/SapeConfig.java b/src/main/java/com/juick/config/SapeConfig.java index 9154de02..7960eed7 100644 --- a/src/main/java/com/juick/config/SapeConfig.java +++ b/src/main/java/com/juick/config/SapeConfig.java @@ -33,7 +33,7 @@ public class SapeConfig { private String token; @Bean - public Sape sape() { + Sape sape() { return new Sape(token, "juick.com", 2000, 3600); } } diff --git a/src/main/java/com/juick/config/SecurityConfig.java b/src/main/java/com/juick/config/SecurityConfig.java index b16dc755..ad24445b 100644 --- a/src/main/java/com/juick/config/SecurityConfig.java +++ b/src/main/java/com/juick/config/SecurityConfig.java @@ -62,7 +62,7 @@ public class SecurityConfig { private static final String COOKIE_NAME = "juick-remember-me"; @Bean - public UserDetailsService userDetailsService() { + UserDetailsService userDetailsService() { return new JuickUserDetailsService(userService); } @@ -85,12 +85,12 @@ public class SecurityConfig { private SignatureManager signatureManager; @Bean - public HashParamAuthenticationFilter apiAuthenticationFilter() { + HashParamAuthenticationFilter apiAuthenticationFilter() { return new HashParamAuthenticationFilter(userService, null); } @Bean - public AuthenticationEntryPoint juickAuthenticationEntryPoint() { + AuthenticationEntryPoint juickAuthenticationEntryPoint() { var entryPoint = new BasicAuthenticationEntryPoint(); entryPoint.setRealmName("Juick"); return entryPoint; @@ -102,12 +102,12 @@ public class SecurityConfig { private String webDomain; @Bean - public HashParamAuthenticationFilter wwwAuthenticationFilter() { + HashParamAuthenticationFilter wwwAuthenticationFilter() { return new HashParamAuthenticationFilter(userService, hashCookieServices()); } @Bean - public RememberMeServices hashCookieServices() { + RememberMeServices hashCookieServices() { TokenBasedRememberMeServices services = new TokenBasedRememberMeServices( rememberMeKey, userDetailsService()); @@ -121,7 +121,7 @@ public class SecurityConfig { @Bean @Order(1) - public SecurityFilterChain apiChain(HttpSecurity http) throws Exception { + SecurityFilterChain apiChain(HttpSecurity http) throws Exception { http.securityMatcher("/api/**") .addFilterBefore(apiAuthenticationFilter(), BasicAuthenticationFilter.class) .addFilterBefore(new HTTPSignatureAuthenticationFilter(signatureManager, userService), @@ -155,14 +155,14 @@ public class SecurityConfig { } @Bean - public AuthenticationSuccessHandler successHandler() { + AuthenticationSuccessHandler successHandler() { SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler(); handler.setUseReferer(true); return handler; } @Bean - public SecurityFilterChain wwwChain(HttpSecurity http) throws Exception { + SecurityFilterChain wwwChain(HttpSecurity http) throws Exception { http.addFilterBefore(wwwAuthenticationFilter(), BasicAuthenticationFilter.class) .authorizeHttpRequests(authorize -> authorize .requestMatchers("/settings", "/pm/**", "/**/bl", "/_twitter", "/post", diff --git a/src/main/java/com/juick/config/SignInWithAppleConfig.java b/src/main/java/com/juick/config/SignInWithAppleConfig.java index 618626d9..85e2bdd1 100644 --- a/src/main/java/com/juick/config/SignInWithAppleConfig.java +++ b/src/main/java/com/juick/config/SignInWithAppleConfig.java @@ -40,7 +40,7 @@ public class SignInWithAppleConfig { private Resource keyPath; @Bean - public AppleClientSecretGenerator clientSecretGenerator() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { + AppleClientSecretGenerator clientSecretGenerator() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { byte[] pemData = IOUtils.toByteArray(keyPath.getInputStream()); return new AppleClientSecretGenerator(appId, teamId, keyId, pemData); } diff --git a/src/main/java/com/juick/config/TelegramConfig.java b/src/main/java/com/juick/config/TelegramConfig.java index 85e00930..3d7fe621 100644 --- a/src/main/java/com/juick/config/TelegramConfig.java +++ b/src/main/java/com/juick/config/TelegramConfig.java @@ -26,7 +26,7 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnProperty(name = "telegram_token") public class TelegramConfig { @Bean - public TelegramBotManager telegramBotManager() { + TelegramBotManager telegramBotManager() { return new TelegramBotManager(); } } diff --git a/src/main/java/com/juick/config/TwitterConfig.java b/src/main/java/com/juick/config/TwitterConfig.java index 22540dc5..6b6ed421 100644 --- a/src/main/java/com/juick/config/TwitterConfig.java +++ b/src/main/java/com/juick/config/TwitterConfig.java @@ -26,7 +26,7 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnProperty("twitter_consumer_key") public class TwitterConfig { @Bean - public TwitterManager twitterManager() { + TwitterManager twitterManager() { return new TwitterManager(); } } diff --git a/src/main/java/com/juick/config/WebConfig.java b/src/main/java/com/juick/config/WebConfig.java index 0c816712..c15f4c18 100644 --- a/src/main/java/com/juick/config/WebConfig.java +++ b/src/main/java/com/juick/config/WebConfig.java @@ -39,6 +39,8 @@ import org.commonmark.node.Link; import org.commonmark.parser.Parser; import org.commonmark.renderer.html.HtmlRenderer; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.boot.web.servlet.support.ErrorPageFilter; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -73,22 +75,24 @@ public class WebConfig implements WebMvcConfigurer { private String tmpDir; @Value("${storage_path:#{systemProperties['java.io.tmpdir']}}") private String baseDir; + @Bean - public StorageService storageService() { + StorageService storageService() { return new FileSystemStorageService(baseDir, tmpDir); } @Bean - public HelpService helpService() { + HelpService helpService() { return new HelpService("help"); } @Bean - public Parser cmParser() { + Parser cmParser() { return Parser.builder().extensions(Collections.singletonList(AutolinkExtension.create())).build(); } + @Bean - public HtmlRenderer helpRenderer() { + HtmlRenderer helpRenderer() { return HtmlRenderer.builder() .attributeProviderFactory(context -> (node, tagName, attributes) -> { if (node instanceof Link) { @@ -102,18 +106,19 @@ public class WebConfig implements WebMvcConfigurer { }) .build(); } + @Bean - public Loader templateLoader() { + Loader templateLoader() { return new ClasspathLoader(); } @Bean - public SpringExtension springExtension() { + SpringExtension springExtension() { return new SpringExtension(null); } @Bean - public PebbleEngine pebbleEngine() { + PebbleEngine pebbleEngine() { boolean devToolsArePresent = false; try { Class.forName("org.springframework.boot.devtools.livereload.Connection"); @@ -135,39 +140,46 @@ public class WebConfig implements WebMvcConfigurer { private Resource keystore; @Value("${keystore_password:secret}") private String keystorePassword; + @Bean - public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { + ResourceUrlEncodingFilter resourceUrlEncodingFilter() { return new ResourceUrlEncodingFilter(); } + @Bean - public KeystoreManager keystoreManager() { + KeystoreManager keystoreManager() { return new KeystoreManager(keystore, keystorePassword); } + @Bean - public Remark remarkConverter() { + Remark remarkConverter() { Options options = new Options(); options.inlineLinks = true; return new Remark(options); } + @Bean - public CommandsManager commandsManager() { + CommandsManager commandsManager() { return new CommandsManager(); } + @Bean - public ServerManager serverManager() { + ServerManager serverManager() { return new ServerManager(); } + @Bean - public SignatureManager signatureManager() { + SignatureManager signatureManager() { return new SignatureManager(); } + @Bean - public TopManager topManager() { + TopManager topManager() { return new TopManager(); } @Bean - public ViewResolver viewResolver() { + ViewResolver viewResolver() { PebbleViewResolver viewResolver = new PebbleViewResolver(pebbleEngine()); viewResolver.setPrefix("templates"); viewResolver.setSuffix(".html"); @@ -197,15 +209,30 @@ public class WebConfig implements WebMvcConfigurer { private String archiveUsername; @Bean - public User serviceUser() { + User serviceUser() { return userService.getUserByName(serviceUsername); } + @Bean - public User archiveUser() { + User archiveUser() { return userService.getUserByName(archiveUsername); } + @Bean - public RequestRejectedHandler requestRejectedHandler() { + RequestRejectedHandler requestRejectedHandler() { return new HttpStatusRequestRejectedHandler(); } + + @Bean + ErrorPageFilter errorPageFilter() { + return new ErrorPageFilter(); + } + + @Bean + FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); + filterRegistrationBean.setFilter(filter); + filterRegistrationBean.setEnabled(false); + return filterRegistrationBean; + } } diff --git a/src/main/java/com/juick/config/XMPPConfig.java b/src/main/java/com/juick/config/XMPPConfig.java index 98623f29..cde4c0dc 100644 --- a/src/main/java/com/juick/config/XMPPConfig.java +++ b/src/main/java/com/juick/config/XMPPConfig.java @@ -29,13 +29,14 @@ import org.springframework.format.support.DefaultFormattingConversionService; @ConditionalOnProperty("xmppbot_jid") public class XMPPConfig { @Bean - public static ConversionService conversionService() { + static ConversionService conversionService() { DefaultFormattingConversionService cs = new DefaultFormattingConversionService(); cs.addConverter(new JidConverter()); return cs; } + @Bean - public XMPPManager xmppConnection() { + XMPPManager xmppConnection() { return new XMPPManager(); } } -- cgit v1.2.3