extends GrantedAuthority> getAuthorities() {Collection
启动项目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);}}
在实际业务中会根据不同的角色 , 执行不同的业务操作 , 基于以上示例可以在服务之间完成复杂的服务鉴权 。
- 二、Vue2.0项目结构内容及配置解析
- Puppet:维护运行环境一致性的利器
- Unity2021+VS2022调试
- 自定义布局Dialog 自定义dialog右上角带叉号的dialog 点击右上角
- 如何安装配置VS Code|配置C语言编译环境|彻底卸载VS Code
- PyQt窗口FramelessWindowHint模式下自定义窗口缩放与移动功能
- 自定义版式 信创办公–基于WPS的PPT最佳实践系列
- roadflow asp.net工作流自定义表单
- 包含原理配置实现及简易demo CAS4.1单点登录实现
- 一个路由器配置多台不同网段的PC,使PC间能够互通