From 4b154d0413a491e0dcb020caffb3f6ee5d026cd3 Mon Sep 17 00:00:00 2001 From: zhaoxianglong Date: Sun, 4 Feb 2024 08:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=AB=E7=A0=81=E6=8A=A5=E8=AD=A6=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 4 ++ .../framework/config/SecurityConfig.java | 53 +++++++++++----- .../CodeScanningAlarmController.java | 63 +++++++++++++++++++ 3 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 zc-business/src/main/java/com/zc/business/controller/CodeScanningAlarmController.java diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 8ded2934..f0180661 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -180,3 +180,7 @@ iot: # #连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: -1ms + +# 允许访问的ip地址 +allowed: + ips: 10.0.81.202,10.168.73.54,10.168.71.194 diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index cc2d93eb..3590526d 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -1,6 +1,7 @@ package com.ruoyi.framework.config; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; @@ -13,24 +14,26 @@ import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.security.web.authentication.logout.LogoutFilter; +import org.springframework.security.web.util.matcher.RequestMatcher; import org.springframework.web.filter.CorsFilter; import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter; import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl; import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl; +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; + /** * spring security配置 - * */ @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true) -public class SecurityConfig extends WebSecurityConfigurerAdapter -{ +public class SecurityConfig extends WebSecurityConfigurerAdapter { /** * 自定义用户认证逻辑 */ @Autowired private UserDetailsService userDetailsService; - + /** * 认证失败处理类 */ @@ -48,13 +51,32 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter */ @Autowired private JwtAuthenticationTokenFilter authenticationTokenFilter; - + /** * 跨域过滤器 */ @Autowired private CorsFilter corsFilter; - + + /** + * 允许访问的ip地址 + */ + @Value("${allowed.ips}") + private String allowedIPs; + + // 定义一个RequestMatcher + RequestMatcher requestMatcher = new RequestMatcher() { + @Override + public boolean matches(HttpServletRequest request) { + // 获取请求的IP + String requestIP = request.getRemoteAddr(); + // 将配置文件中的IP字符串分割为数组 + String[] ips = allowedIPs.split(","); + // 检查请求的IP是否在允许的IP数组中 + return Arrays.asList(ips).contains(requestIP); + } + }; + /** * 解决 无法直接注入 AuthenticationManager * @@ -63,8 +85,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter */ @Bean @Override - public AuthenticationManager authenticationManagerBean() throws Exception - { + public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @@ -84,8 +105,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter * authenticated | 用户登录后可访问 */ @Override - protected void configure(HttpSecurity httpSecurity) throws Exception - { + protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity // CSRF禁用,因为不使用session .csrf().disable() @@ -117,11 +137,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter // 添加统一支付 回调接口 .antMatchers("/payment/wxPay/notify").anonymous() // 滑块验证码 - .antMatchers( "/captcha/get", "/captcha/check", "/captchaConfig").anonymous() + .antMatchers("/captcha/get", "/captcha/check", "/captchaConfig").anonymous() // license .antMatchers("/license/identityCode", "/license/authentication").anonymous() // 第三方、小程序、APP - .antMatchers("/api/**").anonymous() + //.antMatchers("/api/**").anonymous() + + // 对于满足requestMatcher的请求,允许所有用户访问。 + .requestMatchers(requestMatcher).permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated() @@ -139,8 +162,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter * 强散列哈希加密实现 */ @Bean - public BCryptPasswordEncoder bCryptPasswordEncoder() - { + public BCryptPasswordEncoder bCryptPasswordEncoder() { return new BCryptPasswordEncoder(); } @@ -148,8 +170,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter * 身份认证接口 */ @Override - protected void configure(AuthenticationManagerBuilder auth) throws Exception - { + protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); } } diff --git a/zc-business/src/main/java/com/zc/business/controller/CodeScanningAlarmController.java b/zc-business/src/main/java/com/zc/business/controller/CodeScanningAlarmController.java new file mode 100644 index 00000000..4b1edea2 --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/controller/CodeScanningAlarmController.java @@ -0,0 +1,63 @@ +package com.zc.business.controller; + +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.utils.uuid.IdUtils; +import com.zc.business.domain.DcWarning; +import com.zc.business.service.IDcWarningService; +import io.swagger.annotations.Api; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; +import java.util.Objects; + +/** + * License + * + * @author Athena-xiepufeng + */ +@Api(tags = "扫码报警") +@RestController +@RequestMapping("/codeScanningAlarm") +public class CodeScanningAlarmController extends BaseController { + + @Resource + private IDcWarningService dcWarningService; + + /** + * 扫码报警事件回调函数 + * + * @param returnParameters 事件回参 + * @return 结果 + */ + @PostMapping(value = "/event") + public AjaxResult codeScanningAlarmControllerEvent(@RequestBody Map returnParameters) { + DcWarning dcWarning = new DcWarning(); + //设置事件Id UUID无下划线格式32 + String uuid = IdUtils.fastSimpleUUID(); + dcWarning.setId(uuid); + dcWarning.setStakeMark(String.valueOf(returnParameters.get("stakeNum"))); + String roadCode = String.valueOf(returnParameters.get("roadCode")); + dcWarning.setDirection(Objects.equals(roadCode, "1") ? "3" : "1"); + String createTime = String.valueOf(returnParameters.get("createTime")); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmXXX"); + try { + dcWarning.setWarningTime(format.parse(createTime)); + } catch (ParseException e) { + dcWarning.setWarningTime(new Date()); + } + dcWarning.setCreateTime(new Date()); + int warningType = Integer.parseInt(String.valueOf(returnParameters.get("accidentType"))) >= 4 ? 10 : 11; + dcWarning.setWarningType(warningType); + String warningTitle = "济荷高速:" + (Objects.equals(roadCode, "1") ? "济南方向" : "菏泽方向") + "发生" + (warningType == 10 ? "交通事故事件" : "车辆故障事件"); + dcWarning.setWarningTitle(warningTitle); + dcWarning.setOtherConfig(returnParameters.toString()); + dcWarning.setWarningSource(5); + dcWarning.setWarningState(1); + return AjaxResult.success(dcWarningService.insertDcWarning(dcWarning)); + } +}