Commit e72ab383 authored by wujipeng's avatar wujipeng
Browse files

道路详情优化

parent 9c5d4443
......@@ -69,11 +69,6 @@
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<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;
import com.example.pipelinenetwork.util.JwtUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
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.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@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
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws IOException {
try {
String authHeader = request.getHeader("Authorization");
if (authHeader != null && authHeader.startsWith("Bearer ")) {
String token = authHeader.substring(7);
if (jwtUtil.validateToken(token)) {
String username = jwtUtil.getUsernameFromToken(token);
UsernamePasswordAuthenticationToken authentication =
new UsernamePasswordAuthenticationToken(
username, null, new ArrayList<>());
SecurityContextHolder.getContext().setAuthentication(authentication);
} else {
sendErrorResponse(response, HttpServletResponse.SC_UNAUTHORIZED, "Token无效");
return;
}
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
// 设置字符编码和响应类型
request.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
// 获取请求路径
String requestURI = request.getRequestURI();
// 放行登录接口
if (LOGIN_PATH.equals(requestURI)) {
filterChain.doFilter(request, response);
} catch (Exception e) {
sendErrorResponse(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Authentication failed: " + e.getMessage());
return;
}
}
private void sendErrorResponse(HttpServletResponse response, int status, String message) throws IOException {
response.setStatus(status);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(String.format("{\"code\": %d, \"message\": \"%s\"}", status, message));
// 从请求头中获取 Token
String token = request.getHeader("Authorization");
// 检查 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;
import com.example.pipelinenetwork.service.DefectService;
import com.example.pipelinenetwork.service.RoadService;
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.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
......@@ -77,8 +78,12 @@ public class RoadServiceImpl extends ServiceImpl<RoadMapper, RoadEntity> impleme
List<String> relIds = new ArrayList<>();
for (BasicEntity basicEntity : list) {
relIds.add(basicEntity.getRelId());
pipeNum += basicEntity.getPipeNum();
length += basicEntity.getDetectionLength();
if(basicEntity.getPipeNum() != null){
pipeNum += basicEntity.getPipeNum();
}
if(basicEntity.getDetectionLength() != null){
length += basicEntity.getDetectionLength();
}
}
if (relIds.isEmpty()) {
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