Commit 4c13d8f6 authored by huangcb's avatar huangcb

新增AuthFilter过滤器

parent bb8adb0e
...@@ -134,7 +134,7 @@ ...@@ -134,7 +134,7 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<configuration> <configuration>
<skipTests>false</skipTests> <skipTests>true</skipTests>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
...@@ -204,18 +204,6 @@ ...@@ -204,18 +204,6 @@
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build> </build>
</project> </project>
package com.esv.freight.customer.common.filter;
import com.esv.freight.customer.common.response.EResponse;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @description: 权限过滤器
* @project: freight-customer-service
* @name: com.esv.freight.customer.common.filter.AuthFilter
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/04/23 14:11
* @version:1.0
*/
@Slf4j
public class AuthFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
// HttpServletRequest request = (HttpServletRequest) servletRequest;
// HttpServletResponse response = (HttpServletResponse) servletResponse;
// String accessToken = request.getHeader("Union-Authorization");
// if (StringUtils.isBlank(accessToken)) {
// this.errorResponse(EResponse.error(ECode.TOKEN_INVALID), response);
// return;
// } else {
// // 解析并校验Token
// }
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
private void errorResponse(EResponse eResponse, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
OutputStreamWriter osw = null;
PrintWriter writer = null;
try {
osw = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
writer = new PrintWriter(osw, true);
writer.write(eResponse.toString());
writer.flush();
osw.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
if (null != writer) {
writer.close();
}
if (null != osw) {
try {
osw.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
}
package com.esv.freight.customer.common.response; package com.esv.freight.customer.common.response;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.io.Serializable; import java.io.Serializable;
/** /**
...@@ -153,4 +156,9 @@ public class EResponse<T> implements Serializable { ...@@ -153,4 +156,9 @@ public class EResponse<T> implements Serializable {
public void setData(T data) { public void setData(T data) {
this.data = data; this.data = data;
} }
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
}
} }
\ No newline at end of file
package com.esv.freight.customer.config; package com.esv.freight.customer.config;
import com.esv.freight.customer.common.filter.AuthFilter;
import com.esv.freight.customer.common.filter.LogbackFilter; import com.esv.freight.customer.common.filter.LogbackFilter;
import com.esv.freight.customer.common.filter.RestLogFilter; import com.esv.freight.customer.common.filter.RestLogFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
/** /**
* @description: 注册请求Filter * @description: 注册请求Filter
* @project: SpringCloudTemplate * @project: SpringCloudTemplate
...@@ -37,4 +40,18 @@ public class LogFilterConfig { ...@@ -37,4 +40,18 @@ public class LogFilterConfig {
filterRegistrationBean.setOrder(Integer.MIN_VALUE + 1); filterRegistrationBean.setOrder(Integer.MIN_VALUE + 1);
return filterRegistrationBean; return filterRegistrationBean;
} }
@Bean
public AuthFilter getAuthFilter() {
return new AuthFilter();
}
@Bean
public FilterRegistrationBean<AuthFilter> authFilterRegister() {
FilterRegistrationBean<AuthFilter> filterRegistrationBean=new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(getAuthFilter());
filterRegistrationBean.addUrlPatterns(URL);
filterRegistrationBean.setOrder(Integer.MIN_VALUE + 2);
return filterRegistrationBean;
}
} }
...@@ -3,8 +3,8 @@ server: ...@@ -3,8 +3,8 @@ server:
servlet: servlet:
context-path: /customer context-path: /customer
nacos: nacos:
url: 192.168.31.248:8848 url: 127.0.0.1:8848
namespace: aad5aa26-5351-4e7a-a65e-ecb332f3c52c namespace: 548b506d-8d19-4d54-9715-bb0ac3a655b2
group: FREIGHT_GROUP group: FREIGHT_GROUP
spring: spring:
application: application:
......
...@@ -48,6 +48,11 @@ ...@@ -48,6 +48,11 @@
<appender-ref ref="FILE_APPENDER" /> <appender-ref ref="FILE_APPENDER" />
</logger> </logger>
<logger name="com.esv.freight.customer.config.mybatis" level="INFO" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</logger>
<!--控制台和日志文件输出级别--> <!--控制台和日志文件输出级别-->
<root level="INFO" additivity="false"> <root level="INFO" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" /> <appender-ref ref="CONSOLE_APPENDER" />
......
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