Commit e72ab383 authored by wujipeng's avatar wujipeng
Browse files

道路详情优化

parent 9c5d4443
...@@ -69,11 +69,6 @@ ...@@ -69,11 +69,6 @@
<artifactId>jjwt</artifactId> <artifactId>jjwt</artifactId>
<version>0.9.1</version> <version>0.9.1</version>
</dependency> </dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.example.pipelinenetwork.config;
import com.example.pipelinenetwork.filter.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class FilterConfig {
@Resource
private JwtAuthenticationFilter jwtAuthenticationFilter;
@Bean
public FilterRegistrationBean<JwtAuthenticationFilter> jwtFilterRegistration() {
FilterRegistrationBean<JwtAuthenticationFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(jwtAuthenticationFilter);
// 拦截所有请求
registration.addUrlPatterns("/*");
// 设置优先级,数值越小优先级越高
registration.setOrder(1);
return registration;
}
}
\ No newline at end of file
package com.example.pipelinenetwork.config;
import com.example.pipelinenetwork.filter.JwtAuthenticationFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.servlet.http.HttpServletResponse;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final JwtAuthenticationFilter jwtAuthenticationFilter;
public SecurityConfig(JwtAuthenticationFilter jwtAuthenticationFilter) {
this.jwtAuthenticationFilter = jwtAuthenticationFilter;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.authenticationEntryPoint((request, response, authException) -> {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{\"code\":401,\"message\":\"未登录或Token已过期\"}");
})
.accessDeniedHandler((request, response, accessDeniedException) -> {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{\"code\":403,\"message\":\"没有访问权限\"}");
})
.and()
.authorizeRequests()
.antMatchers("/login/in").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
\ No newline at end of file
package com.example.pipelinenetwork.filter; package com.example.pipelinenetwork.filter;
import com.example.pipelinenetwork.util.JwtUtil; import com.example.pipelinenetwork.util.JwtUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.util.StringUtils;
import javax.servlet.FilterChain; import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
@Component @Component
public class JwtAuthenticationFilter extends OncePerRequestFilter { public class JwtAuthenticationFilter implements Filter {
private final JwtUtil jwtUtil; @Resource
private JwtUtil jwtUtil;
public JwtAuthenticationFilter(JwtUtil jwtUtil) { // 需要放行的路径
this.jwtUtil = jwtUtil; private static final String LOGIN_PATH = "/login/in";
}
@Override @Override
protected void doFilterInternal(HttpServletRequest request, public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
HttpServletResponse response, throws IOException, ServletException {
FilterChain filterChain)
throws IOException { HttpServletRequest request = (HttpServletRequest) servletRequest;
try { HttpServletResponse response = (HttpServletResponse) servletResponse;
String authHeader = request.getHeader("Authorization");
if (authHeader != null && authHeader.startsWith("Bearer ")) { // 设置字符编码和响应类型
String token = authHeader.substring(7); request.setCharacterEncoding("UTF-8");
if (jwtUtil.validateToken(token)) { response.setContentType("application/json;charset=UTF-8");
String username = jwtUtil.getUsernameFromToken(token);
UsernamePasswordAuthenticationToken authentication = // 获取请求路径
new UsernamePasswordAuthenticationToken( String requestURI = request.getRequestURI();
username, null, new ArrayList<>());
SecurityContextHolder.getContext().setAuthentication(authentication); // 放行登录接口
} else { if (LOGIN_PATH.equals(requestURI)) {
sendErrorResponse(response, HttpServletResponse.SC_UNAUTHORIZED, "Token无效");
return;
}
}
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} catch (Exception e) { return;
sendErrorResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Authentication failed: " + e.getMessage());
} }
}
private void sendErrorResponse(HttpServletResponse response, int status, String message) throws IOException { // 从请求头中获取 Token
response.setStatus(status); String token = request.getHeader("Authorization");
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(String.format("{\"code\": %d, \"message\": \"%s\"}", status, message)); // 检查 Token 是否存在且以 "Bearer " 开头
if (!StringUtils.hasText(token) || !token.startsWith("Bearer ")) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write("{\"code\":401,\"message\":\"未提供有效的Token\"}");
return;
}
// 去掉 "Bearer " 前缀,获取实际的 Token
token = token.substring(7);
// 验证 Token 是否有效
if (!jwtUtil.validateToken(token)) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write("{\"code\":401,\"message\":\"Token无效或已过期\"}");
return;
}
// 将用户名存入请求属性中,方便后续使用
String username = jwtUtil.getUsernameFromToken(token);
request.setAttribute("username", username);
// 放行请求
filterChain.doFilter(request, response);
} }
} }
\ No newline at end of file
...@@ -13,6 +13,7 @@ import com.example.pipelinenetwork.service.BasicService; ...@@ -13,6 +13,7 @@ import com.example.pipelinenetwork.service.BasicService;
import com.example.pipelinenetwork.service.DefectService; import com.example.pipelinenetwork.service.DefectService;
import com.example.pipelinenetwork.service.RoadService; import com.example.pipelinenetwork.service.RoadService;
import com.example.pipelinenetwork.util.LinestringUtil; import com.example.pipelinenetwork.util.LinestringUtil;
import com.example.pipelinenetwork.util.NumberUtils;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
...@@ -77,8 +78,12 @@ public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> impleme ...@@ -77,8 +78,12 @@ public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> impleme
List<String> relIds = new ArrayList<>(); List<String> relIds = new ArrayList<>();
for (BasicEntity basicEntity : list) { for (BasicEntity basicEntity : list) {
relIds.add(basicEntity.getRelId()); relIds.add(basicEntity.getRelId());
pipeNum += basicEntity.getPipeNum(); if(basicEntity.getPipeNum() != null){
length += basicEntity.getDetectionLength(); pipeNum += basicEntity.getPipeNum();
}
if(basicEntity.getDetectionLength() != null){
length += basicEntity.getDetectionLength();
}
} }
if (relIds.isEmpty()) { if (relIds.isEmpty()) {
return Result.error("未找到该名称的路段"); return Result.error("未找到该名称的路段");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment