From af10316ce019cae96b1e4227f232d32ddb9b0261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Wed, 17 Apr 2024 13:40:38 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20Improving=20structure=20/=20format=20of?= =?UTF-8?q?=20the=20code.=20=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuthorizationServerConfiguration.java | 34 ++++++------ .../pig/auth/endpoint/ImageCodeEndpoint.java | 2 +- .../pig/auth/endpoint/PigTokenEndpoint.java | 19 +++---- .../support/filter/PasswordDecoderFilter.java | 2 +- .../support/filter/ValidateCodeFilter.java | 3 +- .../PigBootSecurityServerConfiguration.java | 52 +++++++++---------- .../servlet/RepeatBodyRequestWrapper.java | 4 +- .../openfeign/PigFeignClientsRegistrar.java | 13 ++--- .../admin/controller/SysLogController.java | 6 +-- .../pig/admin/service/SysLogService.java | 2 - .../admin/service/impl/SysLogServiceImpl.java | 5 +- 11 files changed, 65 insertions(+), 77 deletions(-) diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfiguration.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfiguration.java index 489282f9..ce38ef98 100755 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfiguration.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/config/AuthorizationServerConfiguration.java @@ -81,28 +81,28 @@ public class AuthorizationServerConfiguration { http.addFilterBefore(passwordDecoderFilter, UsernamePasswordAuthenticationFilter.class); http.with(authorizationServerConfigurer.tokenEndpoint((tokenEndpoint) -> {// 个性化认证授权端点 - tokenEndpoint.accessTokenRequestConverter(accessTokenRequestConverter()) // 注入自定义的授权认证Converter - .accessTokenResponseHandler(new PigAuthenticationSuccessEventHandler()) // 登录成功处理器 - .errorResponseHandler(new PigAuthenticationFailureEventHandler());// 登录失败处理器 - }).clientAuthentication(oAuth2ClientAuthenticationConfigurer -> // 个性化客户端认证 - oAuth2ClientAuthenticationConfigurer.errorResponseHandler(new PigAuthenticationFailureEventHandler()))// 处理客户端认证异常 - .authorizationEndpoint(authorizationEndpoint -> authorizationEndpoint// 授权码端点个性化confirm页面 - .consentPage(SecurityConstants.CUSTOM_CONSENT_PAGE_URI)), Customizer.withDefaults()); + tokenEndpoint.accessTokenRequestConverter(accessTokenRequestConverter()) // 注入自定义的授权认证Converter + .accessTokenResponseHandler(new PigAuthenticationSuccessEventHandler()) // 登录成功处理器 + .errorResponseHandler(new PigAuthenticationFailureEventHandler());// 登录失败处理器 + }).clientAuthentication(oAuth2ClientAuthenticationConfigurer -> // 个性化客户端认证 + oAuth2ClientAuthenticationConfigurer.errorResponseHandler(new PigAuthenticationFailureEventHandler()))// 处理客户端认证异常 + .authorizationEndpoint(authorizationEndpoint -> authorizationEndpoint// 授权码端点个性化confirm页面 + .consentPage(SecurityConstants.CUSTOM_CONSENT_PAGE_URI)), Customizer.withDefaults()); - AntPathRequestMatcher[] requestMatchers = new AntPathRequestMatcher[]{ + AntPathRequestMatcher[] requestMatchers = new AntPathRequestMatcher[] { AntPathRequestMatcher.antMatcher("/token/**"), AntPathRequestMatcher.antMatcher("/actuator/**"), AntPathRequestMatcher.antMatcher("/code/image"), AntPathRequestMatcher.antMatcher("/css/**"), - AntPathRequestMatcher.antMatcher("/error")}; + AntPathRequestMatcher.antMatcher("/error") }; http.authorizeHttpRequests(authorizeRequests -> { - // 自定义接口、端点暴露 - authorizeRequests.requestMatchers(requestMatchers).permitAll(); - authorizeRequests.anyRequest().authenticated(); - }) - .with(authorizationServerConfigurer.authorizationService(authorizationService)// redis存储token的实现 - .authorizationServerSettings( - AuthorizationServerSettings.builder().issuer(SecurityConstants.PROJECT_LICENSE).build()), - Customizer.withDefaults()); + // 自定义接口、端点暴露 + authorizeRequests.requestMatchers(requestMatchers).permitAll(); + authorizeRequests.anyRequest().authenticated(); + }) + .with(authorizationServerConfigurer.authorizationService(authorizationService)// redis存储token的实现 + .authorizationServerSettings( + AuthorizationServerSettings.builder().issuer(SecurityConstants.PROJECT_LICENSE).build()), + Customizer.withDefaults()); http.with(new FormIdentityLoginConfigurer(), Customizer.withDefaults()); DefaultSecurityFilterChain securityFilterChain = http.build(); diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/ImageCodeEndpoint.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/ImageCodeEndpoint.java index 52dc625c..85632dc3 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/ImageCodeEndpoint.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/ImageCodeEndpoint.java @@ -40,7 +40,7 @@ public class ImageCodeEndpoint { String result = captcha.text(); redisTemplate.opsForValue() - .set(CacheConstants.DEFAULT_CODE_KEY + randomStr, result, SecurityConstants.CODE_TIME, TimeUnit.SECONDS); + .set(CacheConstants.DEFAULT_CODE_KEY + randomStr, result, SecurityConstants.CODE_TIME, TimeUnit.SECONDS); // 转换流信息写出 captcha.out(response.getOutputStream()); } diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java index 943f811d..f420e92a 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java @@ -92,9 +92,8 @@ public class PigTokenEndpoint { /** * 认证页面 - * * @param modelAndView - * @param error 表单登录失败处理回调的错误信息 + * @param error 表单登录失败处理回调的错误信息 * @return ModelAndView */ @GetMapping("/login") @@ -106,13 +105,13 @@ public class PigTokenEndpoint { @GetMapping("/confirm_access") public ModelAndView confirm(Principal principal, ModelAndView modelAndView, - @RequestParam(OAuth2ParameterNames.CLIENT_ID) String clientId, - @RequestParam(OAuth2ParameterNames.SCOPE) String scope, - @RequestParam(OAuth2ParameterNames.STATE) String state) { + @RequestParam(OAuth2ParameterNames.CLIENT_ID) String clientId, + @RequestParam(OAuth2ParameterNames.SCOPE) String scope, + @RequestParam(OAuth2ParameterNames.STATE) String state) { SysOauthClientDetails clientDetails = RetOps - .of(clientDetailsService.getClientDetailsById(clientId, SecurityConstants.FROM_IN)) - .getData() - .orElseThrow(() -> new OAuthClientException("clientId 不合法")); + .of(clientDetailsService.getClientDetailsById(clientId, SecurityConstants.FROM_IN)) + .getData() + .orElseThrow(() -> new OAuthClientException("clientId 不合法")); Set authorizedScopes = StringUtils.commaDelimitedListToSet(clientDetails.getScope()); modelAndView.addObject("clientId", clientId); @@ -125,7 +124,6 @@ public class PigTokenEndpoint { /** * 退出并删除token - * * @param authHeader Authorization */ @DeleteMapping("/logout") @@ -140,7 +138,6 @@ public class PigTokenEndpoint { /** * 校验token - * * @param token 令牌 */ @SneakyThrows @@ -171,7 +168,6 @@ public class PigTokenEndpoint { /** * 令牌管理调用 - * * @param token token */ @Inner @@ -198,7 +194,6 @@ public class PigTokenEndpoint { /** * 查询token - * * @param params 分页参数 * @return */ diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/PasswordDecoderFilter.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/PasswordDecoderFilter.java index d6c79556..299affdd 100755 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/PasswordDecoderFilter.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/PasswordDecoderFilter.java @@ -84,7 +84,7 @@ public class PasswordDecoderFilter extends OncePerRequestFilter { // 解密密码 String decryptPassword = aes.decryptStr(values[0]); - parameterMap.put(k, new String[]{decryptPassword}); + parameterMap.put(k, new String[] { decryptPassword }); }); chain.doFilter(requestWrapper, response); } diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/ValidateCodeFilter.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/ValidateCodeFilter.java index 75c19164..5d68aac0 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/ValidateCodeFilter.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/filter/ValidateCodeFilter.java @@ -71,7 +71,8 @@ public class ValidateCodeFilter extends OncePerRequestFilter { try { checkCode(); filterChain.doFilter(request, response); - } catch (ValidateCodeException validateCodeException) { + } + catch (ValidateCodeException validateCodeException) { throw new OAuth2AuthenticationException(validateCodeException.getMessage()); } } diff --git a/pig-boot/src/main/java/com/pig4cloud/pig/bootstrap/PigBootSecurityServerConfiguration.java b/pig-boot/src/main/java/com/pig4cloud/pig/bootstrap/PigBootSecurityServerConfiguration.java index 20eb0274..6bc2754e 100644 --- a/pig-boot/src/main/java/com/pig4cloud/pig/bootstrap/PigBootSecurityServerConfiguration.java +++ b/pig-boot/src/main/java/com/pig4cloud/pig/bootstrap/PigBootSecurityServerConfiguration.java @@ -88,39 +88,39 @@ public class PigBootSecurityServerConfiguration { // 认证服务器配置 http.with(authorizationServerConfigurer.tokenEndpoint((tokenEndpoint) -> {// 个性化认证授权端点 - tokenEndpoint.accessTokenRequestConverter(accessTokenRequestConverter) // 注入自定义的授权认证Converter - .accessTokenResponseHandler(new PigAuthenticationSuccessEventHandler()) // 登录成功处理器 - .errorResponseHandler(new PigAuthenticationFailureEventHandler());// 登录失败处理器 - }).clientAuthentication(oAuth2ClientAuthenticationConfigurer -> // 个性化客户端认证 - oAuth2ClientAuthenticationConfigurer.errorResponseHandler(new PigAuthenticationFailureEventHandler()))// 处理客户端认证异常 - , Customizer.withDefaults()) - .with(authorizationServerConfigurer.authorizationService(authorizationService)// redis存储token的实现 - .authorizationServerSettings( - AuthorizationServerSettings.builder().issuer(SecurityConstants.PROJECT_LICENSE).build()), - Customizer.withDefaults()); + tokenEndpoint.accessTokenRequestConverter(accessTokenRequestConverter) // 注入自定义的授权认证Converter + .accessTokenResponseHandler(new PigAuthenticationSuccessEventHandler()) // 登录成功处理器 + .errorResponseHandler(new PigAuthenticationFailureEventHandler());// 登录失败处理器 + }).clientAuthentication(oAuth2ClientAuthenticationConfigurer -> // 个性化客户端认证 + oAuth2ClientAuthenticationConfigurer.errorResponseHandler(new PigAuthenticationFailureEventHandler()))// 处理客户端认证异常 + , Customizer.withDefaults()) + .with(authorizationServerConfigurer.authorizationService(authorizationService)// redis存储token的实现 + .authorizationServerSettings( + AuthorizationServerSettings.builder().issuer(SecurityConstants.PROJECT_LICENSE).build()), + Customizer.withDefaults()); // 资源服务器配置 AntPathRequestMatcher[] requestMatchers = permitAllUrl.getUrls() - .stream() - .map(AntPathRequestMatcher::new) - .toList() - .toArray(new AntPathRequestMatcher[]{}); + .stream() + .map(AntPathRequestMatcher::new) + .toList() + .toArray(new AntPathRequestMatcher[] {}); http.authorizeHttpRequests(authorizeRequests -> authorizeRequests.requestMatchers(requestMatchers) - .permitAll() - .anyRequest() - .authenticated()) - .oauth2ResourceServer( - oauth2 -> oauth2.opaqueToken(token -> token.introspector(customOpaqueTokenIntrospector)) - .authenticationEntryPoint(resourceAuthExceptionEntryPoint) - .bearerTokenResolver(pigBearerTokenExtractor)) - .exceptionHandling(configurer -> configurer.authenticationEntryPoint(resourceAuthExceptionEntryPoint)) - .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable)) - .csrf(AbstractHttpConfigurer::disable); + .permitAll() + .anyRequest() + .authenticated()) + .oauth2ResourceServer( + oauth2 -> oauth2.opaqueToken(token -> token.introspector(customOpaqueTokenIntrospector)) + .authenticationEntryPoint(resourceAuthExceptionEntryPoint) + .bearerTokenResolver(pigBearerTokenExtractor)) + .exceptionHandling(configurer -> configurer.authenticationEntryPoint(resourceAuthExceptionEntryPoint)) + .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable)) + .csrf(AbstractHttpConfigurer::disable); http.with(authorizationServerConfigurer.authorizationService(authorizationService)// redis存储token的实现 - .authorizationServerSettings( - AuthorizationServerSettings.builder().issuer(SecurityConstants.PROJECT_LICENSE).build()), + .authorizationServerSettings( + AuthorizationServerSettings.builder().issuer(SecurityConstants.PROJECT_LICENSE).build()), Customizer.withDefaults()); DefaultSecurityFilterChain securityFilterChain = http.build(); diff --git a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/servlet/RepeatBodyRequestWrapper.java b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/servlet/RepeatBodyRequestWrapper.java index 848b487d..b221f101 100644 --- a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/servlet/RepeatBodyRequestWrapper.java +++ b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/servlet/RepeatBodyRequestWrapper.java @@ -84,7 +84,8 @@ public class RepeatBodyRequestWrapper extends HttpServletRequestWrapper { byte[] body = new byte[0]; try { body = StreamUtils.copyToByteArray(request.getInputStream()); - } catch (IOException e) { + } + catch (IOException e) { log.error("解析流中数据异常", e); } return body; @@ -92,7 +93,6 @@ public class RepeatBodyRequestWrapper extends HttpServletRequestWrapper { /** * 重写 getParameterMap() 方法 解决 undertow 中流被读取后,会进行标记,从而导致无法正确获取 body 中的表单数据的问题 - * * @return Map parameterMap */ @Override diff --git a/pig-common/pig-common-feign/src/main/java/org/springframework/cloud/openfeign/PigFeignClientsRegistrar.java b/pig-common/pig-common-feign/src/main/java/org/springframework/cloud/openfeign/PigFeignClientsRegistrar.java index 5e883b66..84dac6a3 100644 --- a/pig-common/pig-common-feign/src/main/java/org/springframework/cloud/openfeign/PigFeignClientsRegistrar.java +++ b/pig-common/pig-common-feign/src/main/java/org/springframework/cloud/openfeign/PigFeignClientsRegistrar.java @@ -98,7 +98,7 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, validate(attributes); BeanDefinitionBuilder definition = BeanDefinitionBuilder - .genericBeanDefinition(FeignClientFactoryBean.class); + .genericBeanDefinition(FeignClientFactoryBean.class); definition.addPropertyValue("url", getUrl(registry, attributes)); definition.addPropertyValue("path", getPath(attributes)); String name = getName(attributes); @@ -110,7 +110,8 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, String contextId = getContextId(attributes); aliasBuilder.append(contextId); definition.addPropertyValue("contextId", contextId); - } else { + } + else { aliasBuilder.append(name); } @@ -141,10 +142,11 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, } BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, - new String[]{alias}); + new String[] { alias }); BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); - } catch (ClassNotFoundException e) { + } + catch (ClassNotFoundException e) { e.printStackTrace(); } } @@ -153,7 +155,6 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, /** * Return the class used by {@link SpringFactoriesLoader} to load configuration * candidates. - * * @return the factory class */ private Class getSpringFactoriesLoaderFactoryClass() { @@ -259,7 +260,7 @@ public class PigFeignClientsRegistrar implements ImportBeanDefinitionRegistrar, } private void registerClientConfiguration(BeanDefinitionRegistry registry, Object name, Object className, - Object configuration) { + Object configuration) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FeignClientSpecification.class); builder.addConstructorArgValue(name); builder.addConstructorArgValue(className); diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysLogController.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysLogController.java index 2d395222..0fbbd989 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysLogController.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/controller/SysLogController.java @@ -57,8 +57,7 @@ public class SysLogController { /** * 简单分页查询 - * - * @param page 分页对象 + * @param page 分页对象 * @param sysLog 系统日志 * @return */ @@ -69,7 +68,6 @@ public class SysLogController { /** * 批量删除日志 - * * @param ids ID * @return success/false */ @@ -81,7 +79,6 @@ public class SysLogController { /** * 插入日志 - * * @param sysLog 日志实体 * @return success/false */ @@ -93,7 +90,6 @@ public class SysLogController { /** * 导出excel 表格 - * * @param sysLog 查询条件 * @return */ diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysLogService.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysLogService.java index 1352da64..7beb8813 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysLogService.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/SysLogService.java @@ -38,7 +38,6 @@ public interface SysLogService extends IService { /** * 分页查询日志 - * * @param page * @param sysLog * @return @@ -47,7 +46,6 @@ public interface SysLogService extends IService { /** * 插入日志 - * * @param sysLog 日志对象 * @return true/false */ diff --git a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysLogServiceImpl.java b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysLogServiceImpl.java index ba21436f..624f5a5e 100644 --- a/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysLogServiceImpl.java +++ b/pig-upms/pig-upms-biz/src/main/java/com/pig4cloud/pig/admin/service/impl/SysLogServiceImpl.java @@ -52,7 +52,6 @@ public class SysLogServiceImpl extends ServiceImpl impleme /** * 插入日志 - * * @param sysLog 日志对象 * @return true/false */ @@ -65,7 +64,6 @@ public class SysLogServiceImpl extends ServiceImpl impleme /** * 查询日志列表 - * * @param sysLog 查询条件 * @return List */ @@ -76,7 +74,6 @@ public class SysLogServiceImpl extends ServiceImpl impleme /** * 构建查询条件 - * * @param sysLog 前端条件 * @return LambdaQueryWrapper */ @@ -88,7 +85,7 @@ public class SysLogServiceImpl extends ServiceImpl impleme if (ArrayUtil.isNotEmpty(sysLog.getCreateTime())) { wrapper.ge(SysLog::getCreateTime, sysLog.getCreateTime()[0]) - .le(SysLog::getCreateTime, sysLog.getCreateTime()[1]); + .le(SysLog::getCreateTime, sysLog.getCreateTime()[1]); } return wrapper;