配置拦截器

This commit is contained in:
sin 2019-04-10 20:48:35 +08:00
parent 56b8a053df
commit c24697fd1b
3 changed files with 38 additions and 2 deletions

View File

@ -27,6 +27,11 @@
<artifactId>admin-sdk</artifactId> <artifactId>admin-sdk</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>user-sdk</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency> <dependency>
<groupId>cn.iocoder.mall</groupId> <groupId>cn.iocoder.mall</groupId>
<artifactId>common-framework</artifactId> <artifactId>common-framework</artifactId>

View File

@ -1,10 +1,22 @@
package cn.iocoder.mall.order.application.config; package cn.iocoder.mall.order.application.config;
import cn.iocoder.common.framework.config.GlobalExceptionHandler;
import cn.iocoder.common.framework.servlet.CorsFilter;
import cn.iocoder.mall.admin.sdk.interceptor.AdminSecurityInterceptor;
import cn.iocoder.mall.user.sdk.interceptor.UserAccessLogInterceptor;
import cn.iocoder.mall.user.sdk.interceptor.UserSecurityInterceptor;
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 org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.*; import org.springframework.web.servlet.config.annotation.*;
@EnableWebMvc @EnableWebMvc
@Configuration @Configuration
@Import(value = {GlobalExceptionHandler.class, // 统一全局返回
UserSecurityInterceptor.class, AdminSecurityInterceptor.class,
UserAccessLogInterceptor.class}) // 安全拦截器实现认证和授权功能
public class MVCConfiguration implements WebMvcConfigurer { public class MVCConfiguration implements WebMvcConfigurer {
// @Autowired // @Autowired
@ -13,9 +25,28 @@ public class MVCConfiguration implements WebMvcConfigurer {
// @Reference // @Reference
// private OAuth2Service oauth2Service; // private OAuth2Service oauth2Service;
@Autowired
private UserSecurityInterceptor userSecurityInterceptor;
@Autowired
private UserAccessLogInterceptor userAccessLogInterceptor;
@Autowired
private AdminSecurityInterceptor adminSecurityInterceptor;
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(securityInterceptor); // 用户
registry.addInterceptor(userAccessLogInterceptor).addPathPatterns("/users/**");
registry.addInterceptor(userSecurityInterceptor).addPathPatterns("/users/**"); // 只拦截我们定义的接口
// 管理员
registry.addInterceptor(adminSecurityInterceptor).addPathPatterns("/admins/**"); // 只拦截我们定义的接口
}
@Bean
public FilterRegistrationBean<CorsFilter> corsFilter() {
FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new CorsFilter());
registrationBean.addUrlPatterns("/*");
return registrationBean;
} }
// @Override // @Override

View File

@ -4,6 +4,6 @@ spring:
# server # server
server: server:
port: 18084 port: 18088
servlet: servlet:
context-path: /order-api/ context-path: /order-api/