up:: SpringCloud之网关模块开发

说明: 本篇博客涉及的知识点有模块间方法的调用,学习前最好复习下以前知识点,看本篇博客内容就不会一头雾水。。。

指路: 利用Feign实现服务间调用


开发管理员鉴权

package com.imooc.cloud.mall.practice.zuul.filter;
 
import com.imooc.cloud.mall.practice.common.common.Constant;
import com.imooc.cloud.mall.practice.user.model.pojo.User;
import com.imooc.cloud.mall.practice.zuul.feign.UserFeignClient;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
 
/**
 * 描述:     管理员鉴权过滤器
 */
@Component
public class AdminFilter extends ZuulFilter {
 
    @Autowired
    UserFeignClient userFeignClient;
 
 
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }
 
    @Override
    public int filterOrder() {
        return 0;
    }
 
    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String requestURI = request.getRequestURI();
        if (requestURI.contains("adminLogin")) {
            return false;
        }
        if (requestURI.contains("admin")) {
            return true;
        }
        return false;
    }
 
    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        HttpSession session = request.getSession();
        User currentUser = (User)session.getAttribute(Constant.IMOOC_MALL_USER);
        if (currentUser == null) {
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseBody("{\n"
                    + "    \"status\": 10010,\n"
                    + "    \"msg\": \"NEED_LOGIN\",\n"
                    + "    \"data\": null\n"
                    + "}");
            currentContext.setResponseStatusCode(200);
            return null;
        }
 
        //校验是否是管理员
        Boolean adminRole = userFeignClient.checkAdminRole(currentUser);
        if (!adminRole) {
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseBody("{\n"
                    + "    \"status\": 10011,\n"
                    + "    \"msg\": \"NEED_ADMIN\",\n"
                    + "    \"data\": null\n"
                    + "}");
            currentContext.setResponseStatusCode(200);
        }
        return null;
    }
}

说明: 这里调用了用户模块的方法。。。

UserController层编写调用service验证是否管理员登陆的方法;

 
/**
 * 校验是否是管理员
 * @param user
 * @return
 */
@PostMapping("/checkAdminRole")
@ResponseBody
public Boolean checkAdminRole(@RequestBody User user) {
 
    return userService.checkAdminRole(user);
}

接下来就是开发调用用户模块

参考学习利用Feign实现服务间调用

package com.imooc.cloud.mall.practice.zuul.feign;
 
import com.imooc.cloud.mall.practice.user.model.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
 
/**
 * 描述:     UserFeignClient
 */@FeignClient(value = "cloud-mall-user")
public interface UserFeignClient {
 
    @PostMapping("/checkAdminRole")
    public Boolean checkAdminRole(@RequestBody User user);
}

开发启动类

package com.imooc.cloud.mall.practice.zuul;
 
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
 
/**
 * 描述:     网关启动类
 */
@EnableZuulProxy
@EnableFeignClients
@SpringCloudApplication
public class ZuulGatewayApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayApplication.class, args);
    }
}

说明: 各个注解的功能前面已经讲过了嘞。。。

启动测试!!!

无法找到URL

配置过滤URL

再次测试

测试登陆后的功能,比如更新个性签名

说明:

这里问题就是我们下面要讲的session共享问题。。。