springcloud+zuul+eureka实现简单认证

springcloud + zuul+eureka 实现简单版认证

实现步骤

  1. 搭建注册中心:服务注册eureka

  2. 搭建商品服务:测试认证是否通过

  3. 搭建用户服务:用于认证

  4. 搭建网关zuul

搭建注册中心

  1. pom.xml

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.3.7.RELEASE
             
        
        com.example
        eureka-server
        0.0.1-SNAPSHOT
        eureka-server
        Demo project for Spring Boot
    
        
            1.8
            Hoxton.SR9
        
    
        
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-server
            
    
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
                org.projectlombok
                lombok
                true
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
                
                    
                        org.junit.vintage
                        junit-vintage-engine
                    
                
            
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        
                            
                                org.projectlombok
                                lombok
                            
                        
                    
                
            
        
    
    
    
    
  2. application.properties

    server.port=8000
    spring.application.name=eureka-server
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka
    eureka.client.fetch-registry=false
    eureka.client.register-with-eureka=false
    
    
  3. 启动类

    package com.example.eurekaserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    
    }
    
    

搭建商品服务

  1. pom.xml

      
      
          4.0.0
          
              org.springframework.boot
              spring-boot-starter-parent
              2.3.7.RELEASE
               
          
          com.example
          product-server
          0.0.1-SNAPSHOT
          product-server
          Demo project for Spring Boot
    
          
              1.8
              Hoxton.SR9
          
    
          
    
              
                  org.springframework.cloud
                  spring-cloud-starter-netflix-eureka-client
              
    
              
                  org.springframework.boot
                  spring-boot-starter-web
              
              
                  org.springframework.cloud
                  spring-cloud-starter-openfeign
              
    
              
                  org.springframework.boot
                  spring-boot-devtools
                  runtime
                  true
              
              
                  org.projectlombok
                  lombok
                  true
              
              
                  org.springframework.boot
                  spring-boot-starter-test
                  test
                  
                      
                          org.junit.vintage
                          junit-vintage-engine
                      
                  
              
          
    
          
              
                  
                      org.springframework.cloud
                      spring-cloud-dependencies
                      ${spring-cloud.version}
                      pom
                      import
                  
              
          
    
          
              
                  
                      org.springframework.boot
                      spring-boot-maven-plugin
                      
                          
                              
                                  org.projectlombok
                                  lombok
                              
                          
                      
                  
              
          
    
      
    
    
  2. application.properties

    server.port=8001
    spring.application.name=product-server
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka
    
    
  3. 启动类:

    package com.example.productserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ProductServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProductServerApplication.class, args);
        }
    
    }
    
    
  4. api接口

    package com.example.productserver;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
    * @author POEAM
    * @date 2021/1/11
    */
    @RestController
    @RequestMapping("products")
    public class ProductController {
    
        @GetMapping
        public String list(
                @RequestHeader(value = "token",required = false) String token,
                @RequestHeader(value = "Authorization",required = false) String Authorization) {
            return "product pro:"+token+":"+Authorization;
        }
    
    
    }
    
    

搭建用户服务

  1. pom.xml

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.3.7.RELEASE
             
        
        com.example
        user-server
        0.0.1-SNAPSHOT
        user-server
        Demo project for Spring Boot
    
        
            1.8
            Hoxton.SR9
        
    
        
            
                org.springframework.boot
                spring-boot-starter-data-redis
            
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-client
            
    
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
                org.projectlombok
                lombok
                true
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
                
                    
                        org.junit.vintage
                        junit-vintage-engine
                    
                
            
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        
                            
                                org.projectlombok
                                lombok
                            
                        
                    
                
            
        
    
    
    
    
  2. application.properties

    server.port=8080
    spring.application.name=user-server
    eureka.client.service-url.defaultZone=http://localhost:8000/eureka
    
    
  3. 启动类:

    package com.example.userserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class UserServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(UserServerApplication.class, args);
        }
    
    }
    
    
  4. api接口:登录:

    package com.example.userserver;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.UUID;
    import java.util.concurrent.TimeUnit;
    
    @RestController
    @RequestMapping("users")
    public class UserController {
    
        @Autowired
        StringRedisTemplate redisTemplate;
    
    
        @GetMapping
        public String login(HttpServletRequest request, String uname, String upass) {//Chrome
          // request.get
            //登录成功
            if("admin".equals(uname)&&"admin".equals(upass)){
                //生成token
                String token = UUID.randomUUID().toString().replace("-", "");
                // 用户信息写入redis->token:用户信息
                redisTemplate.opsForValue().set(token,"admin",1, TimeUnit.DAYS);
                // 生成token,返回
                return token;
    
            }else{
                //返回错误信息
                return "erro";
            }
    
        }
    
    
    
    }
    
    

搭建网关服务

  • pom.xml

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.3.7.RELEASE
             
        
        com.example
        zuul-server
        0.0.1-SNAPSHOT
        zuul-server
        Demo project for Spring Boot
    
        
            1.8
            Hoxton.SR9
        
    
        
    
    
    
            
                org.springframework.boot
                spring-boot-starter-web
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-client
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-zuul
            
    
            
                org.springframework.boot
                spring-boot-devtools
                runtime
                true
            
            
                org.projectlombok
                lombok
                true
            
            
                org.springframework.boot
                spring-boot-starter-test
                test
                
                    
                        org.junit.vintage
                        junit-vintage-engine
                    
                
            
    
    
            
                org.springframework.boot
                spring-boot-starter-data-redis
            
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
            
        
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        
                            
                                org.projectlombok
                                lombok
                            
                        
                    
                
            
        
    
    
    
    
  • application.properties

    ```pro
    server:
        port: 80
    spring:
        application:
            name: zuul-server
    eureka:
        client:
            service-url:
                defaultZone: http://localhost:8000/eureka
    
    zuul:
        routes:
            baidu:
                path: /baidu/** # http://localhost/baidu/nihao
                url: http://www.baidu.com
        sensitiveHeaders:
    logiApi: users
    
    
    ```
    
  • 启动类:

    package com.example.zuulserver;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    
    @SpringBootApplication
    @EnableZuulProxy
    public class ZuulServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ZuulServerApplication.class, args);
        }
    
    }
    
    
  • 登录拦截器:

    package com.example.zuulserver;
    
    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.context.RequestContext;
    import com.netflix.zuul.exception.ZuulException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Component;
    import org.springframework.util.ObjectUtils;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
    * 登录拦截过滤器
    *
    * */
    @Component
    public class LoginFilter  extends ZuulFilter {
    
    
        @Autowired
        StringRedisTemplate stringRedisTemplate;
    
        @Value("${logiApi}")
        String logiApi;
    
        /**
        * filter的类型 pre:(路有前) route(路由时:ribbonroutefilter)  post:(路由后) error(错误)
        * @return
        */
        @Override
        public String filterType() {
            return FilterConstants.PRE_TYPE;
        }
    
        /**
        * 执行的顺序 越小越先执行
        * */
        @Override
        public int filterOrder() {
            return FilterConstants.DEBUG_FILTER_ORDER-1;
        }
    
        /**
        *表示当前过滤器是否生效->争对请求
        *
        * 没带token:true
        * 有:false
        * /登录:login:false
        * */
        @Override
        public boolean shouldFilter() {
            //1.获取当前的requset
            HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
            //1.1获取当前请求的uri
            String uri = request.getRequestURI();
            //2.从request中提取token
            String token = request.getHeader("Authorization");
    // 3.判断是否存在
            //todo 每次查询都去redis中检测是否存在:1.网络问题,2redis故障->jwt
            Boolean hasKey = stringRedisTemplate.hasKey(token+"");
            //如果不带token并且不是以login结尾,不放行
            if(!hasKey && !uri.endsWith(logiApi)){
                return true;
            }
            return false;
        }
    
        /**
        *如果过滤器对当前请求生效,则执行此方法
        * :返回401,当前请求不能继续路由下去
        * */
        @Override
        public Object run() throws ZuulException {
    //        获取当前的请求信息
            RequestContext context = RequestContext.getCurrentContext();
    //中断流程
            context.setSendZuulResponse(false);
    
    //        获取response
            HttpServletResponse response = context.getResponse();
    //        写回状态码
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            try {
    //   写回错误信息
                response.getWriter().write(HttpStatus.UNAUTHORIZED.getReasonPhrase());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    
    

gitee仓库地址

你可能感兴趣的:(springcloud+zuul+eureka实现简单认证)