This commit is contained in:
lsy
2017-10-24 21:21:44 +08:00
parent 40a88f1c72
commit 8267d92780
10 changed files with 200 additions and 18 deletions
@@ -48,6 +48,10 @@
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
@@ -0,0 +1,47 @@
package com.micro.fast.security.demo.Ineterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 不能获取到处理请求方法中的参数的值
*/
@Component
public class MyInterceptor implements HandlerInterceptor {
//请求之前,
@Override
public boolean preHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object handler)
throws Exception {
//获取请求的类
System.out.println(((HandlerMethod) handler).getBean().getClass().getName());
//获取拦截的方法
System.out.println(((HandlerMethod) handler).getMethod().getName());
return false;
}
//** 请求之后,控制器中抛出了异常的话就不会执行
@Override
public void postHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object handler, ModelAndView modelAndView)
throws Exception {
}
//** 请求之后调用,不管抛不抛出异常都会被调用.参数中异常如果被异常处理器调用的话就不会传入到参数中
@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object handler,
Exception e)
throws Exception {
}
}
@@ -0,0 +1,28 @@
package com.micro.fast.security.demo.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* com.micro.fast.security.demo.controller.*(..))")
public void pointcut(){
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point){
Object[] args = point.getArgs();
try {
//被拦截的方法
Object object = point.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
@@ -0,0 +1,30 @@
package com.micro.fast.security.demo.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* 拦截所有服务的请求获取服务的信息,filter拦截器不能知道filter是哪个controller处理的请求,只能获取请求中的信息
* 拦截顺序Filter->Interceptor->ControllerAdvice->Aspect->Controller 发生异常的时候是反向的传递
*/
@WebFilter(urlPatterns = {"/**"})
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
@@ -0,0 +1,37 @@
package com.micro.fast.security.demo.filter;
import com.micro.fast.security.demo.Ineterceptor.MyInterceptor;
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.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 注册外部的filter,
*/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
@Autowired
private MyInterceptor myInterceptor;
/**
* 添加拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor);
}
@Bean
public FilterRegistrationBean timeFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
String[] urls = new String[]{"/**"};
filterRegistrationBean.addUrlPatterns(urls);
return filterRegistrationBean;
}
}
@@ -1,14 +1,3 @@
# 程序实例的配置,git仓库的配置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://git.oschina.net/kklt1996/config-server/
username: kklt1996
password: studyj2e
server:
port: 9999
# 健康检查的相关配置
@@ -28,12 +17,6 @@ info:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
eureka:
client:
service-url:
defaultZone: http://micro:fast@127.0.0.1:10002/eureka/,http://micro:fast@127.0.0.1:10003/eureka/ #配置所有的注册中心
instance:
hostname: www.config.server.com
security:
basic:
enabled: true
@@ -41,3 +24,4 @@ security:
name: config
password: server
@@ -0,0 +1,17 @@
# 程序实例的配置,git仓库的配置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://git.oschina.net/kklt1996/config-server/
username: kklt1996
password: studyj2e
eureka:
client:
service-url:
defaultZone: http://micro:fast@127.0.0.1:10002/eureka/,http://micro:fast@127.0.0.1:10003/eureka/ #配置所有的注册中心
instance:
hostname: www.config.server.com
@@ -0,0 +1,17 @@
# 程序实例的配置,git仓库的配置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://git.oschina.net/kklt1996/config-server/
username: kklt1996
password: studyj2e
eureka:
client:
service-url:
defaultZone: http://micro:fast@127.0.0.1:10002/eureka/,http://micro:fast@127.0.0.1:10003/eureka/ #配置所有的注册中心
instance:
hostname: www.config.server.com
+1 -1
View File
@@ -15,4 +15,4 @@ eureka:
serviceUrl:
defaultZone: http://micro:fast@127.0.0.1:10002/eureka/,http://micro:fast@127.0.0.1:10003/eureka/ #配置所有的注册中心
instance:
hostname: www.gateway.com
hostname: www.gateway.com
@@ -0,0 +1,18 @@
# 从配置中心加载文件
spring:
cloud:
config:
profile: dev
label: master
name: upms
discovery:
service-id: config-server
enabled: true
username: config
password: server
eureka:
client:
serviceUrl:
defaultZone: http://micro:fast@127.0.0.1:10002/eureka/,http://micro:fast@127.0.0.1:10003/eureka/ #配置所有的注册中心
instance:
hostname: www.upms.com