SpringCloud系列十二:手动创建Feign
1. 回顾
上文讲解了自定义Feign。但是在某些场景下,前文自定义Feign的方式满足不了需求,此时可使用Feign Builder API手动创建Feign。
本文围绕以下场景,为大家讲解如何手动创建Feign。
- 用户微服务的接口需要登录后才能调用,并且对于相同的API,不同角色的用户有不同的行为。
- 让电影微服务中的同一个Feign接口,使用不同的账号登录,并调用用户微服务的接口。
2. 修改用户微服务
> 复制项目 microservice-provider-user,将 ArtifactId 修改为 microservice-provider-user-with-auth
> 添加Spring Security依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
> 创建 Spring Security的配置类
package com.itmuch.cloud.microserviceprovideruserwithauth.security; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import java.util.ArrayList;
import java.util.Collection; public class SecurityUser implements UserDetails { private static final long serialVersoinUID = 1L; private Long id;
private String username;
private String password;
private String role; public SecurityUser() {
} public SecurityUser(String username, String password, String role) {
this.username = username;
this.password = password;
this.role = role;
} @Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(this.role);
authorities.add(authority);
return authorities;
} @Override
public String getPassword() {
return password;
} @Override
public String getUsername() {
return username;
} @Override
public boolean isAccountNonExpired() {
return true;
} @Override
public boolean isAccountNonLocked() {
return true;
} @Override
public boolean isCredentialsNonExpired() {
return true;
} @Override
public boolean isEnabled() {
return true;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public void setUsername(String username) {
this.username = username;
} public void setPassword(String password) {
this.password = password;
} public String getRole() {
return role;
} public void setRole(String role) {
this.role = role;
}
}
package com.itmuch.cloud.microserviceprovideruserwithauth.security; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component; @Component
public class CustomUserDetailsService implements UserDetailsService { /**
* 模拟两个账户
* ① 账号是user,密码是password1,角色是user-role
* ② 账号时候admin,密码是password1,角色是admin-role
* @param username
* 用户名
* @return
*
* @throws UsernameNotFoundException
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
if ("user".equals(username)) {
return new SecurityUser("user", "password1", "user-role");
} else if ("admin".equals(username)) {
return new SecurityUser("admin", "password2", "admin-role");
} else {
return null;
}
}
}
package com.itmuch.cloud.microserviceprovideruserwithauth.security; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired
private CustomUserDetailsService userDetailsService; @Override
protected void configure(HttpSecurity http) throws Exception {
// 所有的请求,都需要经过HTTP basic认证
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
} @Bean
public PasswordEncoder passwordEncoder() {
// 明文编码器。这个一个不做任何操作的密码编码器,是Spring提供给我们做明文测试的
return NoOpPasswordEncoder.getInstance();
} @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(this.userDetailsService).passwordEncoder(this.passwordEncoder());
}
}
> 修改Controller,在其中打印当前登录的用户信息
package com.itmuch.cloud.microserviceprovideruserwithauth.controller; import com.itmuch.cloud.microserviceprovideruserwithauth.dao.UserRepository;
import com.itmuch.cloud.microserviceprovideruserwithauth.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import java.util.Collection; @RestController
public class UserController { private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class); @Autowired
private UserRepository userRepository; @GetMapping("/{id}")
public User findById(@PathVariable Long id) {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof UserDetails) {
UserDetails user = (UserDetails) principal;
Collection<? extends GrantedAuthority> collections = user.getAuthorities();
for (GrantedAuthority ga: collections) {
// 打印当前登录用户的信息
UserController.LOGGER.info("当前用户是{}, 角色是{}", user.getUsername(), ga.getAuthority());
}
} else {
UserController.LOGGER.warn("ε=(´ο`*)))唉,出现问题了");
}
User findOne = userRepository.findById(id).get();
return findOne;
} }
> 启动 microservice-discovery-eureka
> 启动 microservice-provider-user-with-auth
> 访问 http://localhost:8000/1,弹出登录对话框

> 使用 user/password1 登录,可在控制台看到如下日志

> 使用 admin/password2 登录,可在控制台看到如下日志

3. 修改电影微服务
> 复制项目 microservice-consumer-movie-feign,将 ArtifactId 修改为 microservice-consumer-movie-feign-manual
> 去掉Feign接口 UserFeignClient上的@FeignClient注解
package com.itmuch.cloud.microserviceconsumermoviefeignmanual.feign; import com.itmuch.cloud.microserviceconsumermoviefeignmanual.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; public interface UserFeignClient { @GetMapping(value = "/{id}")
User findById(@PathVariable("id") Long id); }
> 去掉启动类上的 @EnableFeignClients 注解
package com.itmuch.cloud.microserviceconsumermoviefeignmanual; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceConsumerMovieFeignManualApplication { public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieFeignManualApplication.class, args);
} }
> 修改 Controller
package com.itmuch.cloud.microserviceconsumermoviefeignmanual.controller; import com.itmuch.cloud.microserviceconsumermoviefeignmanual.feign.UserFeignClient;
import com.itmuch.cloud.microserviceconsumermoviefeignmanual.pojo.User;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; @Import(FeignClientsConfiguration.class) // Spring Cloud为Feign默认提供的配置类
@RestController
public class MovieController { private UserFeignClient userUserFeignClient;
private UserFeignClient adminUserFeignClient; public MovieController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
this.userUserFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
.requestInterceptor(new BasicAuthRequestInterceptor("user", "password1"))
.target(UserFeignClient.class, "http://microservice-provider-user/");
this.adminUserFeignClient = Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract)
.requestInterceptor(new BasicAuthRequestInterceptor("admin", "password2"))
.target(UserFeignClient.class, "http://microservice-provider-user/");
} @GetMapping("/user-user/{id}")
public User findByIdUser(@PathVariable Long id) {
return this.userUserFeignClient.findById(id);
} @GetMapping("/user-admin/{id}")
public User findByIdAdmin(@PathVariable Long id) {
return this.adminUserFeignClient.findById(id);
} }
> 启动 microservice-discovery-eureka
> 启动 microservice-provider-user-with-auth
> 启动 microservice-consumer-movie-feign-manual
> 访问 http://localhost:8010/user-user/1,可正常获取结果,并且在用户微服务控制台打印如下日志

> 访问 http://localhost:8010/user-admin/1,可正常获取结果,并且在用户微服务控制台打印如下日志

4. 总结
由测试不难发现,手动创建Feign的方式更加灵活。
下文将讲解Feign对集成的支持、对压缩的支持、日志、构造多参数请求等。敬请期待~~~
5. 参考
周立 --- 《Spring Cloud与Docker微服务架构与实战》
SpringCloud系列十二:手动创建Feign的更多相关文章
- SpringCloud系列十二:SpringCloudSleuth(SpringCloudSleuth 简介、SpringCloudSleuth 基本配置、数据采集)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringCloudSleuth 2.具体内容 Sleuth 是一种提供的跟踪服务,也就是说利用 sleuth 技术 ...
- Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】
2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...
- SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据
原文:SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Se ...
- Alamofire源码解读系列(十二)之请求(Request)
本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...
- struts2官方 中文教程 系列十二:控制标签
介绍 struts2有一些控制语句的标签,本教程中我们将讨论如何使用 if 和iterator 标签.更多的控制标签可以参见 tags reference. 到此我们新建一个struts2 web 项 ...
- 爬虫系列(十二) selenium的基本使用
一.selenium 简介 随着网络技术的发展,目前大部分网站都采用动态加载技术,常见的有 JavaScript 动态渲染和 Ajax 动态加载 对于爬取这些网站,一般有两种思路: 分析 Ajax 请 ...
- 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探
SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...
- Alamofire源码解读系列(十二)之时间轴(Timeline)
本篇带来Alamofire中关于Timeline的一些思路 前言 Timeline翻译后的意思是时间轴,可以表示一个事件从开始到结束的时间节点.时间轴的概念能够应用在很多地方,比如说微博的主页就是一个 ...
随机推荐
- 福州3中集训day5
数论,zld神犇认为我们都学过数论的,讲了一波高端(入门?)操作,从扩展欧几里得开始,同余方程诸如此类,早晚得重修.连课件都没,拿着画图讲了一上午 sro_zld_orz 具体内容都记在本上. 还是说 ...
- CSS中的层叠、特殊性、继承、样式表中的@import
CSS中的层叠.特殊性.继承.样式表中的@import 层叠 CSS有一个机制是层叠,层叠可以理解为对样式的覆盖,优先性为: 网站开发者的样式表 用户样式(通过设置浏览器的显示选项) 浏览器默认的样式 ...
- 【计算几何】【辛普森积分法】UVALive - 7076 - Highway
春节前后想了好久才在队友的讲解下想明白…… 太难讲了,我就不讲了,大概就是考虑直着走到高速上还是斜着走到高速上,然后平移直线和大圆相切,把生成的最大的“桥”和大圆并一下就行了. #include< ...
- SciPy中两个模块:io 和misc
读写.mat文件 如果你有一些数据,或者在网上下载到一些有趣的数据集,这些数据以Matlab的.mat 文件格式存储,那么可以使用scipy.io 模块进行读取. data = scipy.io.lo ...
- iOS开发中几种常见的存储方式
1.archive 归档 数据的保存 1: let result = NSKeyedArchiver.archiveRootObject(contacts, toFile: path as Strin ...
- mongodb_服务端安装及连接
安装环境:Windows7 64位 附件内容: 1.mongodb Windows7 补丁:451413_intl_x64_zip.exe 2.mongodb Windows 安装程序:mon ...
- linux下目录大小为什么是4K?一个目录下最多有个多少个子目录?最多有多少个文件?ls -l显示的内容中total到底是什么?
子目录数太多,会影响搜索性能. 在同一个路径下,一级子目录的个数限制为31998,如果你的应用生成的目录可能会超过这个数,那要注意进行目录分级.例如,如果目录名为数字的话,可以将数字除以10000后的 ...
- 【C/C++学院】0723-32位与64位/调戏窗体程序/数据分离算法/内存检索/二分查找法/myVC
[送给在路上的程序猿] 对于一个开发人员而言,能够胜任系统中随意一个模块的开发是其核心价值的体现. 对于一个架构师而言,掌握各种语言的优势并能够运用到系统中,由此简化系统的开发,是其架构生涯的第一步. ...
- 实现一个Cglib代理Demo
Cglib动态代理采用的是创建目标类的子类的方式.优点:不用实现额外接口,只操作我们关心类,高性能. package jesse.test; import java.lang.reflect.Meth ...
- Spark(一)-- Standalone HA的部署
首先交代一下集群的规模和配置 集群有六台机器,均是VM虚拟机,每台256M的内存(原谅楼主物理机硬件不太给力) 主机名分别是cloud1~cloud6 每台机器上都装有jdk6,和hadoop-2.2 ...