This commit is contained in:
awin-x 2024-12-21 15:52:15 +08:00
parent 7c6233f569
commit a63967227a
10 changed files with 77 additions and 53 deletions

View File

@ -16,17 +16,10 @@
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="test05" /> <module name="test05" />
</profile> </profile>
<profile name="Annotation profile for learn03-springboot" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<processorPath>
<entry name="D:/software/dev/maven/apache-maven-3.9.9/repository/org/projectlombok/lombok/release/lombok-release.jar" />
</processorPath>
<module name="learn03-springboot" />
</profile>
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel target="21" /> <bytecodeTargetLevel target="21">
<module name="learn03-springboot" target="21" />
</bytecodeTargetLevel>
</component> </component>
<component name="JavacSettings"> <component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE"> <option name="ADDITIONAL_OPTIONS_OVERRIDE">

View File

@ -9,6 +9,11 @@
<option value="$PROJECT_DIR$/test05/pom.xml" /> <option value="$PROJECT_DIR$/test05/pom.xml" />
</list> </list>
</option> </option>
<option name="ignoredFiles">
<set>
<option value="$PROJECT_DIR$/learn03-springboot/pom.xml" />
</set>
</option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />

View File

@ -6,7 +6,6 @@
<module fileurl="file://$PROJECT_DIR$/learn02/learn02.iml" filepath="$PROJECT_DIR$/learn02/learn02.iml" /> <module fileurl="file://$PROJECT_DIR$/learn02/learn02.iml" filepath="$PROJECT_DIR$/learn02/learn02.iml" />
<module fileurl="file://$PROJECT_DIR$/learn1/learn1.iml" filepath="$PROJECT_DIR$/learn1/learn1.iml" /> <module fileurl="file://$PROJECT_DIR$/learn1/learn1.iml" filepath="$PROJECT_DIR$/learn1/learn1.iml" />
<module fileurl="file://$PROJECT_DIR$/test02/test02.iml" filepath="$PROJECT_DIR$/test02/test02.iml" /> <module fileurl="file://$PROJECT_DIR$/test02/test02.iml" filepath="$PROJECT_DIR$/test02/test02.iml" />
<module fileurl="file://$PROJECT_DIR$/test03/test03.iml" filepath="$PROJECT_DIR$/test03/test03.iml" />
<module fileurl="file://$PROJECT_DIR$/test04/test04.iml" filepath="$PROJECT_DIR$/test04/test04.iml" /> <module fileurl="file://$PROJECT_DIR$/test04/test04.iml" filepath="$PROJECT_DIR$/test04/test04.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/web实验.iml" filepath="$PROJECT_DIR$/.idea/web实验.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/web实验.iml" filepath="$PROJECT_DIR$/.idea/web实验.iml" />
</modules> </modules>

View File

@ -4,7 +4,6 @@ import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import top.awin_x.learn.pojo.CourseInfo; import top.awin_x.learn.pojo.CourseInfo;
import top.awin_x.learn.pojo.Result; import top.awin_x.learn.pojo.Result;
import top.awin_x.learn.service.impl.CourseServiceImpl;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -89,6 +89,13 @@
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.53</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -4,10 +4,8 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication @SpringBootApplication
public class Test05Application { public class Test05Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Test05Application.class, args); SpringApplication.run(Test05Application.class, args);
} }

View File

@ -0,0 +1,23 @@
package test05.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import test05.interceptor.LoginCheckInterceptor;
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final LoginCheckInterceptor loginCheckInterceptor;
@Autowired
WebConfig(LoginCheckInterceptor loginCheckInterceptor){
this.loginCheckInterceptor = loginCheckInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/**").excludePathPatterns("/login");
}
}

View File

@ -3,6 +3,7 @@ package test05.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import test05.pojo.Result;
import test05.service.StudentService; import test05.service.StudentService;
@RestController @RestController
@ -15,5 +16,9 @@ public class studentController {
} }
@RequestMapping("get") @RequestMapping("get")
public String get() {} public Result get() {
Result result = new Result();
return result;
}
} }

View File

@ -1,38 +0,0 @@
package test05.filter;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import test05.utils.JwtUtils;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
//@WebFilter("/*")
public class loginFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (((HttpServletRequest) servletRequest).getRequestURI().equals("/userAction/login")) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
String token = ((HttpServletRequest) servletRequest).getHeader("token");
System.out.println("token:" + token);
if (token != null) {
try{
Map<String, Object> jwtContent = JwtUtils.parseJwt(token);
}catch(SignatureException | ExpiredJwtException | MalformedJwtException e){
((HttpServletResponse) servletResponse).sendError(HttpServletResponse.SC_FORBIDDEN);
}
filterChain.doFilter(servletRequest, servletResponse);
}else{
((HttpServletResponse) servletResponse).sendError(HttpServletResponse.SC_FORBIDDEN);
}
}
}

View File

@ -0,0 +1,33 @@
package test05.interceptor;
import com.alibaba.fastjson2.JSONObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import test05.pojo.Result;
import test05.utils.JwtUtils;
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");
try{
JwtUtils.parseJwt(token);
}catch(Exception e){;
response.getWriter().print(JSONObject.toJSONString(Result.error("not login")));
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}