提交 1f307777 编写于 作者: Q qinyingjie

fix:配置拦截器

上级 3d06f36f
......@@ -74,8 +74,15 @@
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>
<plugins>
......@@ -92,5 +99,16 @@
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
\ No newline at end of file
package com.kwan.springbootkwan.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* AOP 实现方法拦截
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/19 16:17
*/
@Component
@Aspect
public class LogAspect {
@Pointcut(value = "execution(* com.kwan.springbootkwan.service.*.*(..))")
public void pcl() {
}
@Before(value = "pcl()")
public void before(JoinPoint point) {
String name = point.getSignature().getName();
System.out.println(name + "方法开始执行 ...");
}
@After(value = "pcl()")
public void after(JoinPoint point) {
String name = point.getSignature().getName();
System.out.println(name + "方法执行结束 ...");
}
@AfterReturning(value = "pcl()", returning = "result")
public void afterReturning(JoinPoint point, Object result) {
String name = point.getSignature().getName();
System.out.println(name + "方法返回值为:" + result);
}
@AfterThrowing(value = "pcl()", throwing = "e")
public void afterThrowing(JoinPoint point, Exception e) {
String name = point.getSignature().getName();
System.out.println(name + "方法抛异常了,异常是:" + e.getMessage());
}
@Around("pcl()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();
}
}
package com.kwan.springbootkwan.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.one")
DataSource dataSourceOne() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.two")
DataSource dataSourceTwo() {
return DruidDataSourceBuilder.create().build();
}
}
package com.kwan.springbootkwan.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义拦截器
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2022/12/19 16:14
*/
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("MyInterceptor>>>>>>>>>preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor>>>>>>>>>postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor>>>>>>>>>afterCompletion");
}
}
......@@ -2,11 +2,12 @@ package com.kwan.springbootkwan.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 全局拦截器-跨域配置
* 全局webconfig-跨域配置
*
* @author : qinyingjie
* @version : 2.2.0
......@@ -14,12 +15,34 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
*/
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
/**
* 配置静态资源
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
/**
* 配置拦截器
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/hello");
}
/**
* 配置全局跨域
*
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/book/**")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册