自定义feign配置与服务调用的安全验证( 二 )

getAuthorities() {Collection authorities=new ArrayList<>();SimpleGrantedAuthority authority=new SimpleGrantedAuthority(this.role);authorities.add(authority);return authorities;}private Long id;private String username;private String password;private String role;public Long getId() {return id;}public void setId(Long id) {this.id = id;}@Overridepublic String getUsername() {return username;}@Overridepublic boolean isAccountNonExpired() {return true;}@Overridepublic boolean isAccountNonLocked() {return true;}@Overridepublic boolean isCredentialsNonExpired() {return true;}@Overridepublic boolean isEnabled() {return true;}public void setUsername(String username) {this.username = username;}@Overridepublic String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getRole() {return role;}public void setRole(String role) {this.role = role;}}}
启动项目hello-auth后,可以见到页面需要输入用户名和密码才能访问 。
package com.example.helloauthconsumerfeign.service;import com.example.helloauthconsumerfeign.model.User;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;/*** @author billjiang 475572229@qq.com* @create 17-8-23*///@FeignClient(value="http://www.kingceram.com/post/hello-auth")public interface HelloAuthService {@GetMapping("/{id}")User findById(@PathVariable("id") Long id);}
3)编写类如下
package com.example.helloauthconsumerfeign.controller;import com.example.helloauthconsumerfeign.model.User;import com.example.helloauthconsumerfeign.service.HelloAuthService;import feign.Client;import feign.Contract;import feign.Feign;import feign.auth.BasicAuthRequestInterceptor;import org.springframework.beans.factory.annotation.Autowired;import feign.codec.Decoder;import feign.codec.Encoder;import org.springframework.cloud.netflix.feign.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;/*** @author billjiang 475572229@qq.com* @create 17-8-26*/@Import(FeignClientsConfiguration.class)@RestControllerpublic class HelloAuthFeignController {private HelloAuthService userAuthService;private HelloAuthService adminAuthService;@Autowiredpublic HelloAuthFeignController(Decoder decoder, Encoder encoder, Client client, Contract contract){this.userAuthService= Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract).requestInterceptor(new BasicAuthRequestInterceptor("user","123456")).target(HelloAuthService.class,"http://hello-auth/");this.adminAuthService= Feign.builder().client(client).encoder(encoder).decoder(decoder).contract(contract).requestInterceptor(new BasicAuthRequestInterceptor("admin","123456")).target(HelloAuthService.class,"http://hello-auth/");}@GetMapping("/user/{id}")public User findByIdUser(@PathVariable Long id){return this.userAuthService.findById(id);}@GetMapping("/admin/{id}")public User findByIdAdmin(@PathVariable Long id){return this.adminAuthService.findById(id);}}
在实际业务中会根据不同的角色 , 执行不同的业务操作 , 基于以上示例可以在服务之间完成复杂的服务鉴权 。